How to add Code Obfuscation for my android application
Asked Answered
H

2

4

How to add Code Obfuscation for android apk. I used proguard but still seeing code from app decompiler.

I added progruad like that

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

It is seeing all of my code and class after decompile.After decomplie

My Proguard file enter image description here

Hifi answered 23/7, 2019 at 4:19 Comment(9)
Can you show me your proguard file?Substitute
In my proguard file,I added no code.Does need to add?Hifi
you need to add some rules in proguard fileLacewing
If there are no rules added in your progaurd file then it's not working.Substitute
Can you show me sampleHifi
Do you want the solution then please show your progaurd file it's not a concern for you?Substitute
Possible duplicate of Obfuscation in Android StudioPungy
@Prince I added my proguard file in my codeHifi
please do update your progaurd file.Substitute
S
7

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(...);
}
Substitute answered 23/7, 2019 at 4:28 Comment(2)
When I add this->Expecting type and name instead of just '***' before '(' in line 367 of file 'D:\AMS mobile\PhoonEw\app\build\intermediates\proguard-rules\dev\release\aapt_rules.txt'Hifi
just delete build folder and rebuild project.Substitute
E
0

R8, the new code shrinker from Google, is available in Android

In your project's gradle.properties file:

android.enableR8=true  

To turn on more optimization:

android.enableR8.fullMode=true

In your build.gradle(Module:app)

android {
   ...
   buildTypes {
       release {
        // Release app code minification:
           useProguard false // <-- disable proguard
           minifyEnabled true // <-- enable minification
           proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
       }
       debug {
       // Debug app code minification
           useProguard false 
           minifyEnabled true
        }
   }
       ...
   }

Android/java: Transition / Migration from ProGuard to R8?

https://android-developers.googleblog.com/2018/11/r8-new-code-shrinker-from-google-is.html

Estancia answered 23/7, 2019 at 5:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.