Android Proguard 如何混淆package name

来源:互联网 发布:简单力学分析软件 编辑:程序博客网 时间:2024/06/04 01:36

一、在Android 的proguard-project.txt 文件里我们可以看到如下选项:

参考 http://proguard.sourceforge.net/manual/usage.html#keeppackagenames

-keeppackagenames [package_filter]

Specifies not to obfuscate the given package names. The optional filter is a comma-separated list of package names. Package names can contain ?, *, and ** wildcards, and they can be preceded by the ! negator. Only applicable when obfuscating.

-flattenpackagehierarchy [package_name]

Specifies to repackage all packages that are renamed, by moving them into the single given parent package. Without argument or with an empty string (''), the packages are moved into the root package. This option is one example of further obfuscating package names. It can make the processed code smaller and less comprehensible. Only applicable when obfuscating.

-repackageclasses [package_name]

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. This option overrides the -flattenpackagehierarchy option. It is another example of further obfuscating package names. It can make the processed code even smaller and less comprehensible. Its deprecated name is -defaultpackage. Only applicable when obfuscating.

Counter-indication: classes that look for resource files in their package directories will no longer work properly if they are moved elsewhere. When in doubt, just leave the packaging untouched by not using this option.

By default, packages that contain classes that can't be renamed aren't renamed either, and the package hierarchy is preserved. (因此我们想到另外一种不让包名被混淆的方法:即在每个package 下放一个空的class指定为不混淆,这样整个package name就会保留,不会把包名也混淆掉)

二、具体使用方法:Obfuscating package names

参考 http://proguard.sourceforge.net/manual/examples.html#repackaging

Package names can be obfuscated in various ways, with increasing levels of obfuscation and compactness. For example, consider the following classes:

mycompany.myapplication.MyMainmycompany.myapplication.Foomycompany.myapplication.Barmycompany.myapplication.extra.FirstExtramycompany.myapplication.extra.SecondExtramycompany.util.FirstUtilmycompany.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 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.MyMainmycompany.myapplication.amycompany.myapplication.bmycompany.myapplication.a.amycompany.myapplication.a.bmycompany.a.amycompany.a.b

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

-flattenpackagehierarchy 'myobfuscated'

The obfuscated class names then look as follows:

mycompany.myapplication.MyMainmycompany.myapplication.amycompany.myapplication.bmyobfuscated.a.amyobfuscated.a.bmyobfuscated.b.amyobfuscated.b.b

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.MyMainmycompany.myapplication.amycompany.myapplication.bmyobfuscated.amyobfuscated.bmyobfuscated.cmyobfuscated.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.MyMainmyobfuscated.amyobfuscated.bmyobfuscated.cmyobfuscated.dmyobfuscated.emyobfuscated.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.MyMainabcdef

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.

0 0
原创粉丝点击