Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obfuscate package name in android studio

I have successfully obfuscate class name and methods using following code

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

in app level build.gradle file

and

-dontwarn mypackage.**
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose
-dontwarn

in proguard-rules.pro

but my problem is package name is not obfuscated.

So how can I achive it, please help.

Thanks

like image 283
Neha Shukla Avatar asked Mar 08 '23 18:03

Neha Shukla


1 Answers

In Pro-Guard, package names can be obfuscated in various ways, with increasing levels of obfuscation and compactness. For example, consider the following classes:

mycompany.myapplication.MyMain 
mycompany.myapplication.Foo
mycompany.myapplication.Bar 
mycompany.myapplication.extra.FirstExtra 
mycompany.myapplication.extra.SecondExtra 
mycompany.util.FirstUtil mycompany.util.SecondUtil

Let's assume the class name mycompany.myapplication.MyMain is the main application class that is kept by the configuration. All other class names except Main can be obfuscated.

By default, packages that contain classes that can't be renamed aren't renamed either, and the package hierarchy is preserved. This results in obfuscated class names like these:

mycompany.myapplication.MyMain 
mycompany.myapplication.a 
mycompany.myapplication.b 
mycompany.myapplication.a.a 
mycompany.myapplication.a.b 
mycompany.a.a 
mycompany.a.b

The -flattenpackagehierarchy option obfuscates the package names further, by flattening the package hierarchy of obfuscated packages:

-flattenpackagehierarchy 'myobfuscated'

Alternatively, the -repackageclasses option obfuscates the entire packaging, by combining obfuscated classes into a single package:

-repackageclasses 'myobfuscated'

The obfuscated class names then look as follows:

mycompany.myapplication.MyMain 
mycompany.myapplication.a 
mycompany.myapplication.b 
myobfuscated.a 
myobfuscated.b 
myobfuscated.c 
myobfuscated.d

Additionally specifying the -allowaccessmodification option allows access permissions of classes and class members to be broadened, opening up the opportunity to repackage all obfuscated classes:

-repackageclasses 'myobfuscated' 
-allowaccessmodification

The obfuscated class names then look as follows:

mycompany.myapplication.MyMain 
myobfuscated.a 
myobfuscated.b 
myobfuscated.c 
myobfuscated.d 
myobfuscated.e 
myobfuscated.f 

The specified target package can always be the root package. For instance:

-repackageclasses '' 
-allowaccessmodification

The obfuscated class names are then the shortest possible names:

mycompany.myapplication.MyMain 
a 
b 
c 
d 
e 
f

Note that not all levels of obfuscation of package names may be acceptable for all code. Notably, you may have to take into account that your application may contain resource files that have to be adapted.

To obfuscate code in Android studio just visit this post.

Or import your code into NetBeans and use its build-in functional to compile, pre-verify, obfuscate and package the code))).

like image 178
Lil Bro Avatar answered Mar 10 '23 10:03

Lil Bro