Kotlin code compiled as jar for Java projects?

I wrote a Java library in kotlin, and I want to compile it into a jar that can be used by Java and kotlin applications

The idea is that jars should work on Java projects, not kotlin projects Make a simple/ The gradlew clean combination generates a jar. When I check the jar, I can see several imports that are not included in the jar and will not be accessed by normal Java applications:

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 do I include the above dependencies in the jar? Now, the jar is only 30KB I prefer not to include the entire kotlin runtime, or I can use only the components already used

Solution

Kotlin binaries use some classes at runtime, but the exact collection of these classes is unspecified and may change even between minor versions, so you should not rely on the list you provide Your best choice is to use the - include runtime option to include the full kotlin runtime, and then run Proguard (as described in the comments) to exclude unused classes

You can use this minimal Proguard profile:

-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 in kotlin. Jar Under reflect

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>