← Nesim Apps

Ads in Kotlin Multiplatform: Shared Interface, Platform Shell

By Nesim Razon, independent mobile developer · 2026-11-23

#Android#iOS#Ads

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.

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.

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.

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.

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.

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.

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