50

Is there any obfuscation tool to use with Android Studio? IntelliGuard plugin is declared to be supported by the Studio, but it doesn't work actually due to missing AntSupport plugin. I wan't able to find one in the repository. Any ideas?

P.S. Android Studio build process is based on Gradle, so I wouldn't expect to see Ant support there at all. May be I'm wrong.

no id
  • 1,642
  • 3
  • 24
  • 36

5 Answers5

72
  • 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

Gurunath Sripad
  • 1,308
  • 2
  • 14
  • 24
Led Machine
  • 7,122
  • 3
  • 47
  • 49
  • It gives this error after uploading apk "AccessDenied" – VVB Jan 28 '16 at 12:30
  • 2
    I have the same configuration except my file is `proguard.cfg` tried to decompile the apk and the classes are the same without obsfucation.. Do I need to rename .cfg to .pro? – mboy Oct 28 '16 at 11:10
  • 4
    Is there any plugin which can write proguard rules automatically – Pramod Waghmare Nov 20 '17 at 06:22
22

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.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
amit pandya
  • 1,384
  • 13
  • 22
14

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 = false
useProguard = 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, visit

android.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.

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • 1
    I guess currently the option setting 'android.enableR8=true' is experimental and unsupported. I don't suggest to use it until supported version is released. – ilkayaktas Oct 16 '18 at 14:15
  • I compared with and without. It didn't shrink at all :) – Sabri Meviş Dec 13 '18 at 11:25
  • if you just write `minifyEnabled true`, in the build-output you can see a task **Task :app:minifyReleaseWithR8**, now you don't need to do it manually, let as take care of all those.. thanks... – Maifee Ul Asad May 17 '20 at 19:06
  • 1
    @MaifeeUlAsad yes, the new behavior is mentioned with an update though moved it to a specific section with more details, Thank! – Pavneet_Singh May 18 '20 at 10:22
2

Proguard is well-supported on Android studio. You have to configure Gradle to run it. Instructions: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Running-ProGuard

Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
  • 1
    With minifyEnabled set to true, code is shrinked and obfuscated by ProGuard. https://teamtreehouse.com/community/what-is-minifyenabled – Roger Huang Oct 14 '15 at 03:27
1

after setting minifyEnabled to true there are two version of apk you can get, so that you have to add debug option in your build.gradle to obfuscate debug one:

buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
    debug {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}

sync, build and build apk

CodeToLife
  • 3,672
  • 2
  • 41
  • 29