Skip to main content
Skip table of contents

R8

R8 is ProGuard's replacement as of Android Gradle plugin 3.4.0. and is compatible with the same rules used by ProGuard. Full details are provided in the official documentation at https://developer.android.com/studio/build/shrink-code

Configuring R8

R8 configuration uses ProGuard rules, which modify its default behaviour by enabling or disabling features and instructing it not to obfuscate or shrink specific classes. The complete set of ProGuard rules can be found at https://www.guardsquare.com/en/products/proguard/manual/usage.

ProGuard rules files

The Android Gradle plugin generates proguard-android-optimize.txt, which contains configuration applicable to most Android projects; you should configure R8 to use this by default (see below).

It may be necessary to define app-specific rules to work around issues caused by limitations in R8's ability to correctly analyse code in certain situations, such as when your code uses reflection or calls native code via the Java Native Interface (JNI). You may need to force R8 to keep specific code in cases like these rather than stripping it out. To define these rules, create your own rules file - for example, named proguard-rules.pro - and place it in the app-level directory.

A basic rules file may look something like this (comments are preceded by # ):

NONE
# Keep MyJNIClass
-keep public class com.example.MyJNIClass
 
# Keep all classes in com.example.database_package, but not in its subpackages
-keep public class com.example.database_package.*
 
# Keep all classes in com.example.open_package, and those in its subpackages
-keep public class com.example.open_package.**

SDK embedded rules

In addition to the default rules in proguard-android-optimize.txt and those specified by the developer, the CONNECT Player SDK 5 library contains its own set of rules automatically applied when R8 is used to obfuscate the app. This protects the CONNECT Player SDK 5 while requiring no additional work from the developer.

Applying R8 to your app

The following additions to your app-level build.gradle file will enable shrinking, obfuscation and optimization:

GROOVY
android {
    buildTypes {
        release {
			// Enables obfuscation, code-shrinking and optimization
            minifyEnabled true
            // Enables resource shrinking, removing unused resources
            shrinkResources true
            // Includes the Android Gradle plugin default rules, with proguard-rules.pro containing app-specific rules
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    ...
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.