SlideShare a Scribd company logo
1 of 58
Ansgar Lin - Android team of Oath: Yahoo TW
1
如何有效精簡ProGuard規則以達到
縮⼩小apk之⽬目的
2
Foot note
iOS/Android Engineer App開發
Frontend Engineer 前端⼯工程師
Backend Engineer 後端⼯工程師
DevOps Engineer 系統維運⼯工程師
AI Engineer / Data Engineer AI⼯工程師
Product Designer 產品設計
Product Manager 產品管理理
Paranoid Software Engineer 資安⼯工程師
E-COMMERCE
電⼦子商務
MEDIA
媒體
DATA
⼤大數據
SEARCH
搜尋引擎
Effective ProGuard keep rules for smaller applications
(Google I/O ’18)
• https://www.youtube.com/watch?v=x9T5EYE-QWQ
3
Foot note
About the topic
• A most popular open source tool in the Android world.
• Shrink apk by removing unused code at run-time.
• Obfuscate code to provide minimal protections against reverse
engineering
• Add “-keep class * extends ClassA” into ProGuard configuration,
you can tell ProGuard don’t touch any subclass of ClassA
4
What is ProGuard?
Foot note
5
What is ProGuard?
// In build.gradle
android {
buildTypes {
debug {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-project.txt'
}
}
}
6
What is ProGuard?
• A most popular open source tool in the Android world.
• Shrink apk by removing unused code at run-time.
• Obfuscate code to provide minimal protections against reverse
engineering
• Add “-keep class * extends ClassA” into ProGuard configuration,
you can tell ProGuard don’t touch any subclass of ClassA
7
What is ProGuard?
// In build.gradle
android {
buildTypes {
debug {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-project.txt'
}
}
}
8
What is ProGuard?
9
Following outline
Foot note
• Refinement steps & reasons
• Result
• Trouble shoot
• Q & A
10
Foot note
Step 1:
Remove duplicated rules
11
ProGuard Work Flow - Read Configuration
Foot note
12
ProGuard Work Flow - Read Configuration
Foot note
13
ProGuard Work Flow - Read Configuration
Foot note
14
ProGuard Work Flow - Read Configuration
Foot note
15
ProGuard Work Flow - Read Configuration
Foot note
16
ProGuard Work Flow - Read Configuration
Foot note
17
ProGuard Work Flow - Read Configuration
Foot note
18
Refine ProGuard Configuration
Foot note
19
ProGuard Work Flow - Read Configuration
Foot note
proguard-android、proguard-default和proguard-android-optimize
20
Foot note
Refine ProGuard Configuration
21
Foot note
Step 2:
Remove redundant rules
22
ProGuard Work Flow - Read Input
Foot note
23
ProGuard Work Flow - Read Input
Foot note
24
ProGuard Work Flow - Read Input
Foot note
25
ProGuard Work Flow - Read Input
Foot note
Won’t get ProGuard
= “-keep” on everything
26
Foot note
Refine ProGuard Configuration
27
Foot note
Step 3:
Use “-whyareyoukeeping”
28
Foot note
• “-keep class com.example.Blur”
—> “-whyareyoukeeping class com.example.Blur”
Refine ProGuard Configuration
com.example.Blur
is invoked by com.example.BlurView: doWorkA(int)
is invoked by com.example.BlurView: BlurView(android.content.Context)
is invoked by com.example.SelectActivity: onCreate(android.os.Bundle)
implements android.app.Activity: onCreate(android.os.Bundle)
is a library method.
29
Foot note
• “-keep class com.example.Blur”
—> “-whyareyoukeeping class com.example.Blur”
Refine ProGuard Configuration
com.example.Blur
is invoked by com.example.BlurView: doWorkA(int)
is invoked by com.example.BlurView: BlurView(android.content.Context)
is invoked by com.example.SelectActivity: onCreate(android.os.Bundle)
implements android.app.Activity: onCreate(android.os.Bundle)
is a library method.
30
Foot note
• “-keep class com.example.Blur”
—> “-whyareyoukeeping class com.example.Blur”
Refine ProGuard Configuration
com.example.Blur
is invoked by com.example.BlurView: doWorkA(int)
is invoked by com.example.BlurView: BlurView(android.content.Context)
is invoked by com.example.SelectActivity: onCreate(android.os.Bundle)
implements android.app.Activity: onCreate(android.os.Bundle)
is a library method.
31
Foot note
• “-keep class com.example.Blur”
—> “-whyareyoukeeping class com.example.Blur”
Refine ProGuard Configuration
com.example.Blur
is invoked by com.example.BlurView: doWorkA(int)
is invoked by com.example.BlurView: BlurView(android.content.Context)
is invoked by com.example.SelectActivity: onCreate(android.os.Bundle)
implements android.app.Activity: onCreate(android.os.Bundle)
is a library method.
• “-keep class com.example.Blur”
—> “-whyareyoukeeping class com.example.Blur”
32
Foot note
Refine ProGuard Configuration
com.example.Blur
is invoked by com.example.BlurView: doWorkA(int)
is invoked by com.example.BlurView: BlurView(android.content.Context)
is invoked by com.example.SelectActivity: onCreate(android.os.Bundle)
implements android.app.Activity: onCreate(android.os.Bundle)
is a library method.
• “-keep class com.example.Blur”
—> “-whyareyoukeeping class com.example.Blur”
33
Foot note
Refine ProGuard Configuration
com.example.Blur
is invoked by com.example.BlurView: doWorkA(int)
is invoked by com.example.BlurView: BlurView(android.content.Context)
is invoked by com.example.SelectActivity: onCreate(android.os.Bundle)
implements android.app.Activity: onCreate(android.os.Bundle)
is a library method.
• “-keep class com.example.Blur”
—> “-whyareyoukeeping class com.example.Blur”
34
Foot note
Refine ProGuard Configuration
com.example.Blur
is invoked by com.example.BlurView: doWorkA(int)
is invoked by com.example.BlurView: BlurView(android.content.Context)
is invoked by com.example.SelectActivity: onCreate(android.os.Bundle)
implements android.app.Activity: onCreate(android.os.Bundle)
is a library method.
35
Foot note
• message from `-whyareyoukeeping`
• is a library class/method/field.
• is not been kept
• nothing
• is kept by a directive in the configuration.
Refine ProGuard Configuration
36
Foot note
• message from `-whyareyoukeeping`
• is a library class/method/field.
• is not been kept
• nothing
• is kept by a directive in the configuration.
-keep class com.example.Blur
-whyareyoukeeping class com.example.Blur
Refine ProGuard Configuration
37
The Result
38
購物中⼼心 - APK size (M)
Foot note
0.9
5.52%
39
購物中⼼心 - Method count
Foot note
12932
11.30%
40
購物中⼼心 - Method count (com.yahoo.*)
Foot note
2294
41
購物中⼼心 - Method count (com.google.*)
Foot note
9407
42
Recap
43
Foot note
1. Remove duplicated rules
Refine ProGuard Configuration
44
Foot note
2. Remove redundant rules
Refine ProGuard Configuration
45
Foot note
3. Use “-whyareyoukeeping”
• “-keep class com.example.Blur”
—> “-whyareyoukeeping class com.example.Blur”
Refine ProGuard Configuration
46
Trouble shoot
47
What should be careful?
Foot note
• 3-rd Library implements with reflection.
• Glide: initialization
• Parsing data with field names
• Gson + Retrofit: JSON model
• Anything implementation with static name.
48
What should be careful?
Foot note
• 3-rd Library implements with reflection.
• Glide: initialization
• Parsing data with field names
• Gson + Retrofit: JSON model
• Anything implementation with static name.
49
Foot note
W/ProGuard: The class 'com.example.SampleActivity' is calling Class.forName to retrieve
the class 'com.exmaple.NoneExistClass', but the latter could not be found.
It may have been obfuscated or shrunk.
You should consider preserving the class with its original name,
with a setting like:
 
