How to add r8 rules in android? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to add r8 rules in android? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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

Answer

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

We are here to answer your question about How to add r8 rules in android? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji