Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add r8 rules in android?

How we can add R8 rules in an android project for dependencies and excluding files and packages from minifing and obfuscate?.

like image 740
Eldhopj Avatar asked Oct 17 '25 07:10

Eldhopj


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

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

like image 119
Eldhopj Avatar answered Oct 19 '25 23:10

Eldhopj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!