CONNECT Player SDK 5 for Android

Obfuscation, code shrinking and optimization

Obfuscation is carried out during the optional build step of shrinking an app to remove unused code and resources. It protects the app by making it difficult to reverse engineer or debug, while leaving it semantically identical to the original. It is usually only performed on the release version of an app, enabling you to continue to debug your app during development. Obfuscation may impact the performance of your app, so it is necessary to measure the performance to determine whether further configuration is needed to mitigate any issues.

Shrinking and obfuscating an app is done using Google's R8 tool. This page describes how to enable R8, leaving the fine details to the official documentation.

Although R8 may be referred to as ProGuard, it is actually ProGuard's replacement as of Android Gradle plugin 3.4.0. R8 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

.

ProGuard Rules

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.

The Android Gradle plugin generates proguard-android-optimize.txt which contains configuration that applies to the majority of Android projects; R8 should be configured to use this by default (see below).

It may be necessary to define app-specific rules to work around issues caused by R8's limitations to analyse code correctly certain situations; such as when your code uses reflection or calls native code via the Java Native Interface (JNI). In cases like these, you may need to force R8 to keep specific code rather than remove it. 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 #):

# 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 that will be applied automatically 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'
        }
    }
    ...
}