-keep class com.exmaple.NoneExistClass
W/System.err: java.lang.ClassNotFoundException: com.exmaple.NoneExistClass
at java.lang.Class.classForName(Native Method)
• Add “-addconfigurationdebugging”
What should be careful?
50
Foot note
W/ProGuard: The class 'com.example.SampleActivity' is calling Class.forName to retrieve
the class 'com.exmaple.NoneExistClass', but the latter could not be found.
It may have been obfuscated or shrunk.
You should consider preserving the class with its original name,
with a setting like:
 
-keep class com.exmaple.NoneExistClass
W/System.err: java.lang.ClassNotFoundException: com.exmaple.NoneExistClass
at java.lang.Class.classForName(Native Method)
• Add “-addconfigurationdebugging”
What should be careful?
51
Foot note
W/ProGuard: The class 'com.example.SampleActivity' is calling Class.forName to retrieve
the class 'com.exmaple.NoneExistClass', but the latter could not be found.
It may have been obfuscated or shrunk.
You should consider preserving the class with its original name,
with a setting like:
 
-keep class com.exmaple.NoneExistClass
W/System.err: java.lang.ClassNotFoundException: com.exmaple.NoneExistClass
at java.lang.Class.classForName(Native Method)
• Add “-addconfigurationdebugging”
Only use in debug version
What should be careful?
52
Foot note
• ClassNotFoundException
• NoSuchFieldException

