Slide 1

Slide 1 text

Relevance of code minification in a performance world Jitin Sharma GDE Android, Gojek Android Worldwide jitinsharma.com @_jitinsharma

Slide 2

Slide 2 text

What we’ll discuss • Code obfuscation and minification • Introduction to obfuscation tools • Proguard rules • Performance impacts of code minification • Common mistakes and how to avoid them

Slide 3

Slide 3 text

Obfuscation

Slide 4

Slide 4 text

Life of code Kotlin Class dex Source code Bytecode Bytecode

Slide 5

Slide 5 text

Life of code Kotlin Class dex Source code Bytecode resources + apk

Slide 6

Slide 6 text

Life of code Kotlin Class dex Source code Bytecode Bytecode • dedexer • smali

Slide 7

Slide 7 text

Life of code Kotlin Class dex Source code Bytecode Bytecode jadx

Slide 8

Slide 8 text

Obfuscation class Repository viewModel.fetchData() fun matchUser() val loggedIn = false package com.awesome.app class a c.zz() fun rt() val y = false package com.g.f

Slide 9

Slide 9 text

minification

Slide 10

Slide 10 text

Minification fun main() { fetchUser() } fun fetchUser() { fetchProfile() } fun fetchProfile() { displayProfile() } fun displayProfile() { .. } main() fetchUser() fetchProfile() displayProfile()

Slide 11

Slide 11 text

