Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Uncovering the magic behind Android Builds (Dro...

Uncovering the magic behind Android Builds (DroidFestival 2018)

Do you know what happen when you push the Run button on your Android Studio? Do you know that Java Bytecodes are not the same as Dalvik Bytecodes?

This talk will cover the building process of your Android apps from a perspective of a Kotlin Developer.

http://droidfestival.com/schedule/#session-11

Armando Picón

October 20, 2018
Tweet

More Decks by Armando Picón

Other Decks in Programming

Transcript

  1. Uncovering the magic behind Android builds Armando Picón Android Engineer

    at Cornershop Inc. [email protected] @devpicon github.com/devpicon medium.com/devpicon
  2. javac Java source .java Transforms DX kotlinc Kotlin source .kt

    Dalvik / Dex bytecodes Java bytecode (.class)
  3. javac Java source .java Transforms DX kotlinc Kotlin source .kt

    Dalvik / Dex bytecodes Java bytecode (.class)
  4. DX

  5. • Stack-based machine • JVM bytecodes is composed by one

    or more .class file (each of this contains only one Java class) • JVM runs in almost every platform (except Android) • Each class is dynamically loaded depending of processing requirements • Register-based • Dalvik bytecode is only composed of one .dex file (containing all the classes of application) • DVM (before) and ART (now) runs only in Android • Every classes in the same DEX file are loaded by the same class loader instance Dalvik Bytecode Java Bytecode 
 & Java Compatible Bytecode from Kotlin
  6. // access flags 0x11 public final main([Ljava/lang/String;)V @Lorg/jetbrains/annotations/NotNull;() // invisible,

    parameter 0 L0 ALOAD 1 LDC "args" INVOKESTATIC kotlin/jvm/internal/ Intrinsics.checkParameterIsNotNull (Ljava/lang/Object;Ljava/ lang/String;)V L1 LINENUMBER 4 L1 ALOAD 0 GETSTATIC Kotlin$main$1.INSTANCE : LKotlin$main$1; CHECKCAST kotlin/jvm/functions/Function1 INVOKESPECIAL Kotlin.load2 (Lkotlin/jvm/functions/ Function1;)V L2 LINENUMBER 5 L2 RETURN L3 LOCALVARIABLE this LKotlin; L0 L3 0 LOCALVARIABLE args [Ljava/lang/String; L0 L3 1 MAXSTACK = 2 MAXLOCALS = 2 # virtual methods .method public final main([Ljava/lang/String;)V .registers 3 .param p1, "args" # [Ljava/lang/String; .annotation build Lorg/jetbrains/annotations/NotNull; .end annotation .end param const-string v0, "args" invoke-static {p1, v0}, Lkotlin/jvm/internal/Intrinsics;- >checkParameterIsNotNull(Ljava/lang/Object;Ljava/lang/ String;)V .line 4 sget-object v0, LKotlin$main$1;->INSTANCE:LKotlin$main$1; check-cast v0, Lkotlin/jvm/functions/Function1; invoke-direct {p0, v0}, LKotlin;->load2(Lkotlin/jvm/ functions/Function1;)V .line 5 return-void .end method Dalvik Bytecode Kotlin Bytecode
  7. javac Java source .java Transforms DX kotlinc Kotlin source .kt

    Dalvik / Dex bytecodes Annotation Processing Desugaring Proguard rules
  8. javac Java source .java Transforms D8 kotlinc Kotlin source .kt

    Dalvik / Dex bytecodes Annotation Processing Desugaring Proguard rules
  9. Desugaring • New java language features require new byte codes

    & new API • Older devices don’t support these • Developers don’t want to be stuck • Desugar replace new byte codes & language API with older ones
  10. javac Java source .java Transforms D8 kotlinc Kotlin source .kt

    Dalvik / Dex bytecodes Annotation Processing Desugaring Proguard rules
  11. javac Java source .java Transforms D8 kotlinc Kotlin source .kt

    Dalvik / Dex bytecodes Annotation Processing Desugaring Proguard rules
  12. javac Java source .java Transforms R8 kotlinc Kotlin source .kt

    Dalvik / Dex bytecodes Annotation Processing Desugaring Proguard rules
  13. R8

  14. Uncovering the magic behind Android builds Armando Picón Android Engineer

    at Cornershop Inc. [email protected] @devpicon github.com/devpicon medium.com/devpicon