• NoSuchMethodException

• RuntimeException

• UnsatisfiedLinkError

• IOException
• Add “-addconfigurationdebugging”
What should be careful?
53
Foot note
W/ProGuard: The class 'com.example.SampleActivity' is calling Class.forName to retrieve
the class 'com.exmaple.NoneExistClass', but the latter could not be found.
It may have been obfuscated or shrunk.
You should consider preserving the class with its original name,
with a setting like:
 
-keep class com.exmaple.NoneExistClass
W/System.err: java.lang.ClassNotFoundException: com.exmaple.NoneExistClass
at java.lang.Class.classForName(Native Method)
• Add “-addconfigurationdebugging”
What should be careful?
54
R8
55
R8
Foot note
• Powered by Google
• Default enable after Android Studio(3.3)
• Java bytecode(.class) —> Dalvik bytecode(.dex)
• ProGuard configuration is acceptable.
• Changes:
• “-addconfigurationdebugging” will be ignored by default.
• Error detection while compiling.
56
Your turn!
Foot note
• Start playing with ProGuard
57
Q & A
58
Reference
Foot note
• MCE 2014 - Eric Lafortune - ProGuard, Optimizer and
Obfuscator in the Android SDK
• Effective ProGuard keep rules for smaller applications
(Google I/O ’18)
• About ProGuard - Part 1

More Related Content

Similar to [MOPCON2018] Effectively shrink ProGuard rules for reducing APK size

ProGuard / DexGuard Tips and Tricks
ProGuard / DexGuard Tips and TricksProGuard / DexGuard Tips and Tricks
ProGuard / DexGuard Tips and Tricksnetomi
 
手把手教你如何串接 Log 到各種網路服務
手把手教你如何串接 Log 到各種網路服務手把手教你如何串接 Log 到各種網路服務
手把手教你如何串接 Log 到各種網路服務Mu Chun Wang
 
Audit your reactive applications
Audit your reactive applicationsAudit your reactive applications
Audit your reactive applicationsOCTO Technology
 
Groovy In the Cloud
Groovy In the CloudGroovy In the Cloud
Groovy In the CloudJim Driscoll
 
Testing Vue Apps with Cypress.io (STLJS Meetup April 2018)
Testing Vue Apps with Cypress.io (STLJS Meetup April 2018)Testing Vue Apps with Cypress.io (STLJS Meetup April 2018)
Testing Vue Apps with Cypress.io (STLJS Meetup April 2018)Christian Catalan
 
04b swing tutorial
04b swing tutorial04b swing tutorial
04b swing tutorialRobert Wolf
 
Native Java with GraalVM
Native Java with GraalVMNative Java with GraalVM
Native Java with GraalVMSylvain Wallez
 
