André Sousa
André Sousa

Reputation: 1730

Kotlin bytecode in Android Apps

I'm a kotlin and Java developer, and recently I started analyzing the bytecode generated by kotlin. And I found out a lot of wrapper code and other stuff that the compiler generates in order to translate what I have coded in Kotlin to Java.

So, my question is:

Imagine that I have an app that its code is 100% written in kotlin. Dependencies and the main app. All Kotlin.

  1. Does this mean that a different compiler will be used in order to avoid Java compatible bytecode?
  2. Or is there any optimization done by the compiler in this kind of scenarios?

Many Thanks.


I know about Kotlin Native but I think it will only be applied to Android in the future.

Upvotes: 1

Views: 861

Answers (1)

TheWanderer
TheWanderer

Reputation: 17834

The only way you're going to avoid Java bytecode with Kotlin is to use Kotlin Native, and you won't be able to use the Android SDK in that case.

Kotlin JVM, as the name implies, compiles to JVM bytecode; it's one of the main draws of using it. If it compiled to something different, it would be Kotlin Native.

To answer your bullets:

  1. No, the same compiler is used whether or not you have Java source files.
  2. Probably not. Kotlin JVM is made to be almost completely interoperable with Java, and that's the same whether or not your project includes Java code.

Think about if you were creating an Android library in Kotlin. Would you really want it to automatically compile to something other than Java bytecode in that case? It wouldn't be able to be used in Java projects, defeating one of the main reasons Kotlin is so good as a Java alternative.

Also remember, you're using the Android SDK. Even if you have no dependencies in your build.gradle, you still reference the core SDK itself, which is Java. The SDK isn't included in your APK, but it's still used during compilation.

If you want something that avoids Java bytecode, use something like Flutter. It has its own SDK, and can bridge back to Java components. Of course, you can't completely avoid the JVM, because you still need some way for Android to install and open the app.

Upvotes: 5

Related Questions