如何将Java库添加到Kotlin本机 [英] How to add java library to kotlin native
问题描述
因此,我尝试使用intellij创建kotlin/native应用程序(我在项目创建中选择了模板kotlin-> kotlin/native).它创建了示例gradle hello world项目.下载所有依赖项后,该文件将编译为exe文件并正常运行.但是现在我需要包括som库,但我不知道怎么做.首先,我只想包含任何jar库(例如jackson-core).这是build.gradle文件的样子:
So I tried to create kotlin/native application using intellij (I selected template kotlin->kotlin/native in project creation). It created sample gradle hello world project. Which after downloading all dependencies compiles to exe file and runs normally. But now I need to include som libraries and I can't figure out how. For beginning I just want to include any jar library (for example jackson-core). This is how build.gradle file looks like:
plugins {
id 'kotlin-multiplatform' version '1.3.0'
}
repositories {
mavenCentral()
}
kotlin {
targets {
// For ARM, preset should be changed to presets.iosArm32 or presets.iosArm64
// For Linux, preset should be changed to e.g. presets.linuxX64
// For MacOS, preset should be changed to e.g. presets.macosX64
fromPreset(presets.mingwX64, 'mingw')
configure([mingw]) {
// Comment to generate Kotlin/Native library (KLIB) instead of executable file:
compilations.main.outputKinds('EXECUTABLE')
// Change to specify fully qualified name of your application's entry point:
compilations.main.entryPoint = 'sample.main'
}
}
sourceSets {
// Note: To enable common source sets please comment out 'kotlin.import.noCommonSourceSets' property
// in gradle.properties file and re-import your project in IDE.
mingwMain {
}
mingwTest {
}
}
}
task runProgram {
def buildType = 'release' // Change to 'debug' to run application with debug symbols.
dependsOn "link${buildType.capitalize()}ExecutableMingw"
doLast {
def programFile = kotlin.targets.mingw.compilations.main.getBinary('EXECUTABLE', buildType)
exec {
executable programFile
args ''
}
}
}
我尝试添加
dependencies {
implementation fileTree(dir: 'lib', include: ['*.jar'])
}
和
dependencies {
compile fileTree(dir: 'lib', include: ['*.jar'])
}
在每个部分中
都无济于事.我仍然无法从jackson包中导入任何内容,并且它也没有出现在想法的外部库"部分中.我还尝试指定lib的全名,并使用其他名称代替实现或编译-仍然没有结果.我在这里想念什么?
in every section and it doesn't help. I still can't import anything from jackson package and it doesn't show up in "external libraries" section of idea. I also tried specifying full name of lib and using something else instead of implementation or compile - still no result. What am I missing here?
推荐答案
Kotlin/Native不支持Java库.您需要使用多平台库( kotlinx.serialization )或本机库,例如 libjson .
Kotlin/Native has no support for Java libraries. You need to use a multiplatform library (kotlinx.serialization) or a native library such as libjson.
这篇关于如何将Java库添加到Kotlin本机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!