basic-ads
1.2.0-beta01indexedEnables rapid integration of Google AdMob on mobile platforms, displaying ads as Composables with customizable ad experiences through lambdas for flexible user interactions.
Enables rapid integration of Google AdMob on mobile platforms, displaying ads as Composables with customizable ad experiences through lambdas for flexible user interactions.
A Kotlin Multiplatform library to rapidly get Google AdMob running on Android and iOS
Basic-Ads uses the existing Android and iOS Google AdMob libraries to display ads as Composables.
A full walkthrough is available on Medium,
and there's also an easy-start template.
In case you need it, here's the Basic-Ads API Documentation
For Android, complete the steps in AdMob's instructions:
For iOS, complete the steps in AdMob's instructions:
[For GDPR Compliance Only] Import the User Messaging Platform SDK
[!NOTE] For Xcode 13+, you can update your Custom iOS Target Properties.
[!IMPORTANT] Don't forget to check the list of transitive dependencies and versions to ensure compatibility.
Add your dependencies from Maven
then include the library in your gradle build
// in your 'composeApp/build.gradle.kts' file
plugins {
alias(libs.plugins.jetbrains.compose)
alias(libs.plugins.jetbrains.compose.compiler)
}
sourceSets {
commonMain.dependencies {
implementation(compose.runtime)
implementation(compose.foundation)
implementation(compose.material)
implementation(compose.ui)
implementation(libs.basic.ads)
}
androidMain.dependencies {
implementation(libs.google.play.services.ads)
implementation(libs.android.ump)
}
}
Call BasicAds.initialize in your commonMain before building ads.
// in your 'composeApp/src/commonMain/App.kt' file
@OptIn(DependsOnGoogleMobileAds::class)
@Composable
fun App() {
BasicAds.Initialize()
}
BannerAdYou can build a BannerAd via a Composable function:
// in your 'composeApp/src/commonMain/AdScreen.kt' file
@Composable
fun AdScreen() {
BannerAd() // Results in a Test Banner Ad being created
}
If you want to preload your BannerAds, there's a way to do that too:
// Load the Ad within your Composable
val bannerAd by rememberBannerAd()
// Display the Ad as soon as it's available
BannerAd(bannerAd)
You can also check the AdState before doing something:
// Load the Ad within your Composable
val bannerAd by rememberBannerAd()
// Determine to show or hide the Ad
var showBannerAd by remember { mutableStateOf(false) }
// Composable Button with callbacks
Button(
onClick = { showBannerAd = true }, // Shows the ad on click
/** Checks AdState and disables the button if ad isn't ready **/
enabled = bannerAd.state == AdState.READY
) { Text() }
(showBannerAd){
BannerAd(bannerAd)
}
You can also build other Ad types.
// in your 'composeApp/src/commonMain/AdScreen.kt' file
InterstitialAd()
RewardedAd(onRewardEarned = {/** do something here **/})
RewardedInterstitialAd(onRewardEarned = {/** do something here **/}) // currently a Google Beta feature
If you want to preload your ads, there's a way to do that too:
// Load the Ad within your Composable
val rewardedAd by rememberRewardedAd()
// Display the Ad as soon as it's available
RewardedAd(
ad = rewardedAd,
onRewardEarned = { rewardItem ->
/** do something here with the rewardItem (if it exists) **/
}
)
You can also check the AdState before doing something:
// Load the Ad within your Composable
val interstitialAd by rememberInterstitialAd()
// Determine to show or hide the Ad
var showInterstitialAd by remember { mutableStateOf(false) }
// Composable Button with callbacks
Button(
onClick = { showInterstitialAd = true }, // Shows the ad on click
enabled = interstitialAd.state == AdState.READY
) { Text() }
(showInterstitialAd){
InterstitialAd(interstitialAd)
}
[!TIP] Consent Popups are typically only required for Californian or International audiences. GDPR is a very in-depth topic, so please begin by reading about what GDPR is and how AdMob complies with GDPR requirements.
You can use the Consent features of Basic Ads with Composables too:
// in your 'composeApp/src/commonMain/AdScreen.kt' file
// You can also configure debugging using the overload version of this function
// val consent by rememberConsent(debugSettings = ConsentDebugSettings(true))
val consent by rememberConsent()
// Create a ConsentPopup (if available in your region)
ConsentPopup(consent)
// Check if the user can see ads
if (consent.canRequestAds) {
// Call your ads here
InterstitialAd()
}
[!CAUTION] Advanced Users Only
# in your 'libs.versions.toml' file
[versions]
kotlin = "+" # gets the latest version
compose = "+" # gets the latest version
basic = "+" # gets the latest version
google-play-services-ads = "+" # you did this during the preparation step
android-ump = "+" # you did this during the preparation step
[libraries]
basic-ads = { module = "app.lexilabs.basic:basic-ads", version.ref = "basic"}
google-play-services-ads = { module = "com.google.android.gms:play-services-ads", version.ref = "google-play-services-ads"}
android-ump = { module = "com.google.android.ump:user-messaging-platform", version.ref = "android-ump" }
[plugins] # make sure you're using the JetBrains plugin to import your composables
jetbrains-compose = { id = "org.jetbrains.compose", version.ref = "compose" }
jetbrains-compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
File > Invalidate Caches..., check all boxes and hit invalidate and restartSync for gradle if banner exists. Ignore the flood of warning lights and klaxons.Build > Clean Cache. Ignore the plethora of errorsBuild > Rebuild Project. NOTE: Despite religious preference, prayer is encouraged.Surfaced from shared tags and platforms — no rankings paid for.