Adding a `.klib` library to kotlin multiplatform
Asked Answered
O

1

3

I'm wondering how I'd be able to import my cinterop-ted library to gradle build of the kotlin multiplatform build.

I've already created the library.def file and filled it, I also generated the library.klib and the folder that goes with it. I just don't understand how to import it into gradle.

I've looked all over internet and I found a reference to Konan and I'm wondering if that's something I have to use, or if that's something being used for something similar to 'cinterop'.

I've looked over the following links, and I haven't found anything remotely connected to the .klib import part of my question.

Link #1 (kotlinlang.org)

Link #2 (github.com)

Link #3 (plugins.gradle.org)

Obligate answered 23/7, 2019 at 18:38 Comment(7)
did you manage to get this working in the end? I am also created the my .klib but no Idea how to load it in. Maybe you used Gradle .interops in the end, but I've not been able to get that working yet. – Constanceconstancia
I'm pretty sure I just gave up on the project entirely. I'm not sure I ever got it working, but back in those days, Kotlin Native was still in the infancy. – Obligate
I'll add an answer when I get it working tomorrow (hopefully). At the moment my Gradle won't even recognize the interops keywords. I wish I was a 6502 developer back in the 80s right now lol – Constanceconstancia
@Pixel i feel ya πŸ˜…, good luck with the project! If you do manage to get it working, write an answer and I'll accept it. – Obligate
@Pixel did you get it working? – Obligate
@IMlolenstine not yet. I've be made progress (see my post here #66851818) but still working on it. I decided to keep hacking away at this problem, but also work on Kotlin/Native/JNI solution too. That way I get what I want working (albeit not with KMM) but if I do manage to get KMM working I can easily reuse my Kotlin/JNI code. I'll keep working on the KMM so hopefully will get it working at some point and update this and the other post. – Constanceconstancia
@IMlolenstine hey, I think I found a solution for my use case (C/C++ interop). Perhaps it might work for you too. Check out the comments in Artyom's answer in the linked SO question of my previous comment above 😊 – Constanceconstancia
H
1

In general, you'll want to use the multiplatform plugin. If you're building a klib separately, you're creating some extra steps (probably). In Link #2 it says that platform plugin is deprecated. Konan is the name of the native platform/compiler. There was a separate plugin for that last year, but you definitely don't want to be using that.

I just created an example but it's not public yet, so this is the best one I have off hand:

https://github.com/JetBrains/kotlin-native/blob/3329f74c27b683574ac181bc40e3836ceccce6c1/samples/tensorflow/build.gradle.kts#L12

I'm working on a Firestore library. The native and interop config live in the multiplatform config.

kotlin {

    android {
        publishAllLibraryVariants()
    }
//        iosArm64()
    iosX64("ios"){
        compilations["main"].cinterops {
            firebasecore {
                packageName 'cocoapods.FirebaseCore'
                defFile = file("$projectDir/src/iosMain/c_interop/FirebaseCore.def")
                includeDirs ("$projectDir/../iosApp/Pods/FirebaseCore/Firebase/Core/Public")
                compilerOpts ("-F$projectDir/src/iosMain/c_interop/modules/FirebaseCore-${versions.firebaseCoreIos}")
            }
            firestore {
                packageName 'cocoapods.FirebaseFirestore'
                defFile = file("$projectDir/src/iosMain/c_interop/FirebaseFirestore.def")
                includeDirs ("$projectDir/../iosApp/Pods/FirebaseFirestore/Firestore/Source/Public", "$projectDir/../iosApp/Pods/FirebaseCore/Firebase/Core/Public")
                compilerOpts ("-F$projectDir/src/iosMain/c_interop/modules/FirebaseFirestore-${versions.firebaseFirestoreIos}")
            }
        }
    }
}

The cinterops sets up where the def files are and params. I then publish that whole thing as a multiplatform library. The actual native artifact is a klib ultimately, but it's all managed with gradle and dependency metadata.

Hydroxylamine answered 23/7, 2019 at 20:54 Comment(1)
Ok, so I've copied the KotlinDSL code from the example on github, but I'm having an error importing org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget as it colours jetbrains red. How can I import that? – Obligate

© 2022 - 2024 β€” McMap. All rights reserved.