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

Journey of APK from Compile to Start

Journey of APK from Compile to Start

This was the keynote presentation at Droidkaigi, 2019 in Tokyo, Japan.

Amanjeet Singh

February 08, 2019
Tweet

More Decks by Amanjeet Singh

Other Decks in Technology

Transcript

  1. AAPT •Android Asset Packaging tool. •Creates R.java file. •Tool for

    creating, updating and viewing components the zip archive.
  2. AAPT •Android Asset Packaging tool. •Creates R.java file. •Tool for

    creating, updating and viewing components the zip archive.
  3. Category •IDs are generated by AAPT to refer resources. •Resource

    bit: How R is prepared? P P T T N N N N 0x7f Next hex available
  4. public static final class id { public static final int

    ALT=0x7f070000; public static final int CTRL=0x7f070001; public static final int FUNCTION=0x7f070002; public static final int META=0x7f070003; public static final int SHIFT=0x7f070004; public static final int SYM=0x7f070005; public static final int action_bar=0x7f070006; public static final int action_bar_activity_content=0x7f070007; public static final int action_bar_container=0x7f070008; }
  5. public static final class id { public static final int

    ALT=0x7f070000; public static final int CTRL=0x7f070001; public static final int FUNCTION=0x7f070002; public static final int META=0x7f070003; public static final int SHIFT=0x7f070004; public static final int SYM=0x7f070005; public static final int action_bar=0x7f070006; public static final int action_bar_activity_content=0x7f070007; public static final int action_bar_constraintLayout=0x7f070008; public static final int action_bar_container=0x7f070009; } PP TT NNNN
  6. Java Java class Java . . . . javac Java

    javac javac javac class class class dx or d8 dex Proguard
  7. Java Java class Java . . . . javac Java

    javac javac javac class class class dx or d8 dex Proguard
  8. Java Java class Java . . . . javac Java

    javac javac javac class class class dx or d8 dex Proguard
  9. Java Java class Java . . . . javac Java

    javac javac javac class class class dx or d8 dex Proguard
  10. Java Java class Java . . . . javac Java

    javac javac javac class class class dex Proguard Optimization 
 layers dx or d8
  11. public static long sumArray(int[] arr){ long sum = 0; for(int

    i: arr){ sum += i; } return sum; } public static long sumArray(int[]); Code: 0: lconst_0 1: lstore_1 2: aload_0 3: astore_3 4: aload_3 5: arraylength 6: istore 4 8: iconst_0 9: istore 5 11: iload 5 13: iload 4 15: if_icmpge 36 18: aload_3 19: iload 5 21: iaload 22: istore 6 24: lload_1 25: iload 6 27: i2l 28: ladd 29: lstore_1 30: iinc 5, 1 33: goto 11 36: lload_1 37: lreturn .class
  12. public static long sumArray(int[] arr){ long sum = 0; for(int

    i: arr){ sum += i; } return sum; } public static long sumArray(int[]); Code: 0: lconst_0 1: lstore_1 2: aload_0 3: astore_3 4: aload_3 5: arraylength 6: istore 4 8: iconst_0 9: istore 5 11: iload 5 13: iload 4 15: if_icmpge 36 18: aload_3 19: iload 5 21: iaload 22: istore 6 24: lload_1 25: iload 6 27: i2l 28: ladd 29: lstore_1 30: iinc 5, 1 33: goto 11 36: lload_1 37: lreturn .class 11: iload 5 13: iload 4 15: if_icmpge 36 18: aload_3 19: iload 5 21: iaload 22: istore 6 24: lload_1 25: iload 6 27: i2l 28: ladd 29: lstore_1 30: iinc 5, 1 33: goto 11 Dispatches 14 Read 20 Write 16
  13. public static long sumArray(int[] arr){ long sum = 0; for(int

    i: arr){ sum += i; } return sum; } 0000: const-wide/16 v2, #int 0 // #0 0002: array-length v1, v6 0003: const/4 v0, #int 0 // #0 0004: if-ge v0, v1, 000d // +0009 0006: aget v4, v6, v0 0008: int-to-long v4, v4 0009: add-long/2addr v2, v4 000a: add-int/lit8 v0, v0, #int 1 // #01 000c: goto 0004 // -0008 000d: return-wide v2 .dex:
  14. public static long sumArray(int[] arr){ long sum = 0; for(int

    i: arr){ sum += i; } return sum; } 0000: const-wide/16 v2, #int 0 // #0 0002: array-length v1, v6 0003: const/4 v0, #int 0 // #0 0004: if-ge v0, v1, 000d // +0009 0006: aget v4, v6, v0 0008: int-to-long v4, v4 0009: add-long/2addr v2, v4 000a: add-int/lit8 v0, v0, #int 1 // #01 000c: goto 0004 // -0008 000d: return-wide v2 .dex: 0004: if-ge v0, v1, 000d // +0009 0006: aget v4, v6, v0 0008: int-to-long v4, v4 0009: add-long/2addr v2, v4 000a: add-int/lit8 v0, v0, #int 1 // #01 000c: goto 0004 // -0008 Dispatches 6 Reads 10 Writes 6
  15. Dalvik Upto KitKat •Byte Code Verification To check the integrity

    of Dex Done at install time rather than run time.
  16. Dalvik Upto KitKat •Byte Code Verification To check the integrity

    of Dex Done at install time rather than run time. Validity of indices and structure of file.
  17. Dalvik Upto KitKat •Byte Code Verification To check the integrity

    of Dex Done at install time rather than run time. Validity of indices and structure of file. Ordering of the instructions.
  18. Dalvik Upto KitKat •Optimizations VTable Method Index for method invocation.

    Replacing high volume of methods with their inline versions.
  19. Dalvik Upto KitKat •Optimizations VTable Method Index for method invocation.

    Replacing high volume of methods with their inline versions. Prune empty methods.
  20. Dalvik Upto KitKat •Optimizations VTable Method Index for method invocation.

    Replacing high volume of methods with their inline versions. Prune empty methods. Pre computations.
  21. Dalvik Upto KitKat •Optimizations VTable Method Index for method invocation.

    Replacing high volume of methods with their inline versions. Prune empty methods. Pre computations. dexopt ODEX
  22. ART •Preloading boot.art boot.oat Compiler
 Driver ELF file •Load Compiler

    Dex Resolve classes Verify classes Init classes Compile classes ELF File
  23. dex file, oat file ART Execute
 method Is
 it compiled


    ? Is it
 JIT compiled Y Run code
 from .oat file N
  24. dex file, oat file ART Execute
 method Is
 it compiled


    ? Is it
 JIT compiled Y Run code
 from .oat file N Run code
 from jit cache Y
  25. dex file, oat file ART Execute
 method Is
 it compiled


    ? Is it
 JIT compiled Y Run code
 from .oat file N N Interpreter Run code
 from jit cache Y
  26. dex file, oat file ART Execute
 method Is
 it compiled


    ? Is it
 JIT compiled Y Run code
 from .oat file N N Interpreter Run code
 from jit cache Y Record Profiles info
  27. dex file, oat file ART Execute
 method Is
 it compiled


    ? Is it
 JIT compiled Y Run code
 from .oat file N N Interpreter Is
 it hot? Run code
 from jit cache Y Record Profiles info
  28. dex file, oat file ART Execute
 method Is
 it compiled


    ? Is it
 JIT compiled Y Run code
 from .oat file N N Interpreter Is
 it hot? JIT
 Cache Run code
 from jit cache Y Record Profiles info Y
  29. dex file, oat file ART Execute
 method Is
 it compiled


    ? Is it
 JIT compiled Y Run code
 from .oat file N N Interpreter Is
 it hot? JIT
 Cache Run code
 from jit cache Y Record Profiles info N Y
  30. dex file, oat file ART Execute
 method Is
 it compiled


    ? Is it
 JIT compiled Y Run code
 from .oat file N N Interpreter Is
 it hot? JIT
 Cache Run code
 from jit cache Y Record Profiles info N Y When Profile guided compilation 
 occurs?
  31. Optimizations 1. Constant Folding numberOfDays = 2 + 2 +

    previous; add-int/lit8 v0, v2, #int 4
  32. 2. Inlining fun len (string: String): Int{ return string.length }

    const-string v0, "string" invoke-static {v1, v0}, Lkotlin/jvm/internal/Intrinsics;.checkParameterIsNotNull invoke-virtual {v1}, Ljava/lang/String;.length move-result v0 return v0
  33. 2. Inlining fun len (string: String): Int{ return string.length }

    const-string v0, "string" invoke-static {v1, v0}, Lkotlin/jvm/internal/Intrinsics;.checkParameterIsNotNull invoke-virtual {v1}, Ljava/lang/String;.length move-result v0 return v0
  34. const-string v0, "string" if-nez v1, :continue invoke-static {v1}, throwParameterIsNullException :continue

    invoke-virtual {v1}, Ljava/lang/String;.length move-result v0 return v0
  35. 3. Bounds Check Elimination int sum = 0; for (int

    i = 0; i < size; i++) { sum += array[i]; }
  36. 3. Bounds Check Elimination int sum = 0; for (int

    i = 0; i < size; i++) { sum += array[i]; }
  37. 4. Strength Reduction 5. Dead Code Elimination 6. DexLayout 7.

    Class Hierarchy Analysis And much more…..
  38. MULTIDEX: THE AFTERMATH •Adverse effect on App Startup •Build time

    increases. •Strange errors during runtime •Strangers in Crashyltics..
  39. MULTIDEX: THE AFTERMATH •Adverse effect on App Startup •Build time

    increases. •Strange errors during runtime •Strangers in Crashyltics..
  40. MULTIDEX: THE AFTERMATH •Adverse effect on App Startup •Build time

    increases. •Strange errors during runtime •Strangers in Crashyltics..
  41. 10s

  42. Time for tips • Use lazy initializations. • Don’t use

    reflections at time of APP startup • Initialize all the SDKs in background • Load images asynchronously. • Avoid nesting layout