swiftobjective-ckotlinadmobkotlin-multiplatform

KMP Library to interact with AdMob API


I can independently build projects for iOS (using Swift) or Android (using Kotlin or Java) with Google AdMob. Unfortunately, my company switched to Kotlin Multiplatform (KMP), which isn't directly supported by Google yet.

Here's a typical KMP project structure for me in case you're wondering:

- composeApp
  - src
    + androidMain # Android implementation
    + commonMain # Shared implementation
    + iosMain # iOS implementation
- iosApp # Swift / iOS-specific frameworks
  - iosApp # target iOS app

In my KMP project, I have built a commonMain class with functions that call androidMain and iosMain implementations. androidMain is working well displaying ads, but I'm struggling to get AdMob working on the iOS side.

Are there any instructions or walkthroughs for how to create an iosMain / iosApp implementation of AdMob in a Kotlin Multiplatform (KMP) project?


Solution

  • which isn't directly supported by Google yet

    Well, that depends what you mean by "Google" and "support".

    Are there any instructions or walkthroughs for how to create an iosMain / iosApp implementation of AdMob in a Kotlin Multiplatform (KMP) project?

    Probably nothing specific to AdMob. If what you're calling in common code is relatively limited, I'd create an interface in common with those calls, then implement Android in androidMain, and implement the iOS side in Swift.

    Interface, not expect/actual. Generally speaking, you won't use expect/actual much for classes/objects. If ever.

    Create an implementation of the Kotlin interface in Swift, and pass it in on app start.

    I gave a talk about bridging native apis a couple years ago: https://www.droidcon.com/2022/06/28/sdk-design-and-publishing-for-kotlin-multiplatform-mobile/