I'm following the guide here to try and use an iOS framework without CocoaPods in a new KMM project:
https://kotlinlang.org/docs/kmm-add-dependencies.html#without-cocoapods
I have an existing, working .xcframework that I added to the project under shared/src
. I added a MyKit.def
file in src/nativeInterop/cinterop/
and updated the build.gradle.kts
file in same shared dir:
MyKit.def looks like
language = Objective-C
modules = MyKit
package = MyKit
build.gradle.kts
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
plugins {
kotlin("multiplatform")
id("com.android.library")
}
kotlin {
android()
val iosTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
::iosArm64
else
::iosX64
iosTarget("ios") {
binaries {
framework {
baseName = "shared"
}
}
}
sourceSets {
val commonMain by getting
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13.2")
}
}
val iosMain by getting
val iosTest by getting
}
iosArm64() {
compilations.getByName("main") {
val MyKit by cinterops.creating {
// Path to .def file
defFile("src/nativeInterop/cinterop/MyKit.def")
compilerOpts("-framework", "MyKit", "-F/src/MyKit.framework")
}
}
binaries.all {
// Linker options required to link to the library -- the framework binary is located under src/MyKit.framework/ios-arm64/MyKit.framework/
linkerOpts("-framework", "MyKit", "-F/src/MyKit.framework/ios-arm64/MyKit.framework/")
}
}
}
android {
compileSdkVersion(31)
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdkVersion(26)
targetSdkVersion(31)
}
}
After adding import MyKit.*
to my MainActivity I get unknown reference
errors.
Are iOS binary frameworks supported? They have a .
separator in the file name so maybe that's an issue. Problem with my -F
paths?? I'm not clear on whether the path should be all the way to the dir with Headers
and the binary itself or just to the Framework root. TIA
-F
option. 2. Have you performed thecinteropMyKitIosArm64
task before using the import in youriosArm64Main
source set? β Prizewinnerbuild.gradle.kts
I'm running Gradle sync and then running the app from Android Studio on a device, so I would assume theiosArm64
step has been run. I did not see any instructions to run a cinterop task. Thanks! β Novice.xcframework
or.framework
extension. If your cinterop block contains the correct extension, then just forget about this question. 2. What are the results of thecinteropMyKitIosArm64
in your case? In general, it should create a.klib
file located at$projectbuild/classes/kotlin/native/main/cinterop/.
if its empty that mean thecinterop
tool did not found the appropriate headers to generate bindings. β PrizewinnerMyKit.framework/module.modulemap
andMyKit.framework/Headers/MyKitHeadersAsNamedInTheModulemap
. β Prizewinnerxcframework
usingxcode
and added dependency toAndroid studio
and was able to create.klib
library in$projectbuild/classes/kotlin/native/main/cinterop/
. But couldn't import my library either inxcode
nor iniosMain
source set. β Fermat