Kotlin代码已编译到Jar中以用于Java Project? [英] Kotlin Code compiled to Jar to be used for Java Project?

查看:436
本文介绍了Kotlin代码已编译到Jar中以用于Java Project?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Kotlin中编写了一个Java库,并且希望将其编译为Java和Kotlin应用程序可以使用的jar.

I've written a Java library in Kotlin, and I'd like to compile it to a jar that is usable by Java and Kotlin applications.

这个想法是jar应该能够在Java项目和Kotlin项目上工作.做一个简单的./gradlew clean assemble会生成一个jar,当我检查jar时,我可以看到jar中没有包含的几种导入,普通的Java应用程序也无法访问:

The idea is that the jar should be able to work on Java projects, and Kotlin projects. Doing a simple ./gradlew clean assemble generates a jar, and when I inspect the jar I can see several imports that aren't included in the jar that normal Java applications won't have access too:

import jet.runtime.typeinfo.JetValueParameter;
import kotlin.jvm.internal.Intrinsics;
import kotlin.jvm.internal.KotlinClass;
import kotlin.jvm.internal.KotlinClass.Kind;
import kotlin.jvm.internal.KotlinSyntheticClass;
import kotlin.jvm.internal.KotlinSyntheticClass.Kind;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

如何将上述依赖项包含在jar中?现在,该罐子只有30kb.我希望也不必包括整个Kotlin运行时,而只需在使用的组件中进行编译.

How could I include the above dependencies in the jar? Right now the jar is only 30kb. I'd prefer to not have to include the entire Kotlin runtime either, but only compile in the used components.

推荐答案

Kotlin二进制文件确实在运行时使用了一些类,但是这些类的确切集合是未指定的,甚至在次要版本之间也可能会发生变化,因此您不应该依赖在您提供的列表中.最好的选择是在-include-runtime选项中包含完整的Kotlin运行时,然后运行ProGuard(如注释中所述)以排除未使用的类.

Kotlin binaries do use some classes from the runtime, but the exact set of those classes is unspecified and may change even between minor versions, so you shouldn't rely on the list you've provided. Your best option is to include the full Kotlin runtime with the -include-runtime option and then run ProGuard (as noted in the comments) to exclude unused classes.

您可以使用以下最小的ProGuard配置文件:

You can use this minimal ProGuard config file:

-injars input.jar
-outjars output.jar
-libraryjars <java.home>/lib/rt.jar

-keep class org.jetbrains.annotations.** {
    public protected *;
}

-keep class kotlin.jvm.internal.** {
    public protected *;
}

-keepattributes Signature,InnerClasses,EnclosingMethod

-dontoptimize
-dontobfuscate

如果使用反射(kotlin-reflect.jar),则还应将所有内容都保留在kotlin.reflect.**

If you use reflection (kotlin-reflect.jar), you should also keep everything under kotlin.reflect.**

这篇关于Kotlin代码已编译到Jar中以用于Java Project?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