Kotlin Native编译jar和框架

我正在为Android和iOS构建一个多平台库。我的gradle文件如下所示:

plugins {
    id 'org.jetbrains.kotlin.multiplatform' version '1.4.0'
}
repositories {
    mavenCentral()
}
group 'com.example'
version '0.0.1'

apply plugin: 'maven-publish'

kotlin {
    jvm()
    // This is for iPhone simulator
    // Switch here to iosArm64 (or iosArm32) to build library for iPhone device
    ios {
        binaries {
            framework()
        }
    }
    sourceSets {
        commonMain {
            dependencies {
                implementation kotlin('stdlib-common')
                implementation("com.ionspin.kotlin:bignum:0.2.2")
            }
        }
        commonTest {
            dependencies {
                implementation kotlin('test-common')
                implementation kotlin('test-annotations-common')
            }
        }
        jvmMain {
            dependencies {
                implementation("com.ionspin.kotlin:bignum:0.2.2")
            }
        }
        jvmTest {
            dependencies {
                implementation kotlin('test')
                implementation kotlin('test-junit')
            }
        }
        iosMain {
        }
        iosTest {
        }
    }
}

configurations {
    compileClasspath
}

我正在使用第三方库,而我这样使用它:

fun test(value: String): Int {
    return BigDecimal.parseString(value).toBigInteger().intValue()
}

问题是,当我构建.jar时,不包括bignum库,而当我在Android项目中使用lib时,出现异常ClassNotFoundException:未找到类“ com.ionspin.kotlin.bignum.decimal.BigDecimal ”。

有没有办法在Android的.jar和iOS的.framework中包含第三方库?

影子羊

虚拟机

因此,我发现生成可以像您期望的那样工作Fat JAR的唯一方法在应用Java插件KMP库中添加两个自定义gradle任务project:build.gradle.kts

plugins {
    [...]
    id("java")
}

[...]

kotlin {
    jvm {
        [...]
        compilations {
            val main = getByName("main")
            tasks {
                register<Copy>("unzip") {
                    group = "library"
                    val targetDir = File(buildDir, "3rd-libs")
                    project.delete(files(targetDir))
                    main.compileDependencyFiles.forEach {
                        println(it)
                        if (it.path.contains("com.")) {
                            from(zipTree(it))
                            into(targetDir)
                        }
                    }
                }
                register<Jar>("fatJar") {
                    group = "library"
                    manifest {
                        attributes["Implementation-Title"] = "Fat Jar"
                        attributes["Implementation-Version"] = archiveVersion
                    }
                    archiveBaseName.set("${project.name}-fat")
                    val thirdLibsDir = File(buildDir, "3rd-libs")
                    from(main.output.classesDirs, thirdLibsDir)
                    with(jar.get() as CopySpec)
                }
            }
            tasks.getByName("fatJar").dependsOn("unzip")
        }

    }
    [...]
}

然后,您必须启动fatJargradle任务,任务将生成一个.jar文件,其中包含从第3个库类中提取的相应jar存档。

您可以进一步自定义两个自定义gradle脚本,以更好地满足您的需求(此处我仅以com。软件包名称开头deps包含在内)。

在此处输入图片说明

然后,您app:build.gradle可以在Android应用程序文件中像以前一样使用它,也可以简单地使用它

implementation files('libs/KMLibraryTest001-fat-1.0-SNAPSHOT.jar')

的iOS

当您在标题中还要求提供iOS部分时(即使它是问题的第二主题,即使您是第二公民),也只需使用,api而不是将其implementation用于第三方库以及框架的导出选项。

ios() {
    binaries {
        framework() {
            transitiveExport = true // all libraries
            //export(project(":LibA")) // this library project in a trainsitive way
            //export("your 3rd party lib") // this 3rd party lib in a transitive way
        }
    }
}

您可以在此处找到完整的参考

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章