Obfuscation in Android Studio
Asked Answered
M

5

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.

Matsu answered 25/6, 2013 at 5:49 Comment(1)
tools.android.com/tech-docs/new-build-system/… might helpOrissa
H
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

Highoctane answered 29/5, 2015 at 16:1 Comment(3)
It gives this error after uploading apk "AccessDenied"Bushore
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?Spangle
Is there any plugin which can write proguard rules automaticallyBedrabble
B
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.

Bacchanal answered 10/7, 2017 at 13:16 Comment(2)
Personally, I'll never upload my apk to a website like that! How can I trust that site?!?Clemenciaclemency
@Clemenciaclemency I have gave a reference just for testing and crossChecking purposes.Bacchanal
P
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.

Pal answered 3/10, 2018 at 5:25 Comment(4)
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.Transmit
I compared with and without. It didn't shrink at all :)Locomotor
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...Prang
@MaifeeUlAsad yes, the new behavior is mentioned with an update though moved it to a specific section with more details, Thank!Pal
B
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

Burtburta answered 25/6, 2013 at 5:57 Comment(1)
With minifyEnabled set to true, code is shrinked and obfuscated by ProGuard. teamtreehouse.com/community/what-is-minifyenabledEstrone
S
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

Swadeshi answered 4/3, 2021 at 16:17 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.