ProGuard
-
Upload
tomas-kypta -
Category
Technology
-
view
342 -
download
2
Transcript of ProGuard
![Page 1: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/1.jpg)
ProGuardTomáš Kypta
![Page 2: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/2.jpg)
ProGuard
• free tool
• shrinker, optimizer, obfuscator
![Page 3: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/3.jpg)
ProGuard
![Page 4: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/4.jpg)
Configuration
![Page 5: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/5.jpg)
Configuration
• Empty configuration?
• You have to specify '-keep' options for the shrinking step.
![Page 6: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/6.jpg)
Configuration
• define entry points
![Page 7: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/7.jpg)
Inputs & Outputs
-injars
-libraryjars
-outjars
![Page 8: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/8.jpg)
Keep rules-keep
• keep class and class members
-keepclassmembers
• keep class members if their class is kept
-keepclasseswithmembers
• keep class with members if all the class members are present
![Page 9: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/9.jpg)
Keep rules
-keepnames
• short for -keep,allowshrinking class_specification
-keepclassmembernames
-keepclasseswithmembernames
![Page 10: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/10.jpg)
Keep Attributes
• -keepattributes Signature
• for generics (JDK 5.0 and higher)
• -keepattributes Exceptions
• for exceptions
![Page 11: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/11.jpg)
Keep Attributes
-keepattributes *Annotation*
*Annotation* = RuntimeVisibleAnnotations, RuntimeInvisibleAnnotations, RuntimeVisibleParameterAnnotations, RuntimeInvisibleParameterAnnotations, AnnotationDefault
![Page 12: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/12.jpg)
Keep Attributes-keepattributes EnclosingMethod
• specified the method in which the class was defined
-keepattributes InnerClasses
• if you have inner class that can be reference from outside of the library
![Page 13: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/13.jpg)
Other
-keepparameternames
• keeps parameter names in LocalVariableTable and LocalVariableTypeTable
• might be useful for IDEs
![Page 14: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/14.jpg)
Keep Modifiersallowshrinking
• Specifies whether the entry points specified in the keep tag may be shrunk.
allowoptimization
• Specifies whether the entry points specified in the keep tag may be optimized.
allowobfuscation
• Specifies whether the entry points specified in the keep tag may be obfuscated.
![Page 15: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/15.jpg)
Output Filesdump.txt
• internal structure of code
mapping.txt
• obfuscation mapping
seeds.txt
• unobfuscated code
usage.txt
• stripped code
![Page 16: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/16.jpg)
Notes & Warnings
• Notes
• -dontnote <filter>
• Warnings
• -dontwarn <filter>
![Page 17: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/17.jpg)
Problems
• Reflection!!! • missing attributes
![Page 18: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/18.jpg)
ProGuard & Android
![Page 19: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/19.jpg)
Output files
• created in build/outputs/mapping
![Page 20: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/20.jpg)
Gradle config
![Page 21: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/21.jpg)
Gradle config
buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }
![Page 22: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/22.jpg)
Gradle configbuildTypes { debug { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), ‘proguard-rules.pro’, ‘proguard-rules-debug.pro' } release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }
![Page 23: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/23.jpg)
Gradle config
productFlavors { flavor1 { proguardFile ‘proguard-rules-flavor1.pro' } }
![Page 24: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/24.jpg)
ProGuard & Android Libraries
![Page 25: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/25.jpg)
Gradle config - library
defaultConfig { consumerProguardFiles ‘proguard-rules-lib.pro’}
• packed into aar
• proguard.txt
![Page 26: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/26.jpg)
Generated ProGuard config
• build/intermediates/proguard-rules
• components in AndroidManifest.xml
• custom views in layouts
• only when minifyEnabled true
![Page 27: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/27.jpg)
Config merging
-printconfiguration configuration.txt
• merging is a bit stupid
-keepattributes *Annotation*,SourceFile,LineNumberTable,Signature,Exceptions,*Annotation*,Exceptions,*Annotation*,Exceptions,*Annotation*,Signature,Exceptions,*Annotation*,Exceptions,Signature,*Annotation*,Signature,Exceptions,*Annotation*,Exceptions,*Annotation*,Signature,Exceptions,*Annotation*,Signature,Signature,Exceptions,*Annotation*,Signature
![Page 28: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/28.jpg)
Apk build
• ProGuard output in apk build
• build/intermediates/classes-proguard/{variant}/classes.jar
![Page 29: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/29.jpg)
Deobfuscation• ReTrace
• retrace.sh mapping.txt [<stacktrace_file>]
• completeness depends on presence of line number tables • -keepattributes SourceFile,LineNumberTable
• ambiguous without these attributes - it will list all possible original method names
• -renamesourcefileattribute MyApp
• resolve unknown source
![Page 30: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/30.jpg)
Deobfuscation
![Page 31: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/31.jpg)
Frequent library configs
![Page 32: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/32.jpg)
Some library configs• Retrofit
-dontwarn retrofit.** -keep class retrofit.** { *; } -keepattributes Signature -keepattributes Exceptions
• ButterKnife -keep class butterknife.** { *; } -dontwarn butterknife.internal.** -keep class **$$ViewBinder { *; } -keepclasseswithmembernames class * { @butterknife.* <fields>; } -keepclasseswithmembernames class * { @butterknife.* <methods>; }
![Page 33: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/33.jpg)
Some library configs
• Otto -keepattributes *Annotation* -keepclassmembers class ** { @com.squareup.otto.Subscribe public *; @com.squareup.otto.Produce public *; }
![Page 34: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/34.jpg)
Some library configs
• Dagger 2
• doesn’t require anything
• Rx
• dependency compile 'com.artemzin.rxjava:proguard-rules:1.0.14.2'
![Page 35: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/35.jpg)
Tips, Tricks & Traps
![Page 36: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/36.jpg)
Tips, Tricks & Traps
• never use
-dontwarn **
-dontnote **
![Page 37: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/37.jpg)
Tips, Tricks & Traps• in library projects, in customerProguardFiles don’t
use: • -printconfiguration configuration.txt
• -dontobfuscate, -dontoptimize, …
• -keepattributes SourceFile,LineNumberTable,LocalVariableTable,LocalVariableTypeTable
• declare the bare minimum
![Page 38: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/38.jpg)
Tips, Tricks & Traps-applymapping <file>
• reuse previous mapping
-obfuscationdictionary <file>
• custom dictionary
• you can e.g. use Java keywords there (not that helpful)
![Page 39: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/39.jpg)
Tips, Tricks & Traps
-repackageclasses 'com.example.obfuscated'
• in Java there can be a problem when class tries to load resource in the same directory
![Page 40: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/40.jpg)
DexGuard• comercial
• extra features
• resource obfuscation
• string encryption
• class encryption
• dex splitting
• native code obfuscation
![Page 41: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/41.jpg)
Links
• http://proguard.sourceforge.net/
• https://www.guardsquare.com/dexguard
![Page 42: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/42.jpg)
Q&A
![Page 43: ProGuard](https://reader031.fdocument.pub/reader031/viewer/2022030216/588812191a28ab083c8b505f/html5/thumbnails/43.jpg)
THE END