问题描述
我想在我的Kotlin/Multiplatform项目(通用级别)中使用C代码.通过本文:> https://theprogrammershangout.com/resources/kotlin/native/creating-c-bindings.md/我得到了.klib文件,现在我想将其添加到IntelliJ Kotlin/Multiplatform项目中.我试图将其添加到Gradle而不成功:
I want to use C code in my Kotlin/Multiplatform project (common level).By this article:https://theprogrammershangout.com/resources/kotlin/native/creating-c-bindings.md/I got my .klib file and now I want to add it to my IntelliJ Kotlin/Multiplatform project.I'm trying to add this to Gradle without success:
implementation files("/path/to/App.klib")
项目已成功构建,但导入的应用程序未解析.我想念什么?
The project is built successfully but the importing App is not resolved.What am I missing?
我已经在互联网上进行了搜索,没有发现任何有用的信息.
I already searched all over the internet and did not found anything helpful.
我的build.gradle:
My build.gradle:
buildscript {
repositories {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
}
}
plugins {
id 'org.jetbrains.kotlin.multiplatform' version '1.4.10'
id 'maven-publish'
}
apply plugin: 'com.android.library'
repositories {
mavenCentral()
google()
jcenter()
}
group 'com.texel.examples.kotlinnative'
version '0.0.1'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName '1.0'
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
}
buildTypes {
release {
minifyEnabled false
}
}
sourceSets {
main {
manifest.srcFile 'src/jvmMain/AndroidManifest.xml'
java.srcDirs = ['src/jvmMain/kotlin']
res.srcDirs = ['src/jvmMain/resources']
}
}
}
dependencies {
androidTestImplementation 'com.android.support.test:runner:1.0.2'
}
kotlin {
// This is for iPhone emulator
// Switch here to iosArm64 (or iosArm32) to build library for iPhone device
// iosX64("ios") {
// binaries {
// framework()
// }
// }
macosX64("maccos") {
binaries {
framework()
}
}
android("android") {
// you can also publish both "release" and "debug"
publishLibraryVariants("release")
}
sourceSets {
commonMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-common:1.4.10'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9-native-mt"
implementation files("/Users/user/Downloads/App.klib")
}
}
jvmMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
}
}
jsMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-js'
}
}
macosMain {
dependencies {
}
}
iosMain {
dependencies {
}
}
}
}
configurations {
compileClasspath
}
非常感谢.
推荐答案
以下是官方文档: C Interop ,以及为应用程序使用C Interop和libcurl(您可能希望查看 https://github.com/JetBrains/kotlin-native/tree/master/samples/libcurl 代替)
Here is the official documentation: C Interop and also an Using C Interop and libcurl for an App (you may prefer to look at the https://github.com/JetBrains/kotlin-native/tree/master/samples/libcurl instead)
我看到gradle的设置非常不同:
As I see the gradle setup is pretty different:
hostTarget.apply {
compilations["main"].cinterops {
val libcurl by creating {
when (preset) {
presets["macosX64"] -> includeDirs.headerFilterOnly("/opt/local/include", "/usr/local/include")
presets["linuxX64"] -> includeDirs.headerFilterOnly("/usr/include", "/usr/include/x86_64-linux-gnu")
presets["mingwX64"] -> includeDirs.headerFilterOnly(mingwPath.resolve("include"))
}
}
}
这篇关于如何在Kotlin/Multiplatform项目中使用.klib库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!