Obfuscation in Android Studio

Obfuscation in Android Studio

  • Basic Obfuscation

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

Sample Image

Change the minifyEnabled property from false to true

Sample Image

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

Sample Image

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

Sample Image

  • 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

Sample Image

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

Sample Image

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

Sample Image

How to enable ProGuard obfuscation in Android Studio?

I figured out the problem:

Open up the proguard-rules.pro for your project and add this to the bottom:

-dontwarn java.nio.file.Files
-dontwarn java.nio.file.Path
-dontwarn java.nio.file.OpenOption
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement

Basically how I solved it was this I tried to run my app in 'release' mode and got a bunch of errors similar to this guy here: https://github.com/square/okio/issues/144

I pretty much followed what he said and fixed it.

Hope this can help others with generating their APK's!

visit more detail here :

Error:Execution failed for task ':app:packageRelease'. > Unable to compute hash of /../AndroidStudioProjects/../classes.jar

Pro-guard Obfuscation not working in android studio

To enable ProGuard in Android Studio. Below is the sample how to enable default ProGuard in Android Studio.

1) Go to the build.gradle file of app

2) enable the proguard minifyEnabled true and useProguard true

3) enable shrinkResources true to reduce the APK size by shrinking resources.

4) proguardFiles getDefaultProguardFile('proguard-android.txt') to enable the default one. If you want to use your own proguard file then use the below rules.

buildTypes {
release {
debuggable false
useProguard true
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}

debug {
debuggable true
useProguard true
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

How to add Code Obfuscation for my android application

Use the below code to get your solution.

android {
buildTypes {
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
zipAlignEnabled true
shrinkResources false
}
release {
debuggable false
// Enables code shrinking, obfuscation, and optimization for only
// your project's release build type.
minifyEnabled true

// Includes the default ProGuard rules files that are packaged with
// the Android Gradle plugin. To learn more, go to the section about
// R8 configuration files.
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

zipAlignEnabled true

// Enables resource shrinking, which is performed by the
// Android Gradle plugin.
shrinkResources true
}
}
}

TODO update proguard

#Specifies not to ignore non-public library classes.
-dontskipnonpubliclibraryclasses

#Specifies not to ignore package visible library class members
-dontskipnonpubliclibraryclassmembers

-optimizationpasses 5
#Specifies that the access modifiers of classes and class members may have become broad during processing. This can improve the results of the optimization step.
-allowaccessmodification
#Specifies that interfaces may be merged, even if their implementing classes don't implement all interface methods. This can reduce the size of the output by reducing the total number of classes.
-mergeinterfacesaggressively

#Specifies to apply aggressive overloading while obfuscating. Multiple fields and methods can then get the same names, This option can make the processed code even smaller
#-overloadaggressively

#Specifies to repackage all packages that are renamed, by moving them into the single given parent package
-flattenpackagehierarchy

#Specifies to repackage all class files that are renamed, by moving them into the single given package. Without argument or with an empty string (''), the package is removed completely.
-repackageclasses

#For example, if your code contains a large number of hard-coded strings that refer to classes, and you prefer not to keep their names, you may want to use this option
-adaptclassstrings
#Specifies the resource files to be renamed, all resource files that correspond to class files are renamed
-adaptresourcefilenames

#Specifies the resource files whose contents are to be updated. Any class names mentioned in the resource files are renamed
-adaptresourcefilecontents

#Specifies not to verify the processed class files.
#-dontpreverify

-verbose

#Specifies to print any warnings about unresolved references and other important problems, but to continue processing in any case.
-ignorewarnings

# ADDED
#-dontobfuscate
#-useuniqueclassmembernames

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}

-assumenosideeffects class android.util.Log {
public static boolean isLoggable(java.lang.String, int);
public static *** d(...);
public static *** v(...);
public static *** i(...);
public static *** w(...);
public static *** e(...);
}


Related Topics



Leave a reply



Submit