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.
Android Studio provides full support for Kotlin, enabling you to add Kotlin files to your existing project and convert Java language code to Kotlin. You can then use all of Android Studio's existing tools with your Kotlin code, including autocomplete, lint checking, refactoring, debugging, and more.
The Kotlin compiler for JVM compiles Kotlin source files into Java class files. The command-line tools for Kotlin to JVM compilation are kotlinc and kotlinc-jvm . You can also use them for executing Kotlin script files.
If your question is can you use kotlin files in java files and vice versa then the answer is yes.
In fact, Kotlin code is fully compatible with Java code. Therefore, you can be able to use both the Java and Kotlin languages in a single project due to Interoperability. You can call Kotlin functions in Java as well as Java methods and variables in Kotlin code.
To use J2K on a file, click Convert Java File to Kotlin Filein its context menu or in the Codemenu of IntelliJ IDEA. While the converter is not fool-proof, it does a pretty decent job of converting most boilerplate code from Java to Kotlin. Some manual tweaking however is sometimes required.
You can get your source code by clicking on the "Download file" or "Copy to clipboard" buttons. Kotlin is a general-purpose programming language with type inference. This language is intended to be concise (Rough estimates indicate approximately a 40% cut in the number of lines of code) and 100% interoperable with the Java programming language.
As a matter of fact, Kotlin is an advanced programming language, which handles some modern features like Interoperability. In other words, two languages can communicate and exchange information with each other. As a result, calling Java codes from Kotlin would be a key factor in designing and implementing codes in Android development.
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.**
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With