JavaOne 2016: Life after Modularity
JavaOne 2016: Life after ModularityJavaOne 2016: Life after Modularity
JavaOne 2016: Life after ModularityDanHeidinga
 
performance optimization: UI
performance optimization: UIperformance optimization: UI
performance optimization: UI晓东 杜
 
How to lock a Python in a cage? Managing Python environment inside an R project
How to lock a Python in a cage?  Managing Python environment inside an R projectHow to lock a Python in a cage?  Managing Python environment inside an R project
How to lock a Python in a cage? Managing Python environment inside an R projectWLOG Solutions
 
Eric Lafortune - Fighting application size with ProGuard and beyond
Eric Lafortune - Fighting application size with ProGuard and beyondEric Lafortune - Fighting application size with ProGuard and beyond
Eric Lafortune - Fighting application size with ProGuard and beyondGuardSquare
 
Eric Lafortune - Fighting application size with ProGuard and beyond
Eric Lafortune - Fighting application size with ProGuard and beyondEric Lafortune - Fighting application size with ProGuard and beyond
Eric Lafortune - Fighting application size with ProGuard and beyondGuardSquare
 
Virtual Environment and Web development using Django
Virtual Environment and Web development using DjangoVirtual Environment and Web development using Django
Virtual Environment and Web development using DjangoSunil kumar Mohanty
 
AppSec California 2016 - Making Security Agile
AppSec California 2016 - Making Security AgileAppSec California 2016 - Making Security Agile
AppSec California 2016 - Making Security AgileOleg Gryb
 

Similar to [MOPCON2018] Effectively shrink ProGuard rules for reducing APK size (20)

ProGuard / DexGuard Tips and Tricks
ProGuard / DexGuard Tips and TricksProGuard / DexGuard Tips and Tricks
ProGuard / DexGuard Tips and Tricks
 
手把手教你如何串接 Log 到各種網路服務
手把手教你如何串接 Log 到各種網路服務手把手教你如何串接 Log 到各種網路服務
手把手教你如何串接 Log 到各種網路服務
 
Audit your reactive applications
Audit your reactive applicationsAudit your reactive applications
Audit your reactive applications
 
Js tacktalk team dev js testing performance
Js tacktalk team dev js testing performanceJs tacktalk team dev js testing performance
Js tacktalk team dev js testing performance
 
Groovy In the Cloud
Groovy In the CloudGroovy In the Cloud
Groovy In the Cloud
 
Testing Vue Apps with Cypress.io (STLJS Meetup April 2018)
Testing Vue Apps with Cypress.io (STLJS Meetup April 2018)Testing Vue Apps with Cypress.io (STLJS Meetup April 2018)
Testing Vue Apps with Cypress.io (STLJS Meetup April 2018)
 
groovy & grails - lecture 7
groovy & grails - lecture 7groovy & grails - lecture 7
groovy & grails - lecture 7
 
Proguard android
Proguard androidProguard android
Proguard android
 
04b swing tutorial
04b swing tutorial04b swing tutorial
04b swing tutorial
 
04b swing tutorial
04b swing tutorial04b swing tutorial
04b swing tutorial
 
java swing tutorial for beginners(java programming tutorials)
java swing tutorial for beginners(java programming tutorials)java swing tutorial for beginners(java programming tutorials)
java swing tutorial for beginners(java programming tutorials)
 
Native Java with GraalVM
Native Java with GraalVMNative Java with GraalVM
Native Java with GraalVM
 
JavaOne 2016: Life after Modularity
JavaOne 2016: Life after ModularityJavaOne 2016: Life after Modularity
JavaOne 2016: Life after Modularity
 
performance optimization: UI
performance optimization: UIperformance optimization: UI
performance optimization: UI
 
How to lock a Python in a cage? Managing Python environment inside an R project
How to lock a Python in a cage?  Managing Python environment inside an R projectHow to lock a Python in a cage?  Managing Python environment inside an R project
How to lock a Python in a cage? Managing Python environment inside an R project
 
Eric Lafortune - Fighting application size with ProGuard and beyond
Eric Lafortune - Fighting application size with ProGuard and beyondEric Lafortune - Fighting application size with ProGuard and beyond
Eric Lafortune - Fighting application size with ProGuard and beyond
 
