Android Learning Tips---Android Code obfuscation (2)

Source: Internet
Author: User

I am on the blog to publish some of my Android learning experience, I hope that everyone can help.
This article tells us about how Android obfuscation can be customized Proguard parameters to implement

Environment Configuration Reference Android learning experience (1)---Android environment under Mac build
Signature Reference Android learning experience (KeyStore)---APK signature
Code Obfuscation Android Learning Experience (1)----Android code obfuscation
Dex2jar&jd-guiandroid Learning Experience (8)---Mac Dex2jar and Jd-gui use

1, Introduction Proguard configuration

In the previous article we described how to do proguard obfuscation by default
There are several situations where you cannot change the name of a Java element, or it will cause a program error.

一, 我们用到反射的地方。二,我们代码依赖于系统的接口,比如被系统代码调用的回调方法,这种情况最复杂。三, 是我们的java 元素名称是在配置文件中配置好的。

So when using Proguard, we need to use the config file to tell Proguard which Java elements are not to be confused.

2. Proguard Configuration Parameters
-include {filename}                      从给定的文件中读取配置参数 -basedirectory {directoryname}           指定基础目录为以后相对的档案名称 -injars {class_path}                     指定要处理的应用程序jar,war,ear和目录 -outjars {class_path}                    指定处理完后要输出的jar,war,ear和目录的名称 -libraryjars {classpath}                 指定要处理的应用程序jar,war,ear和目录所需要的程序库文件 -dontskipnonpubliclibraryclasses         指定不去忽略非公共的库类。 -dontskipnonpubliclibraryclassmembers    

Retention options

-keep {Modifier} {class_specification}    保护指定的类文件和类的成员 -keepclassmembers {modifier} {class_specification}     保护指定类的成员,如果此类受到保护他们会保护的更好-keepclasseswithmembers {class_specification}     保护指定的类和类的成员,但条件是所有指定的类和类成员是要存在。 -keepnames {class_specification}     保护指定的类和类的成员的名称(如果他们不会压缩步骤中删除) -keepclassmembernames {class_specification}     保护指定的类的成员的名称(如果他们不会压缩步骤中删除) -keepclasseswithmembernames {class_specification}     保护指定的类和类的成员的名称,如果所有指定的类成员出席(在压缩步骤之后) -printseeds {filename}     

Compression

-dontshrink    不压缩输入的类文件 -printusage {filename} -whyareyoukeeping {class_specification}     

Optimization

-dontoptimize     不优化输入的类文件 -assumenosideeffects {class_specification}     优化时假设指定的方法,没有任何副作用 -allowaccessmodification     

Confuse

-dontobfuscate                            不混淆输入的类文件 -printmapping {filename} -applymapping {filename}                  重用映射增加混淆 -obfuscationdictionary {filename}         使用给定文件中的关键字作为要混淆方法的名称 -overloadaggressively                     混淆时应用侵入式重载 -useuniqueclassmembernames                确定统一的混淆类的成员名称来增加混淆 -flattenpackagehierarchy {package_name}   重新包装所有重命名的包并放在给定的单一包中 -repackageclass {package_name}            重新包装所有重命名的类文件中放在给定的单一包中 -dontusemixedcaseclassnames               混淆时不会产生形形色色的类名 -keepattributes {attribute_name,...}     保护给定的可选属性,例如LineNumberTable, LocalVariableTable, SourceFile, Deprecated, Synthetic, Signature, and InnerClasses. -renamesourcefileattribute {string}       设置源文件中给定的字符串常量
3. Example

In the previous article we made a default confusion about the HelloWorld project.
Below, we're going to use configuration to avoid android-support-v4.jar confusion
Modify the Proguard-project.txt file and add the following code:

-libraryjars libs/android-support-v4.jar-dontwarn android.support.v4.**-keep class android.support.v4.** { *; }
解读:    -libraryjars libs/android-support-v4.jar    表示声明需要使用-libraryjars加上项目中使用到的第三方库    -dontwarn android.support.v4.**    ProGuard不要警告找不到android.support.v4.**这个包里面的类的相关引用    -keep class android.support.v4.** { *; }    保持android.support.v4.**这个包里面的所有类和所有方法而不混淆

We do this through Dex2jar, after viewing through Jd-gui

添加上述命令之后的项目

未添加上述命令之后的项目


We can find that after adding the corresponding parameters, there is no confusion within the android.support.v4.**

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Learning Tips---Android Code obfuscation (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.