Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Kotlin code get executed in an Android application, and how is it different from Java?

I am learning Kotlin and would like to understand how the compiled code gets executed, and how is it different from Java code execution with Android.

I also want to know why we write MainActivity::class.java (class.java) to reference class file.

Thank you!

like image 509
Ashish Tiwari Avatar asked Dec 03 '25 18:12

Ashish Tiwari


1 Answers

how is it different from Java code execution with Android

It isn't. Android doesn't execute Java code or Kotlin code. Instead there is the following chain:

  1. Java/Kotlin/Scala/etc. compiler takes Java/Kotlin/Scala code and emits JVM bytecode in .class files.

  2. Android Gradle plugin takes JVM bytecode, converts it to Dalvik bytecode (using dx program) and packs it into .apk.

  3. When the .apk file is installed on device, ART converts Dalvik bytecode it contains to machine code.

  4. It's that machine code (and/or Dalvik bytecode) which is executed. ART has no idea which language source code was in at step 1 and doesn't care.

I also want to know why we write MainActivity::class.java (class.java) to reference class file.

You don't. You write it to reference the Class object corresponding to the MainActivity class.

like image 155
Alexey Romanov Avatar answered Dec 06 '25 06:12

Alexey Romanov