Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin Code compiled to Jar to be used for Java Project?

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.

like image 633
spierce7 Avatar asked Aug 02 '15 00:08

spierce7


People also ask

Can I use Kotlin in Java project?

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.

Does Kotlin get compiled to Java?

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.

Can I use Java and Kotlin in same project?

If your question is can you use kotlin files in java files and vice versa then the answer is yes.

Is Kotlin compatible with Java?

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.

How do I convert a J2K file to Kotlin?

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.

How do I get the source code for Kotlin?

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.

What is Kotlin and why is it important for Android development?

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.


1 Answers

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.**

like image 62
Alexander Udalov Avatar answered Sep 26 '22 02:09

Alexander Udalov