Ads in Kotlin Multiplatform: Shared Interface, Platform Shell
By Nesim Razon, independent mobile developer · 2026-11-23
Ads are the part of a Kotlin Multiplatform app that cannot be shared, because the SDKs are native on each side. What can be shared is everything around them: when to show an ad, what happens if there is none, and what the screens are allowed to know. This is how my small Android and iOS apps split that work, with real names from the code.
One small interface in shared code
In Slinglings, the shared module has one interface named GameServices. The shared Compose screens know only this interface and nothing about AdMob. Its members fall into a few groups.
A global holder keeps the current implementation: Services.impl starts as NoServices, an object with no ads, where a rewarded request gives the reward immediately. The Android activity replaces it in onCreate with AndroidServices, which wraps AdMob, UMP, Play Billing and Firebase Analytics. One rule shapes every method: the callback always fires. If no ad is ready, onDone is called at once, so the game never waits for an ad.
- adsRemoved, billingAvailable, removeAdsPrice: state for the remove-ads purchase.
- interstitialBeforeNext(totalSolved, onDone): called when the player taps Next after a level.
- showRewarded(onReward, onUnavailable): a rewarded ad that the player asks for.
- privacyOptionsRequired and showPrivacyOptions(): the GDPR privacy options entry in settings.
- logLevelComplete and similar log functions: analytics events.
Why an interface and not expect/actual
I use expect/actual in these projects too, but only for tiny pure functions: systemLanguage in Slinglings, image decoding in Snowlings. Ads are different, for four reasons.
- An ad needs an Activity on Android and a UIViewController on iOS. Both exist only at run time, so a compile-time declaration has nothing to hold on to.
- An interface can be swapped. NoServices serves previews and unit tests, and CalisanAsistani has NoAds for the same purpose.
- A Kotlin interface is exported to Swift as a protocol. The iOS ad SDK stays in Swift and the Kotlin module never imports it.
- The shared code stays testable: the frequency rules below need no device, only counters and a clock.
The iOS entry point makes the same idea explicit. In CalisanAsistani the function MainViewController takes a nullable IosNativeServices, and the Kotlin class IosAds calls it with a safe call. Pass null and the app simply runs without ads.
Android shell: consent first, SDK second
On Android the class AdMobAds does the work, and the order follows Google's guidance. Ads must not start before consent is settled.
- 1. requestConsentInfoUpdate refreshes the consent state.
- 2. loadAndPresentIfRequired shows the UMP form only where the law requires it.
- 3. canRequestAds is checked. If a previous session already stored consent, the SDK starts without waiting.
- 4. MobileAds.setRequestConfiguration runs, then MobileAds.initialize, off the main thread.
- 5. When initialization finishes, an interstitial and a rewarded ad are loaded. After each one closes, the next is loaded.
Step 4 has a lesson in a code comment. The initialize callback can arrive on the calling thread, but loading ads needs the main thread. In version 1.1.0 this crashed now and then at startup with the message that it must be called on the main UI thread. The fix is to post the loads to the main handler. A rewarded ad gives its reward only after it closes, so the player sees the result when back in the game.
Test IDs in debug, real IDs in release
Ad unit IDs are not secret, but mixing test and real IDs is dangerous: clicking your own real ads can get an account flagged. The Gradle files in Astrolings and CalisanAsistani solve it by construction.
- Debug builds always use Google's public test IDs.
- Release builds read the real values from gradle.properties, with keys such as admob.app and admob.banner.
- If a real value is missing, the release task throws an error and no store bundle is built, so test ads cannot reach the store by mistake.
- In debug, UMP is told to behave as if the device were in the EEA, using a hashed test device ID, so the consent form can be tested from any country.
- Home phones are registered with setTestDeviceIds, from a comma separated Gradle property. On iOS the same job is done by an AdMobTestDeviceIDs list in the Info.plist.
The iOS side: a Swift bridge
On iOS a file named NativeServices.swift implements the Kotlin interface. In CalisanAsistani the interface is called IosNativeServices and offers a handful of members: logEvent, setAdsReadyListener, makeBannerView, bannerHeight, showInterstitialAd, privacyOptionsRequired and showPrivacyOptions. The SwiftUI app passes the Swift object into MainViewController, and the Kotlin class IosAds turns it into the same shared Ads interface that Android uses.
The flow matches Android: consent info update, form if required, then MobileAds start and an interstitial preload. Two details matter. The Swift start function is called about 0.6 seconds after the first screen appears, because the consent form needs a window to present from. And the interstitial method returns a Boolean: true if it showed an ad, false if none was ready. On false the Kotlin side calls onDone itself.
- Banner: a Compose UIKitView whose factory calls makeBannerView, with the height from bannerHeight.
- Astrolings uses a banner only. CalisanAsistani uses banner and interstitial.
- Slinglings on iOS still runs with NoServices. The iOS ad unit IDs are stored in code, but the Swift part is not written yet.
A naming trap: the Kotlin Ads interface and the Swift-facing interface should not share a method name with a different signature. Swift then complains about argument labels. That is why the Swift-facing method is called showInterstitialAd and not showInterstitial.
Frequency rules live in shared code
The policy for how often to show an ad is plain Kotlin, so both platforms get it. In CalisanAsistani the AdPolicy object sits in commonMain and uses a monotonic time source, so changing the device clock cannot cheat it.
- CalisanAsistani: no ad for the first 3 returns to the home screen, then every 3rd return, and at least 240 seconds after the last ad. Never inside a calculator screen.
- Slinglings: no ads in the first 6 solved levels, then at most one every 3 levels, at least 90 seconds apart, up to 8 a day. Never while playing, in menus or on undo.
- Rewarded ads only when the player asks, for example after hints run out.
- The counters live only in the app process. They reset when the app restarts.
Because the rules only depend on counters and time, they are easy to test without a device. A rule that decides when to annoy a player deserves a test.
Analytics from the first iOS version, and debugging
My iOS checklist says Firebase Analytics and Crashlytics come with the first version. The rule dates from 14 November 2026, after the 2048 iOS app shipped without them. The Swift app calls FirebaseApp.configure first, before anything else. Crashlytics starts by itself, and a build step uploads the dSYM files. Kotlin sends events through the logEvent member, with the same names as Android. In CalisanAsistani no salary, date or other personal value is ever sent.
For debugging, the Android side logs ad events: loaded, shown, impression, clicked, dismissed. One extra event, resumed_no_dismiss, fires when the app returns to the front and the SDK still says an ad is showing. That caught a real stuck ad on one phone. The full story is in the article Catching a Stuck Interstitial Ad on Android With Event Logs. On iOS, the banner delegate writes a short NSLog line when an ad arrives or fails, readable over USB.
Frequently asked questions
Can the AdMob SDK be called directly from Kotlin common code?
Not as one shared call. The Android and iOS SDKs are separate native libraries, so each platform keeps its own shell behind a shared interface.
Why not use expect/actual for ads?
Ads need an Activity or a view controller that exist only at run time, and an interface can be swapped for a no-ads version in tests and previews.
How do you avoid clicking your own real ads while developing?
Debug builds use Google's test IDs, release builds fail without real IDs, and test devices are registered on both Android and iOS.
When should the consent form appear?
Before any ad request. Check for consent first, show the UMP form if required, and start the ad SDK only when canRequestAds is true.
Should the ad rules be shared too?
Yes. Keeping the frequency policy in common Kotlin gives both platforms the same behavior and lets you unit test it.
More from the blog
- Measuring Play Installs: UTM Links and Listing Experiments
- Secret Santa Draw Algorithm: No Self-Picks, No Swaps
- No macOS Minutes Left on GitHub Actions: Moving to Codemagic
- Edge-to-Edge and 3-Button Navigation: Fixing Clipped Compose UI
- Clarity AI Visibility: Topic Insights for a Small Site, First Results
- Scheduling 36 YouTube Community Posts When There Is No API