Is it possible to translate Kotlin to Java without Kotlin libraries?


Is it possible to translate Kotlin to Java without Kotlin libraries?
Some environments can only run standard Java files (some acm online judgments for example). Is it possible using Kotlin in this kind case?

I tried using decompile Kotlin byte code, but most cases it imports Kotlin’s libraries (import kotlin.collections.CollectionsKt; for example). Can it be translated to pure Java?


You can’t avoid standard library. You will have to translate it as well. Of course you can try to avoid stdlib calls, but your program will be more like Java then.


The bigger problem is that it also includes various Kotlin intrinsics (such as for null checks and equality). As far as I know there is no codegen option to avoid this at all.


Whats is the problem?

CollectionsKt is as “CollectionsUtils”. If you see in the code, it is because you use them (extensions methods?).

If you don’t use any method of std, you will not get imports of kotlin library but you will not get all power of kotlin.


Unfortunately the compiler itself will generate (hidden) calls to some methods in the standard library (the part which is effectively the Kotlin runtime). For example it will call a method to do null-checks rather than repeating the code everywhere (this also makes optimization easier for the JIT). Similarly if you do a comparison between two objects Kotlin semantics allow either one to be null without throwing an exception. To do this, Kotlin invokes a helper function rather than invoking .equals directly. While it is possible to tell the compiler to omit the null checks, the other “intrinsics” provided by the standard library that are needed to support language semantics cannot be disabled. As such you will need a subset of the Kotlin standard library to run Kotlin code.