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

Uncovering the magic behind Android Builds (DroidFestival 2018)

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

5c1316478a9b5320d57dd851a258b5be?s=128

Armando Picón

October 20, 2018
Tweet

Transcript

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

    at Cornershop Inc. armando@cornershopapp.com @devpicon github.com/devpicon medium.com/devpicon
  2. Android Engineer at Cornershop Inc. Lifelong learning developer

  3. Some time ago…

  4. None
  5. None
  6. None
  7. Java interoperability

  8. How?

  9. Wait a minute! Let me give you some context first!

  10. Android builds

  11. Compilers Sources Libraries Resources Transforms Packaged Resources DX APK

  12. Compilers Sources Libraries Resources Transforms Packaged Resources DX APK

  13. javac Java source .java Transforms DX Dalvik bytecodes

  14. javac Java source .java Transforms DX Dalvik bytecodes Java bytecode

  15. javac Java source .java Transforms DX kotlinc Kotlin source .kt

    Dalvik bytecodes Java bytecode
  16. *JVM = Java Virtual Machine

  17. Java compatible bytecodes?

  18. bytecodes?

  19. https://en.wikipedia.org/wiki/Bytecode “Bytecode is a form of instruction set designed for

    efficient execution by a software interpreter.”
  20. https://en.m.wikipedia.org/wiki/Java_bytecode “Java bytecode is the instruction set of the Java

    Virtual Machine (JVM)”
  21. javac Java source .java Transforms DX kotlinc Kotlin source .kt

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

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

  24. Dalvik / Dex bytecodes?

  25. https://source.android.com/devices/tech/dalvik

  26. Java bytecodes != Dalvik bytecodes

  27. • 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
  28. // 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
  29. None
  30. javac Java source .java Transforms DX kotlinc Kotlin source .kt

    Dalvik / Dex bytecodes
  31. javac Java source .java Transforms DX kotlinc Kotlin source .kt

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

    Dalvik / Dex bytecodes Annotation Processing Desugaring Proguard rules
  33. 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
  34. javac Java source .java Transforms D8 kotlinc Kotlin source .kt

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

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

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

  38. R8 …Bye bye ProGuard??

  39. Summary

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

    at Cornershop Inc. armando@cornershopapp.com @devpicon github.com/devpicon medium.com/devpicon