Dexs, R8 and 3.3

32018c62accb6edf57251e04308d4216?s=47 Iñaki Villar
February 07, 2019

Dexs, R8 and 3.3

DroidKaigi, Tokyo 2019
MobileRefresh, Wellington 2018
AppsConf, Moscow 2018

32018c62accb6edf57251e04308d4216?s=128

Iñaki Villar

February 07, 2019
Tweet

Transcript

  1. 3.
  2. 4.
  3. 16.
  4. 17.
  5. 18.
  6. 19.
  7. 21.

    CLASS DEX APK JVM DALVIK-ART STACK BASED REGISTER BASED 1.

    POP 20 2. POP 7 3. ADD 20, 7, result 4. PUSH result ADD R1, R2, R3 ;
  8. 24.

    CLASS DEX APK fun hello(): String { return "Hello" }

    http://mariokmk.github.io/programming/2015/03/06/learning-android-bytecode.html https://source.android.com/devices/tech/dalvik/dalvik-bytecode
  9. 26.

    CLASS A HEADER CONSTANT POOL CLASS FIELD ATTRIBUTES METHODS CLASS

    B HEADER CONSTANT POOL CLASS FIELD ATTRIBUTES METHODS
  10. 27.

    CLASS A HEADER CONSTANT POOL CLASS FIELD ATTRIBUTES METHODS CLASS

    B HEADER CONSTANT POOL CLASS FIELD ATTRIBUTES METHODS HEADER String_ids Type_ids Proto_ids Methods Fields Classes Data DEX
  11. 28.

    CLASS A HEADER CONSTANT POOL CLASS FIELD ATTRIBUTES METHODS CLASS

    B HEADER CONSTANT POOL CLASS FIELD ATTRIBUTES METHODS HEADER String_ids Type_ids Proto_ids Methods Fields Classes Data DEX
  12. 29.

    CLASS A HEADER CONSTANT POOL CLASS FIELD ATTRIBUTES METHODS CLASS

    B HEADER CONSTANT POOL CLASS FIELD ATTRIBUTES METHODS HEADER String_ids Type_ids Proto_ids Methods Fields Classes Data DEX
  13. 41.
  14. 42.
  15. 43.
  16. 44.
  17. 45.

    D8

  18. 56.
  19. 57.
  20. 58.

    A.JAVA JAVAC A.CLASS DX B.JAVA B.CLASS Android Gradle Plugin >=

    3.1 A.DEX B.DEX DX MERGING CLASSES.DEX D8
  21. 60.

    Desugaring A.JAVA JAVAC A.CLASS 8 DX CLASSES.DEX DESUGARING A.CLASS 6-7

    .subscribe( this::handleSupportFeatureGet, error -> log.e(error, "Unable to updateSupportFeature”) )
  22. 72.
  23. 73.
  24. 76.

    Processing 'classes.dex'... Opened 'classes.dex', DEX version '035' Class #0 -

    Class descriptor : 'Lcom/example/myapplication/MainActivity;' Access flags : 0x0011 (PUBLIC FINAL) Superclass : 'Landroid/support/v7/app/AppCompatActivity;' Interfaces - Static fields - Direct methods - #0 : (in Lcom/example/myapplication/MainActivity;) name : '<init>' type : '()V' access : 0x10001 (PUBLIC CONSTRUCTOR) registers : 1 ins : 1 outs : 1 insns size : 4 16-bit code units positions : 0x0000 line=6 locals : 0x0000 - 0x0004 reg=0 this Lcom/example/myapplication/MainActivity; Virtual methods - #0 : (in Lcom/example/myapplication/MainActivity;) name : 'onCreate' type : '(Landroid/os/Bundle;)V' access : 0x0004 (PROTECTED) registers : 3 ins : 2 outs : 2 insns size : 10 16-bit code units positions : 0x0000 line=9 0x0003 line=10 0x0009 line=11 locals : 0x0000 - 0x000a reg=1 this Lcom/example/myapplication/MainActivity; 0x0000 - 0x000a reg=2 savedInstanceState Landroid/os/Bundle;
  25. 79.

    3.1.X 3.2.X 3.3.X 3.0.X ENABLED ENABLED ENABLED DISABLED 1st Phase:

    D8/DX - 6 months approx 2nd Phase: D8/DX(deprecated) - 1 year approx 3rd Phase: DX Removed
  26. 80.

    R8

  27. 81.
  28. 86.
  29. 91.

    SHRINK OBFUSCATE OPTIMIZE minification.InterfaceA -> ItfA: int functionFromIntToInt(int) -> foo

    minification.InterfaceB -> ItfB: int functionFromIntToInt(int) -> bar PROGUARD R8
  30. 92.

    SHRINK OBFUSCATE OPTIMIZE minification.InterfaceA -> ItfA: int functionFromIntToInt(int) -> foo

    minification.InterfaceB -> ItfB: int functionFromIntToInt(int) -> bar PROGUARD R8
  31. 93.

    SHRINK OBFUSCATE OPTIMIZE minification.InterfaceA -> ItfA: int functionFromIntToInt(int) -> foo

    minification.InterfaceB -> ItfB: int functionFromIntToInt(int) -> bar PROGUARD R8
  32. 95.

    SHRINK OBFUSCATE OPTIMIZE PROGUARD R8 PROGUARD & R8 Remove unused

    classes/fields/methods Inline constants Propagate constants Remove unused code Propagate constant fields Remove write-only fields Simplify basic container classes Merge interfaces with single implementations Propagate constant return values Inline methods Outline common code into new methods PROGUARD Propagate constant arguments Simplify plain enum types Merge classes Make methods private Make methods static Desynchronize methods Simplify tail recursion Remove logging code Merge code R8 Simplify basic container classes Outline common code into new methods Merge Kotlin lambda constructs Optimize Kotlin lambda constructs https://www.guardsquare.com/en/blog/proguard-and-r8
  33. 97.

    SHRINK R8 ABSTRACT METHOD REMOVER ANNOTATION REMOVER HORIZONTAL CLASS MERGING

    VERTICAL CLASS MERGING ASSUME VALUES API OBFUSCATE OPTIMIZE
  34. 100.

    SHRINK R8 OBFUSCATE OPTIMIZE IR INLINING STATICIZER PROCESSING MEMBER VALUE

    STRING OPTIMIZE DEVIRTUALI ZER JAVA 8 REWRITER LAMBDA REWRITER LAMBDA MERGER
  35. 101.

    SHRINK R8 OBFUSCATE OPTIMIZE IR REWRITE LONG COMPARE & REQUIRE

    NON NULL SIMPLIFY ARRAY CONSTRUCTOR SPLIT RANGE INVOKE CONSTANTS SIMPLIFY IF REWRITE SWITCH LOG ARGUMENT TYPES COLLECT CLASS INIT DEFAULTS REWRITE THROWABLE ADD AND GET SUPRESS IDENTIFY RETURN ARGUMENTS NUMBER CONVERSION REGISTER ALLOCATION INLINING STATICIZER PROCESSING MEMBER VALUE STRING OPTIMIZE DEVIRTUALI ZER JAVA 8 REWRITER LAMBDA REWRITER LAMBDA MERGER
  36. 102.

    SHRINK R8 OBFUSCATE OPTIMIZE IR REWRITE LONG COMPARE & REQUIRE

    NON NULL SIMPLIFY ARRAY CONSTRUCTOR SPLIT RANGE INVOKE CONSTANTS SIMPLIFY IF REWRITE SWITCH LOG ARGUMENT TYPES COLLECT CLASS INIT DEFAULTS REWRITE THROWABLE ADD AND GET SUPRESS IDENTIFY RETURN ARGUMENTS NUMBER CONVERSION REGISTER ALLOCATION INLINING STATICIZER PROCESSING MEMBER VALUE STRING OPTIMIZE DEVIRTUALI ZER JAVA 8 REWRITER LAMBDA REWRITER LAMBDA MERGER
  37. 107.

    R8

  38. 112.

    Use Case: Multi Module Gradle 4.9 Android Plugin 3.3.0-alpha08 15

    Modules + 1000 Classes R8 Disabled/Enabled
  39. 113.

    Use Case: Multi Module Gradle 4.9 Android Plugin 3.3.0-alpha08 15

    Modules + 1000 Classes R8 Disabled/Enabled
  40. 114.

    Use Case: Multi Module Gradle 4.9 Android Plugin 3.3.0-alpha08 15

    Modules + 1000 Classes R8 Disabled/Enabled
  41. 120.

    Test $ git clone https://r8.googlesource.com/r8 $ tools/gradle.py d8 r8 $

    java -jar d8.jar —debug —output out HelloWorld.class $ java -jar r8.jar —output out --pg-conf proguard.cfg classes
  42. 121.