【问题标题】:How to add java library to kotlin native如何将 java 库添加到 kotlin 本机
【发布时间】:2019-04-09 16:17:14
【问题描述】:

所以我尝试使用 intellij 创建 kotlin/native 应用程序(我在项目创建中选择了模板 kotlin->kotlin/native)。它创建了示例 gradle hello world 项目。下载所有依赖项后编译为exe文件并正常运行。但现在我需要包含 som 库,但我不知道如何。一开始我只想包含任何 jar 库(例如 jackson-core)。这是 build.gradle 文件的样子:

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'])
}

在每个部分都没有帮助。我仍然无法从杰克逊包中导入任何东西,它也没有出现在想法的“外部库”部分。我还尝试指定 lib 的全名并使用其他东西而不是实现或编译 - 仍然没有结果。我在这里错过了什么?

【问题讨论】:

    标签: java kotlin kotlin-native


    【解决方案1】:

    Kotlin/Native 不支持 Java 库。您需要使用多平台库 (kotlinx.serialization) 或原生库,例如 libjson

    【讨论】:

    • 默认有哪些库可用?我只想获取当前时间。必须有一些默认库。
    • 现在有了获取当前时间的kotlinx-datetime github.com/Kotlin/kotlinx-datetime
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 1970-01-01
    • 2019-12-01
    相关资源
    最近更新 更多