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

Dexs, R8 and 3.3

Iñaki Villar
February 07, 2019

Dexs, R8 and 3.3

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

Iñaki Villar

February 07, 2019
Tweet

More Decks by Iñaki Villar

Other Decks in Technology

Transcript

  1. 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 ;
  2. 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
  3. CLASS A HEADER CONSTANT POOL CLASS FIELD ATTRIBUTES METHODS CLASS

    B HEADER CONSTANT POOL CLASS FIELD ATTRIBUTES METHODS
  4. 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
  5. 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
  6. 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
  7. D8

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

    3.1 A.DEX B.DEX DX MERGING CLASSES.DEX D8
  9. 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”) )
  10. 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;
  11. 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
  12. R8

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

    minification.InterfaceB -> ItfB: int functionFromIntToInt(int) -> bar PROGUARD R8
  14. SHRINK OBFUSCATE OPTIMIZE minification.InterfaceA -> ItfA: int functionFromIntToInt(int) -> foo

    minification.InterfaceB -> ItfB: int functionFromIntToInt(int) -> bar PROGUARD R8
  15. SHRINK OBFUSCATE OPTIMIZE minification.InterfaceA -> ItfA: int functionFromIntToInt(int) -> foo

    minification.InterfaceB -> ItfB: int functionFromIntToInt(int) -> bar PROGUARD R8
  16. 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
  17. SHRINK R8 ABSTRACT METHOD REMOVER ANNOTATION REMOVER HORIZONTAL CLASS MERGING

    VERTICAL CLASS MERGING ASSUME VALUES API OBFUSCATE OPTIMIZE
  18. SHRINK R8 OBFUSCATE OPTIMIZE IR INLINING STATICIZER PROCESSING MEMBER VALUE

    STRING OPTIMIZE DEVIRTUALI ZER JAVA 8 REWRITER LAMBDA REWRITER LAMBDA MERGER
  19. 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
  20. 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
  21. R8

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

    Modules + 1000 Classes R8 Disabled/Enabled
  23. Use Case: Multi Module Gradle 4.9 Android Plugin 3.3.0-alpha08 15

    Modules + 1000 Classes R8 Disabled/Enabled
  24. Use Case: Multi Module Gradle 4.9 Android Plugin 3.3.0-alpha08 15

    Modules + 1000 Classes R8 Disabled/Enabled
  25. 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