How we can add R8 rules in an android project for dependencies and excluding files and packages from minifing and obfuscate?.
-
1Possible duplicate of [How to write rules for proguard?](https://stackoverflow.com/questions/45976225/how-to-write-rules-for-proguard) – Vladyslav Matviienko Sep 03 '19 at 07:03
1 Answers
Adding R8 rules is similar to progurad rules, but some dependencies we don't need to add the rules in R8, it might be mentioned in the docs. From Android Studio 3.4 R8 is the default code shrinker.
Add this line in build.gradle app module
buildTypes {
release {
minifyEnabled true //Important step
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
Select proguard-rules.pro
Add the rule to exclude package or files
-keep class com.xyz.model.** { *; }
The above code excludes model package from minifing , its better to exclude your network pojo class from minifing.
If any dependency you added have proguard/ R8 rules add it also, NOTE : libraries like Retrofit we don't need to add it in R8, it will be mentioned in there respective github pages
-keepattributes *Annotation*
-keepclassmembers class * {
@org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }
# Only required if you use AsyncExecutor
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
<init>(java.lang.Throwable);
}
The above example is for green bot proguard rules. just copy paste it in your proguard-rules.pro files
For reference : https://www.youtube.com/watch?v=yduedsaxfDw

- 2,703
- 3
- 20
- 39
-
the official docs of retrofit https://square.github.io/retrofit/ mentioned that you have to add rules for retrofit – EL TEGANI MOHAMED HAMAD GABIR Jan 30 '20 at 06:50
-
Thanks, Please check this @ELTEGANIMOHAMEDHAMMAD https://github.com/square/retrofit If you are using R8 the shrinking and obfuscation rules are included automatically. ProGuard users must manually add the options from this file. (Note: You might also need rules for OkHttp and Okio which are dependencies of this library) – Eldhopj Jan 30 '20 at 17:31