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)

MG lolenstine
  • 919
  • 1
  • 11
  • 32
  • 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. –  Mar 25 '21 at 22:37
  • 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. – MG lolenstine Mar 25 '21 at 22:44
  • 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 –  Mar 25 '21 at 23:05
  • @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. – MG lolenstine Mar 25 '21 at 23:08
  • @Pixel did you get it working? – MG lolenstine Apr 02 '21 at 05:55
  • 1
    @IMlolenstine not yet. I've be made progress (see my post here https://stackoverflow.com/questions/66851318/kotlin-multiplatform-mobile-cant-find-klib-package) 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. –  Apr 02 '21 at 06:17
  • 1
    @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 –  Apr 07 '21 at 10:07

1 Answers1

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.

Kevin Galligan
  • 16,159
  • 5
  • 42
  • 62
  • 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? – MG lolenstine Jul 24 '19 at 07:48