Enabling R8 code obfuscation:
App > build.gradle
debug {
signingConfig signingConfigs.debug
}
release {
signingConfig signingConfigs.release
debuggable false
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
Default three proguard rules files are automatically generated in app > build > intermediates > proguard-files
- proguard-android-optimize.txt
- proguard-android.txt
- proguard-defaults.txt
We can choose based on our requirement.
For additional rules we can write below code in app > Proguard-rules.pro
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /usr/local/Cellar/android-sdk/24.3.3/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
-assumenosideeffects classandroid.utils.Log{*;}
-keep class io.invertase.firebase.** { *; }. ————> Firebase rules are added additionally
-dontwarn io.invertase.firebase.**
-keepattributes *Annotation*
-keepclassmembers class ** {
@org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }
For running : If below is not there in app > build.gradle, please add
// applicationVariants are e.g. debug, release
applicationVariants.all { variant ->
variant.outputs.each { output ->
// For each separate APK per architecture, set a unique version code as described here:
// https://developer.android.com/studio/build/configure-apk-splits.html
def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4]
def abi = output.getFilter(OutputFile.ABI)
if (abi != null) { // null for the universal-debug, universal-release variants
output.versionCodeOverride =
versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
}
}
}
By default react-native run-android
builds the apk in debug mode but R8 obfuscation doesn’t work in debug.
Running in release mode:
react-native run-android --variant=release
Building the apk in release mode:
Cd android
./gradlew assembleRelease
For enabling code obfuscation in proguard instead of R8.
debug {
signingConfig signingConfigs.debug
}
release {
signingConfig signingConfigs.release
debuggable false
minifyEnabled true
useProguard true ———> enables proguard
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}