Obfuscation in Android Studio Obfuscation in Android Studio android android

Obfuscation in Android Studio


  • Basic Obfuscation

To obfuscate code in Android studio just go to your build.gradle file in your Android Studio project:

enter image description here

Change the minifyEnabled property from false to true

enter image description here

This is a basic Obfuscation.

After generating the apk you can see the obfuscation result by decompiling the apk with any software. This page could help you:

http://www.decompileandroid.com/

In the obfuscation result you will see classes with name: a,b,c....

enter image description here

And the obfuscation variables and methods will have also names like aa,c,ac...

enter image description here

  • Normal obfuscation:

To obfuscate the code in a more complex form you could go to your root directory app and create a .pro file. For example in the following picture I have created the file: proguard-rules-new.pro. In the same directory you should see a file called proguard-rules.pro

enter image description here

Now add the file you have created to the build.gradle file

enter image description here

And edit the .pro file you have create with your own custom proguard rules

enter image description here


First enable minifyEnabled in your build.gradle file, like

minifyEnabled true

After this, add below lines in progurad-rules.txt file

-keep class yourpackage.** { *; }-dontusemixedcaseclassnames-dontskipnonpubliclibraryclasses-verbose

For checking that its working fine go to:

http://www.javadecompilers.com/apktool website so that you can verify after decompilation.

It will work and your classes will be hidden completely.


Update: R8 is by default enabled in android studio version 3.4.0 and above

In android studio 3.4+, R8 is enabled by default so no need to add additional property though you can opt for deep optimizations by adding fullMode property in gradle.properties as:

android.enableR8.fullMode=true 

You can disable R8 and enable proguard by adding following properties in gradle.properties as:

android.enableR8 = falseuseProguard = true

Android September 2018 release a new tool R8 shrinker and obfuscation tool.

R8 - R8 is a java code shrinker and minifying tool that converts java byte code to optimized dex code

For AS version below 3.4.0.

  1. Open gradle.properties
  2. Add android.enableR8 = true

as

# Specifies the JVM arguments used for the daemon process.# The setting is particularly useful for tweaking memory settings.org.gradle.jvmargs=-Xmx1536m# When configured, Gradle will run in incubating parallel mode.# This option should only be used with decoupled projects. More details, visitandroid.enableR8 = true

Minimum Requirements:

  • Android studio 3.2 September 2018 release or above
  • Java 8

R8 Tool

R8 supports Proguard:

Keep in mind, R8 is designed to work with your existing ProGuard rules, so you’ll likely not need to take any actions to benefit from R8. However, because it’s a different technology to ProGuard that’s designed specifically for Android projects, shrinking and optimization may result in removing code that ProGuard may have not. So, in this unlikely situation, you might need to add additional rules to keep that code in your build output.

To Disable R8 in AS 3.4.0 and above:

# Disables R8 for Android Library modules only.android.enableR8.libraries = false# Disables R8 for all modules.android.enableR8 = false

Note: For a given build type, if you set useProguard to false in your app module's build.gradle file, the Android Gradle plugin uses R8 to shrink your app's code for that build type, regardless of whether you disable R8 in your project's gradle.properties file.