Eric Lafortune - Fighting application size with ProGuard and beyond
Eric Lafortune - Fighting application size with ProGuard and beyondEric Lafortune - Fighting application size with ProGuard and beyond
Eric Lafortune - Fighting application size with ProGuard and beyond
 
Java 9
Java 9Java 9
Java 9
 
Virtual Environment and Web development using Django
Virtual Environment and Web development using DjangoVirtual Environment and Web development using Django
Virtual Environment and Web development using Django
 
AppSec California 2016 - Making Security Agile
AppSec California 2016 - Making Security AgileAppSec California 2016 - Making Security Agile
AppSec California 2016 - Making Security Agile
 

Recently uploaded

Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...jabtakhaidam7
 
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...ronahami
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxkalpana413121
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Servicemeghakumariji156
 
Introduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfIntroduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfsumitt6_25730773
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxMuhammadAsimMuhammad6
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesMayuraD1
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptxJIT KUMAR GUPTA
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxSCMS School of Architecture
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdfKamal Acharya
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network DevicesChandrakantDivate1
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsvanyagupta248
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationBhangaleSonal
 
Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...
Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...
Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...gragchanchal546
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...Amil baba
 

Recently uploaded (20)

Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...
 
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptx
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
Introduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfIntroduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdf
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...
Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...
Ghuma $ Russian Call Girls Ahmedabad ₹7.5k Pick Up & Drop With Cash Payment 8...
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 

