Skip to content

Instantly share code, notes, and snippets.

@Prasanth-Shakti
Last active October 7, 2023 23:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Prasanth-Shakti/26d4c870a4352fd9a380f358fa837aba to your computer and use it in GitHub Desktop.
Save Prasanth-Shakti/26d4c870a4352fd9a380f358fa837aba to your computer and use it in GitHub Desktop.
Enabling android code obfuscation in react native with R8 and proguard rules

Android code obfuscation in react native

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'
	}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment