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

Uncovering the magic behind Android Builds (Dev...

Uncovering the magic behind Android Builds (DevFest Lima 2018)

Durante años el desarrollo de Android se ha venido apoyando en el uso de herramientas que nos ahorran la necesidad de ver el proceso de generación de APKs de forma cruda y pura. En esta charla hablaremos sobre este proceso desde el punto de vista de un Kotlin Developer y qué utilidad tiene conocer sobre ello.

https://devfestlima.com/schedule/2018-10-26?sessionId=324

Armando Picón

October 26, 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
  8. javac Java source .java Transforms DX kotlinc Kotlin source .kt

    Dalvik / Dex bytecodes Annotation Processing
  9. javac Java source .java Transforms DX kotlinc Kotlin source .kt

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

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

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

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

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

    Dalvik / Dex bytecodes Desugaring Proguard rules
  16. R8

  17. R8 • New Shrinker & Optimizer • Faster compilation •

    Smaller code • Better diagnostics • ProGuard Compatible
  18. • Best practices using compilers in Android Studio
 https://www.youtube.com/watch?v=gGOOkk2y_Ss&t=644s •

    What’s new in Android Runtime?
 https://www.youtube.com/watch?v=Yi9-BqUxsno&t=446s
  19. Uncovering the magic behind Android builds Armando Picón Android Engineer

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