Search code examples
kotlincocoapodskotlin-multiplatform

Cocoapod dependency in iosMain of a Kotlin Multiplatform project, cocoapod unresolved reference


I am trying to create a wrapper around AWS Amplify to use for my project. I have a module inside my shared ( common ) module called Amplify. Here I integrated cocoapods as instructed in the official docs. But when I try to import anything from the iosMain I keep getting Unresolved reference: cocoapods.

My project structure is as follows

Common module (shared)
|- root module
|- other features
|- amplify wrapper module

In the root module I have the

kotlin {
    ios {
        binaries {
            framework {
                baseName = "Framework"
                linkerOpts.add("-lsqlite3")
                export(project(":common:main"))
            }
        }
    }

And the setup for the cocoapods in the amplify module

import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

plugins {
    id("multiplatform-setup") // this is from buildSrc, it adds the multiplatform plugin
    id("android-setup")
    id("org.jetbrains.kotlin.native.cocoapods")
    kotlin("plugin.serialization") version "1.5.10"
}

version = "1.0"


kotlin {
    sourceSets {
        named("commonMain") {
            dependencies {
            }
        }
    }

    cocoapods {
        summary = "Amplify wrapper for KMP project"
        homepage = "Link to a Kotlin/Native module homepage"
        frameworkName = "AmplifyKMP"

        pod("Amplify")
        pod("AmplifyPlugins/AWSCognitoAuthPlugin")
        pod("AmplifyPlugins/AWSPinpointAnalyticsPlugin")
    }
}

My idea is that I'd be able to expose my wrapper from the commonMain code, that would call actual implementations from the Amplify Android and Amplify IOS libraries. My first problem is the cocoapods being unresolved and secondly all of the examples from the official docs and github all have cocoapods in the main module (root module in my case) from where they export the framework, I am not sure my approach is even feasible.


Solution

  • The first problem is easy. You need to add kotlin("native.cocoapods") to your plugins section.

    On the second, can a sub-module import pods with cinterop and make them available to a module that depends on them? I haven't tried it. In theory the cocoapods plugin should be able to just import pods definitions to kotlin. However, the cocoapods Kotlin gradle plugin (aka kotlin("native.cocoapods")) will configure ios targets to create a framework. That might create issues with the dependency config.

    In the amplify module I don't see that you're defining any iOS targets, so you'll probably need to do that, but that config will be altered by kotlin("native.cocoapods"). You'll likely need to step in and modify that yourself. You can step in and do that in gradle, but I would prepare to spend some time tweaking that.

    https://github.com/touchlab/KaMPKit/blob/main/shared/build.gradle.kts#L114