Obfuscation in Android Studio

2019-01-12 17:43发布

问题:

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.

回答1:

  • Basic Obfuscation

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

Change the minifyEnabled property from false to true

That is a basic offuscation.

After generating the apk you can see the obfuscation result decompilating the apk with any software, for example this page could help you:

http://www.decompileandroid.com/

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

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

  • 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 create the file: proguard-rules-new.pro, in the same directory you should see a file called proguard-rules.pro

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

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



回答2:

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.



回答3:

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

  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



回答4:

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