https://kotlinlang.org logo
Title
k

kitto

02/10/2023, 12:26 PM
Hello, I have a question, for KMM iOS default build sourceset generates in gradle like this:
val iosX64Main by getting
        val iosArm64Main by getting
        val iosSimulatorArm64Main by getting
        val iosMain by creating {
            dependsOn(commonMain)
            iosX64Main.dependsOn(this)
            iosArm64Main.dependsOn(this)
            iosSimulatorArm64Main.dependsOn(this)
            dependencies {
              //
            }
        }
can I achieve this processor arch separation for Android too? (androidNativeArm64?) (I'm trying to add some C static compiled library with kotlin-native cinterop)
e

ephemient

02/10/2023, 12:33 PM
https://kotlinlang.org/docs/native-target-support.html the recently-updated list of native targets on the Kotlin website shows the current Android NDK targets:
androidNativeArm32
androidNativeArm64
androidNativeX86
androidNativeX64
k

kitto

02/10/2023, 12:58 PM
ok, thanks! Now, when I'm using list of archs like that:
listOf(
    androidNativeArm64(),
    androidNativeArm32(),
    androidNativeX64(),
    androidNativeX86()
).forEach {
    it.binaries.framework {
        baseName = "shared"
    }
    it.compilations.getByName("main") {
        cinterops {
            val libssl by creating
        }
    }
}
I'm getting a build error :
java.lang.IllegalArgumentException: Cannot create a framework: debugFramework. Binaries of this kind are not available for target androidNativeArm64
(I unfortunately don't really know what I'm doing in regard to gradle builds)
e

ephemient

02/10/2023, 1:09 PM
binaries.framework
is only for Objective-C (macos, ios). use
binaries.sharedLib
for other platforms
k

kitto

02/10/2023, 1:34 PM
oh, thanks for the proper tag! Can I set somewhere an option to keep on using the 'common' libs in android (in my example I'm using jvm ktor, but after adding new builds, gradle trying to look up 'cinterop-klib' by default) This is the log:
> No matching variant of io.ktor:ktor-client-core:2.2.2 was found. The consumer was configured to find a usage of 'kotlin-cinterop' of a library, with the library elements 'cinterop-klib', preferably optimized for non-jvm, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native', attribute 'org.jetbrains.kotlin.native.target' with value 'android_arm64' but:
          - Variant 'commonMainMetadataElements' capability io.ktor:ktor-client-core:2.2.2 declares a usage of 'kotlin-api' of a library:
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'common' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native'
              - Other compatible attributes:
                  - Doesn't say anything about its target Java environment (preferred optimized for non-jvm)
                  - Doesn't say anything about its elements (required them with the library elements 'cinterop-klib')
                  - Doesn't say anything about org.jetbrains.kotlin.native.target (required 'android_arm64')
e

ephemient

02/10/2023, 1:36 PM
ktor depends on kotlinx.coroutines depends on kotlinx.atomicfu… none of which are currently published for androidNative :(
you can either rebuild them yourself, adding those platforms, or rely on somebody else's builds, such as https://github.com/danbrough/kotlinxtras
k

kitto

02/10/2023, 1:42 PM
oh, so androidNative is not compatible with android-JVM stuff, ok I hope I can avoid it then, by keeping androidNative as a small lib, and put it as a lib into another project (I really hope there is no traps in this approach)
k

kitto

02/10/2023, 2:26 PM
Thank you for the help, you're the best kotlin Tachikoma
e

Evegenii Khokhlov

02/11/2023, 7:31 AM
@kitto I recently posted an article about how to add c/c++ code to your Kotlin Multiplatform shared library it was very much inspired by Cashapp’s Zipline project Please take a look, I believe for Android is better to use Android NDK from android plugin for native code and not to use Android Native targets in KMM. Very simple example you can find in the repo.