Advance apologies, on this being a long one. I’m w...
# ios
c
Advance apologies, on this being a long one. I’m working on a KMM module and not strong with gradle. I’m trying to use the gradle XCFramework plugin in conjunction cocoapods for external dependency management, and I have several questions about how to make all the iOS pieces play together. Below is basically what the iOS outline of my gradle looks like (reduced to the relevant iOS bits)
Copy code
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.apple.XCFramework

val bitcode: String = if ("release".equals(configuration, true)) "bitcode" else "marker"
val iosFrameworkName = "FooFramework"

kotlin {
    
    val xcf = XCFramework(iosFrameworkName)

    ios {
        binaries.framework {
            xcf.add(this)
        }
    }

    val iosTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget = when {
        System.getenv("SDK_NAME")?.startsWith("iphoneos") == true -> ::iosArm64
        System.getenv("NATIVE_ARCH")?.startsWith("arm") == true -> ::iosSimulatorArm64
        else -> ::iosX64
    }
    iosTarget("ios") {}

    cocoapods {
        summary = "summary here"
        homepage = "<https://www.example.com>"
        ios.deploymentTarget = "10.0"
        podfile = project.file("../iosApp/Podfile")
        framework {
            baseName = iosFrameworkName
            freeCompilerArgs += listOf("-module-name", "FOO")
            embedBitcode(bitcode)
        }
        pod("AFNetworking", "~> 4.0.1")
    }

}
First question: Am I doing it wrong? 😀 Second question: Is it right to configure the framework (baseName, module-name, etc) inside the
cocoapods
extension instead of the
ios
target? Third question: When set up this way, the
iosTarget
workaround to for the IDE in conjunction with cocoapods reports exceptions about the x64 architecture already having a binary:
Copy code
Cannot add binary podDebugFramework dependency to default fat framework
java.lang.IllegalArgumentException: This fat framework already has a binary for architecture `x64` (podDebugFramework for target `iosX64`)…

Cannot add binary podReleaseFramework dependency to default fat framework
java.lang.IllegalArgumentException: This fat framework already has a binary for architecture `x64` (podReleaseFramework for target `iosX64`)…
Even though things seem to build successfully, even with these pod<config>Framework exceptions, is there a possibility of a problem rearing its head? Is there something I can do to clean the exceptions up? references: Build XCFramework: https://kotlinlang.org/docs/mpp-build-native-binaries.html#build-xcframeworks iOS dependencies with CocoaPods: https://kotlinlang.org/docs/kmm-add-dependencies.html#with-cocoapods Note for iOS dependencies needing workaround to get code completion/highlighting in the IDE for the 3rd party libs https://kotlinlang.org/docs/kmm-add-dependencies.html#workaround-to-enable-ide-support-for-the-shared-ios-source-set
Since some of the conflict seems to from the interplay of invoking the
ios
target alongside the IDE workaround, does the following approach make sense to check if we’re being invoked by AS to get the IDE features working, otherwise just use the
ios
target?
Copy code
if (properties.containsKey("android.injected.invoked.from.ide")) {
    // When running from Android Studio, the shared iOS source set needs this workaround for IDE features like code-completion/highlighting with 3rd party iOS libs
    // <https://kotlinlang.org/docs/kmm-add-dependencies.html#workaround-to-enable-ide-support-for-the-shared-ios-source-set>
    val iosTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget = when {
        System.getenv("SDK_NAME")?.startsWith("iphoneos") == true -> ::iosArm64
        System.getenv("NATIVE_ARCH")?.startsWith("arm") == true -> ::iosSimulatorArm64
        else -> ::iosX64
    }
    iosTarget("ios") {}
} else {
    val xcf = XCFramework(iosFrameworkName)
    ios {
        binaries.framework {
            baseName = iosFrameworkName
            xcf.add(this)
        }
    }
}