Minification fun main() { fetchUser() } fun fetchUser() { // fetchProfile() } fun fetchProfile() { displayProfile() } fun displayProfile() { .. } main() fetchUser() fetchProfile() displayProfile()

Slide 12

Slide 12 text

Minification fun main() { fetchUser() } fun fetchUser() { // fetchProfile() } fun fetchProfile() { displayProfile() } fun displayProfile() { .. } main() fetchUser() fetchProfile() displayProfile()

Slide 13

Slide 13 text

Minification Activities Service Application Receivers Code

Slide 14

Slide 14 text

Minification Activities Service Application Receivers Code resources

Slide 15

Slide 15 text

Tools

Slide 16

Slide 16 text

Proguard Dexguard R8

Slide 17

Slide 17 text

Android Toolchain Code (source code + libraries) Obfuscation + minification D8/R8 minified dex AGP Toolchain (removes unused resources) manifest, resources apk dex/IR

Slide 18

Slide 18 text

Android Toolchain Code (source code + libraries) dex/IR minified dex AGP Toolchain (removes unused resources) manifest, resources ResourceUsageAnalyzer.java apk Obfuscation + minification D8/R8

Slide 19

Slide 19 text

Rules

Slide 20

Slide 20 text

Rules fun main() { val clazz = Class.forName() val method = clazz.getDeclaredMethod() method.invoke() } main() method()

Slide 21

Slide 21 text

Rules fun main() { val clazz = Class.forName() val method = clazz.getMethod() method.invoke() } main() method() Classes Fields Interfaces Functions

Slide 22

Slide 22 text

Rules - keep class … - keepclassmembers … - keep attributes … - dontwarn …

Slide 23

Slide 23 text

Optimisation and Performance

Slide 24

Slide 24 text

Test bed project ~20 modules, 100 classes, 6000 methods androidx Rxjava Glide Kotlin AGP 4.1 D8/R8

Slide 25

Slide 25 text

Comparison App Type Size App Launch Mode Non Minified 11.8MB 596.6ms Release Minified 6.5MB 367.9ms Release Minified + Shrinked 6.4MB 361.3ms Release

Slide 26

Slide 26 text

11.8 mb 596.6 ms 6.4 mb (45%) 361.3 ms (39%) Non Minified Minified

Slide 27

Slide 27 text

Optimisations

Slide 28

Slide 28 text

Code Inlining fun myBranchedFunction() { if (BuildConfig.DEBUG) { debugMethod() } else { releaseMethod() } } fun myBranchedFunction() { releaseMethod() }

Slide 29

Slide 29 text

Code Inlining fun myBranchedFunction() { releaseMethod() } fun main() { myBranchedFunction() myFunction2() }

Slide 30

Slide 30 text

Code Inlining fun myBranchedFunction() { releaseMethod() } fun main() { releaseMethod() myFunction2() }

Slide 31

Slide 31 text

Code Inlining if (Build.VERSION.SDK_INT >= 21) { window.statusBarColor = .. } if (Build.VERSION.SDK_INT >= 28) { val displayCutOut = decorView.rootWindowInsets.displayCutout ... } minSdk 19 if (Build.VERSION.SDK_INT >= 21) { window.statusBarColor = .. } if (Build.VERSION.SDK_INT >= 28) { val displayCutOut = decorView.rootWindowInsets.displayCutout ... }

Slide 32

Slide 32 text

Code Inlining if (Build.VERSION.SDK_INT >= 21) { window.statusBarColor = .. } if (Build.VERSION.SDK_INT >= 28) { val displayCutOut = decorView.rootWindowInsets.displayCutout ... } minSdk 21 window.statusBarColor = .. if (Build.VERSION.SDK_INT >= 28) { val displayCutOut = decorView.rootWindowInsets.displayCutout ... }

Slide 33

Slide 33 text

Code Inlining if (Build.VERSION.SDK_INT >= 21) { window.statusBarColor = .. } if (Build.VERSION.SDK_INT >= 28) { val displayCutOut = decorView.rootWindowInsets.displayCutout ... } minSdk 28 window.statusBarColor = .. val displayCutOut = decorView.rootWindowInsets.displayCutout ...

Slide 34

Slide 34 text

Code Inlining fun trim(String name) { Intrinsics.checkParameterIsNotNull(name) name(…) } fun trim(String name) { if (name != null) { name(…) } else { // exception } }

Slide 35

Slide 35 text

App Startup 596.6 ms 361.3 ms (39%) Non Minified Minified

Slide 36

Slide 36 text

App Startup Non Minified Minified

Slide 37

Slide 37 text

App Startup Non Minified Minified Dex load: 2.96 ms Dex load: 0.07 ms

Slide 38

Slide 38 text

Systrace Event Non minified Minified Mode bindApplication 54.46 ms 19.24 ms Release ResourcesManager#getRes ources 4.62 ms 3.97 ms Release ResourcesImpl#updateCon figuration 3.98 ms 2.62 ms Release LoadApkAssets 1.3 ms 0.68 ms Release VectorDrawable#inflate 0.41 ms 0.39 ms Release

Slide 39

Slide 39 text

Rules

Slide 40

Slide 40 text

Rules can hurt you - keep class com.mypackage.** - keep com.somepackage.R$* - keepclassmembers enum * { public static **[] values(); public static ** valueOf(java.lang.String); }

Slide 41

Slide 41 text

Case Study keepclassmembers class **.R$* { public static ; }

Slide 42

Slide 42 text

Transitive R classes App Module1 Module2 Module3 Module4 Module5 aar aar aar aar

Slide 43

Slide 43 text

Transitive R classes App Module1 Module2 Module3 Module4 Module5 aar aar aar aar R.class R.class R.class R.class R.class R.class R.class R.class R.class

Slide 44

Slide 44 text

11.8 mb 596.6 ms 6.4 mb (45%) 361.3 ms (39%) Non Minified Minified

Slide 45

Slide 45 text

6.4 mb 361.3 ms Minified 6.6 mb (+3%) 487.1 ms (+34%) Minified with faulty rules

Slide 46

Slide 46 text

6.4 mb 361.3 ms Minified 6.6 mb (+3%) 487.1 ms (+34%) Minified with faulty rules Dex Count: 1 Dex Size: 634KB Dex load: 0.07ms Dex Count: 2 Dex Size: 894KB Dex load: 0.09ms

Slide 47

Slide 47 text

Rules • Be aggressive instead of defensive when defining proguard rules. • Avoid wildcard package rules. • Use @Keep to attach rules to codebase instead of rules files. • Scan for rules which third party rules are importing in your codebase.

Slide 48

Slide 48 text

Imported Rules proguard-rules.pro -printconfiguration

Slide 49

Slide 49 text

Imported Rules proguard-rules.pro -printconfiguration build/outputs/mapping/release/configuration.txt

Slide 50

Slide 50 text

Further Optimisations proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' proguardFiles getDefaultProguardFile(‘proguard-android-optimize.txt’), 'proguard-rules.pro'

Slide 51

Slide 51 text

Further Optimisations gradle.properties android.enableR8.fullMode=true

Slide 52

Slide 52 text

Debugging Rules -addconfigurationdebugging

Slide 53

Slide 53 text

Proguard Playground https://playground.proguard.com/

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

Further Reading • https://developer.android.com/studio/build/shrink-code • https://www.zacsweers.dev/android-proguard-rules/ • https://jakewharton.com/blog (Search for R8 optimization)

Slide 56

Slide 56 text

Thanks @_jitinsharma