Kotlin Code compiled to Jar to be used for Java Pr

2020-07-05 18:56发布

问题:

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.

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;

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.

回答1:

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.

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

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