[MOPCON2018] Effectively shrink ProGuard rules for reducing APK size

  • 1. Ansgar Lin - Android team of Oath: Yahoo TW 1 如何有效精簡ProGuard規則以達到 縮⼩小apk之⽬目的
  • 2. 2 Foot note iOS/Android Engineer App開發 Frontend Engineer 前端⼯工程師 Backend Engineer 後端⼯工程師 DevOps Engineer 系統維運⼯工程師 AI Engineer / Data Engineer AI⼯工程師 Product Designer 產品設計 Product Manager 產品管理理 Paranoid Software Engineer 資安⼯工程師 E-COMMERCE 電⼦子商務 MEDIA 媒體 DATA ⼤大數據 SEARCH 搜尋引擎
  • 3. Effective ProGuard keep rules for smaller applications (Google I/O ’18) • https://www.youtube.com/watch?v=x9T5EYE-QWQ 3 Foot note About the topic
  • 4. • A most popular open source tool in the Android world. • Shrink apk by removing unused code at run-time. • Obfuscate code to provide minimal protections against reverse engineering • Add “-keep class * extends ClassA” into ProGuard configuration, you can tell ProGuard don’t touch any subclass of ClassA 4 What is ProGuard? Foot note
  • 5. 5 What is ProGuard? // In build.gradle android { buildTypes { debug { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt' } } }
  • 6. 6 What is ProGuard? • A most popular open source tool in the Android world. • Shrink apk by removing unused code at run-time. • Obfuscate code to provide minimal protections against reverse engineering • Add “-keep class * extends ClassA” into ProGuard configuration, you can tell ProGuard don’t touch any subclass of ClassA
  • 7. 7 What is ProGuard? // In build.gradle android { buildTypes { debug { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt' } } }
  • 9. 9 Following outline Foot note • Refinement steps & reasons • Result • Trouble shoot • Q & A
  • 10. 10 Foot note Step 1: Remove duplicated rules
  • 11. 11 ProGuard Work Flow - Read Configuration Foot note
  • 12. 12 ProGuard Work Flow - Read Configuration Foot note
  • 13. 13 ProGuard Work Flow - Read Configuration Foot note
  • 14. 14 ProGuard Work Flow - Read Configuration Foot note
  • 15. 15 ProGuard Work Flow - Read Configuration Foot note
  • 16. 16 ProGuard Work Flow - Read Configuration Foot note
  • 17. 17 ProGuard Work Flow - Read Configuration Foot note
  • 19. 19 ProGuard Work Flow - Read Configuration Foot note proguard-android、proguard-default和proguard-android-optimize
  • 21. 21 Foot note Step 2: Remove redundant rules
  • 22. 22 ProGuard Work Flow - Read Input Foot note
  • 23. 23 ProGuard Work Flow - Read Input Foot note
  • 24. 24 ProGuard Work Flow - Read Input Foot note
  • 25. 25 ProGuard Work Flow - Read Input Foot note Won’t get ProGuard = “-keep” on everything
  • 27. 27 Foot note Step 3: Use “-whyareyoukeeping”
  • 28. 28 Foot note • “-keep class com.example.Blur” —> “-whyareyoukeeping class com.example.Blur” Refine ProGuard Configuration com.example.Blur is invoked by com.example.BlurView: doWorkA(int) is invoked by com.example.BlurView: BlurView(android.content.Context) is invoked by com.example.SelectActivity: onCreate(android.os.Bundle) implements android.app.Activity: onCreate(android.os.Bundle) is a library method.
  • 29. 29 Foot note • “-keep class com.example.Blur” —> “-whyareyoukeeping class com.example.Blur” Refine ProGuard Configuration com.example.Blur is invoked by com.example.BlurView: doWorkA(int) is invoked by com.example.BlurView: BlurView(android.content.Context) is invoked by com.example.SelectActivity: onCreate(android.os.Bundle) implements android.app.Activity: onCreate(android.os.Bundle) is a library method.
  • 30. 30 Foot note • “-keep class com.example.Blur” —> “-whyareyoukeeping class com.example.Blur” Refine ProGuard Configuration com.example.Blur is invoked by com.example.BlurView: doWorkA(int) is invoked by com.example.BlurView: BlurView(android.content.Context) is invoked by com.example.SelectActivity: onCreate(android.os.Bundle) implements android.app.Activity: onCreate(android.os.Bundle) is a library method.
  • 31. 31 Foot note • “-keep class com.example.Blur” —> “-whyareyoukeeping class com.example.Blur” Refine ProGuard Configuration com.example.Blur is invoked by com.example.BlurView: doWorkA(int) is invoked by com.example.BlurView: BlurView(android.content.Context) is invoked by com.example.SelectActivity: onCreate(android.os.Bundle) implements android.app.Activity: onCreate(android.os.Bundle) is a library method.
  • 32. • “-keep class com.example.Blur” —> “-whyareyoukeeping class com.example.Blur” 32 Foot note Refine ProGuard Configuration com.example.Blur is invoked by com.example.BlurView: doWorkA(int) is invoked by com.example.BlurView: BlurView(android.content.Context) is invoked by com.example.SelectActivity: onCreate(android.os.Bundle) implements android.app.Activity: onCreate(android.os.Bundle) is a library method.
  • 33. • “-keep class com.example.Blur” —> “-whyareyoukeeping class com.example.Blur” 33 Foot note Refine ProGuard Configuration com.example.Blur is invoked by com.example.BlurView: doWorkA(int) is invoked by com.example.BlurView: BlurView(android.content.Context) is invoked by com.example.SelectActivity: onCreate(android.os.Bundle) implements android.app.Activity: onCreate(android.os.Bundle) is a library method.
  • 34. • “-keep class com.example.Blur” —> “-whyareyoukeeping class com.example.Blur” 34 Foot note Refine ProGuard Configuration com.example.Blur is invoked by com.example.BlurView: doWorkA(int) is invoked by com.example.BlurView: BlurView(android.content.Context) is invoked by com.example.SelectActivity: onCreate(android.os.Bundle) implements android.app.Activity: onCreate(android.os.Bundle) is a library method.
  • 35. 35 Foot note • message from `-whyareyoukeeping` • is a library class/method/field. • is not been kept • nothing • is kept by a directive in the configuration. Refine ProGuard Configuration
  • 36. 36 Foot note • message from `-whyareyoukeeping` • is a library class/method/field. • is not been kept • nothing • is kept by a directive in the configuration. -keep class com.example.Blur -whyareyoukeeping class com.example.Blur Refine ProGuard Configuration
  • 38. 38 購物中⼼心 - APK size (M) Foot note 0.9 5.52%
  • 39. 39 購物中⼼心 - Method count Foot note 12932 11.30%
  • 40. 40 購物中⼼心 - Method count (com.yahoo.*) Foot note 2294
  • 41. 41 購物中⼼心 - Method count (com.google.*) Foot note 9407
  • 43. 43 Foot note 1. Remove duplicated rules Refine ProGuard Configuration
  • 44. 44 Foot note 2. Remove redundant rules Refine ProGuard Configuration
  • 45. 45 Foot note 3. Use “-whyareyoukeeping” • “-keep class com.example.Blur” —> “-whyareyoukeeping class com.example.Blur” Refine ProGuard Configuration
  • 47. 47 What should be careful? Foot note • 3-rd Library implements with reflection. • Glide: initialization • Parsing data with field names • Gson + Retrofit: JSON model • Anything implementation with static name.
  • 48. 48 What should be careful? Foot note • 3-rd Library implements with reflection. • Glide: initialization • Parsing data with field names • Gson + Retrofit: JSON model • Anything implementation with static name.
  • 49. 49 Foot note W/ProGuard: The class 'com.example.SampleActivity' is calling Class.forName to retrieve the class 'com.exmaple.NoneExistClass', but the latter could not be found. It may have been obfuscated or shrunk. You should consider preserving the class with its original name, with a setting like:   -keep class com.exmaple.NoneExistClass W/System.err: java.lang.ClassNotFoundException: com.exmaple.NoneExistClass at java.lang.Class.classForName(Native Method) • Add “-addconfigurationdebugging” What should be careful?
  • 50. 50 Foot note W/ProGuard: The class 'com.example.SampleActivity' is calling Class.forName to retrieve the class 'com.exmaple.NoneExistClass', but the latter could not be found. It may have been obfuscated or shrunk. You should consider preserving the class with its original name, with a setting like:   -keep class com.exmaple.NoneExistClass W/System.err: java.lang.ClassNotFoundException: com.exmaple.NoneExistClass at java.lang.Class.classForName(Native Method) • Add “-addconfigurationdebugging” What should be careful?
  • 51. 51 Foot note W/ProGuard: The class 'com.example.SampleActivity' is calling Class.forName to retrieve the class 'com.exmaple.NoneExistClass', but the latter could not be found. It may have been obfuscated or shrunk. You should consider preserving the class with its original name, with a setting like:   -keep class com.exmaple.NoneExistClass W/System.err: java.lang.ClassNotFoundException: com.exmaple.NoneExistClass at java.lang.Class.classForName(Native Method) • Add “-addconfigurationdebugging” Only use in debug version What should be careful?
  • 52. 52 Foot note • ClassNotFoundException • NoSuchFieldException • NoSuchMethodException • RuntimeException • UnsatisfiedLinkError • IOException • Add “-addconfigurationdebugging” What should be careful?
  • 53. 53 Foot note W/ProGuard: The class 'com.example.SampleActivity' is calling Class.forName to retrieve the class 'com.exmaple.NoneExistClass', but the latter could not be found. It may have been obfuscated or shrunk. You should consider preserving the class with its original name, with a setting like:   -keep class com.exmaple.NoneExistClass W/System.err: java.lang.ClassNotFoundException: com.exmaple.NoneExistClass at java.lang.Class.classForName(Native Method) • Add “-addconfigurationdebugging” What should be careful?
  • 54. 54 R8
  • 55. 55 R8 Foot note • Powered by Google • Default enable after Android Studio(3.3) • Java bytecode(.class) —> Dalvik bytecode(.dex) • ProGuard configuration is acceptable. • Changes: • “-addconfigurationdebugging” will be ignored by default. • Error detection while compiling.
  • 56. 56 Your turn! Foot note • Start playing with ProGuard
  • 58. 58 Reference Foot note • MCE 2014 - Eric Lafortune - ProGuard, Optimizer and Obfuscator in the Android SDK • Effective ProGuard keep rules for smaller applications (Google I/O ’18) • About ProGuard - Part 1