Android Code obfuscation tool Proguard Learning

Source: Internet
Author: User
Tags xml attribute

Overview of the Proguard Code obfuscation tool: code can be de-redundant compression, code optimization, code obfuscation and so on. The main application in Android is to confuse the code: the class name, method name, field name into difficult to read and understand names such as a,b,c or three-way, in order to prevent reverse engineering and be anti-compiled reading source.
Enable eclipse with Proguard: There are two files under the project root path: Project.Properties and proguard-project.txt have such a passage in Project.Properties: # to enable Proguard to shrink and obfuscate your code, uncomment this (available Properties:sdk.dir, user.home):
#proguard. Config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
Set the Sdk.dir variable and then cancel the Proguard.config comment to enable it.
Androidstudio: There are two files under the project root path: Build.gradle and Proguard-rules.pro have the following fragment in Build.gradle, that is, enable Proguard.
  android{... Buildtypes {        Release {            minifyenabled false            proguardfiles getdefaultproguardfile (' Proguard-android.txt ') ), ' Proguard-rules.pro '}}    
Code that cannot be confused
System interface
JNI interface
Other such as four components, view, etc.
Third-party class libraries such as Android-support
Configuration syntax
-include {filename} reads the configuration parameter from the given file-basedirectory {directoryname} specifies the underlying directory for the later relative file name-injars {Class_path} specifies the application to be processed Jar,war,ear and directory-outjars {Class_path} Specifies the name of the Jar,war,ear and directory to output after processing,-libraryjars {classpath} Specifies the applications to be processed jar,war,ear and directories required The library file-dontskipnonpubliclibraryclasses specifies that the non-public libraries class is not to be ignored. -dontskipnonpubliclibraryclassmembers specifies that the members of the Library class that the package is visible are not ignored. The Preserve option-keep {Modifier} {class_specification} protects the specified class file and the members of the class-keepclassmembers {Modifier} {class_specification} protects the specified class of If this class is protected they will protect it better-keepclasseswithmembers {class_specification} protects the specified class and members of the class, but the condition is that all the specified classes and class members are to be present. -keepnames {class_specification} protects the names of the members of the specified class and class (if they are not removed in the compression step)-keepclassmembernames {class_specification} protects the specified class from The name of the member (if they are not removed in the compression step)-keepclasseswithmembernames {class_specification} protects the name of the specified class and members of the class, if all the specified class members are present (after the compression step)- printseeds {filename} lists the members of the class and class-keep option, standard output to the given file compression-dontshrink does not compress the input class file-printusage {filename}-whyareyoukeepin The g {class_specification} optimization-dontoptimize does not optimize the input class file-assumenosideeffects {class_specification} optimizations assume that the specified method, without any side effects, is-allowaccessmodification optimized to allow access to and modify members of classes and classes that have modifiers to confuse-dontobfusca Te does not confuse the input class file-printmapping {filename}-applymapping {filename} reuse mappings Add confusion-obfuscationdictionary {filename} using the given file The keyword as the name of the method to be confused-overloadaggressively obfuscation when applying intrusive overloads-useuniqueclassmembernames to determine the uniform confusing class member names to increase confusion-flattenpackagehierar chy {package_name} repack all the renamed packages and place them in a given single package-repackageclass {package_name} repack all the renamed class files in a given single package-DONTUSEMIXEDCASECLA    Ssnames confusion does not result in a variety of class names-keepattributes {attribute_name,...} Protects the given optional attributes, such as linenumbertable, Localvariabletable, sourcefile, Deprecated, synthetic, Signature, and innerclasses. -renamesourcefileattribute {String} sets the given string constant in the source file
Application examples (more configuration examples can be seen in the SDK's Proguard folder under the examples directory) retain all local native methods are not confused. This is important when we do the NDK development.
-keepclasseswithmembernames class * {
Native <methods>;
}
Keep all of the set and get start methods.
-keepclassmembers public class * extends Android.view.View {
void set* (* * *);
Get* ();
}
# We want to keep methods in Activity that could is used in the XML attribute OnClick
The method parameter that we keep in the activity is the method of view, so that we write the onclick in the XML will not be affected.
-keepclassmembers class * extends Android.app.Activity {
public void * (Android.view.View);
}
-keepclassmembernames class *******************. model{
Java.lang.Long mId;
}
The above series of * numbers is my package name.
This is what I wrote at the beginning.
-keepclassmembernames class *******************. model{
Long mId;
}
After viewing many of the pro files under the examples folder under the Proguard folder, they found their commonalities, It is not possible to find that the basic type is not written like this.
Preserve third-party class libraries:
-libraryjars/libs/achartengine-1.1.0.jar-dontwarn Achartengine.**-keep class achartengine.** {*;} -libraryjars/libs/android-support-v4.jar-dontwarn Android.support.v4.**-keep class android.support.v4.** {*;}
To delete a log code using Proguard
-assumenosideeffectsclass Android.util.Log {public    static * * * e (...);    public static * * * W (...);    public static * * * WTF (...);    public static * * * * d (...);    public static * * * V (...);}
Demo Example:
-ignorewarnings# ignores warnings and avoids packaging when certain warnings appear-optimizationpasses the compression level of the specified code-dontusemixedcaseclassnames#                            Whether to use case blending-dontskipnonpubliclibraryclasses# whether to confuse third-party jar-dontpreverify # When doing pre-calibration-verbose # when confusing the algorithm used when logging-optimizations!code/simplification/arithmetic,!field/*,!class/merging/* # is confused-libra Ryjars Libs/treecore.jar-dontwarn android.support.v4.** #缺省proguard will check that each reference is correct, but there are often some classes that are not used in third-party libraries and are not referenced correctly. If not configured, the system will error.  -dontwarn Android.os.**-keep class android.support.v4.** {*;} # Keep What classes are not confused-keep class com.baidu.** {*;} -keep class vi.com.gdi.bgl.android.**{*;} -keep class android.os.**{*;}  -keep interface android.support.v4.app.** {*;} -keep public class * Extends android.support.v4.**-keep public class * extends Android.app.fragment-keep public class * Extends Android.app.activity-keep public class * extends Android.app.application-keep public class * extends Android.app. Service-keep public class * extends Android.content.BroadcastReCeiver-keep public class * extends Android.content.contentprovider-keep public class * extends Android.support.v4.widget-keep public class * extends Com.sqlcrypt.database-keep public class * extends Com.sqlcrypt.database.sqlite-keep public class * extends Com.treecore.**-keep public class * extends de.greenrobot.dao.* *-keepclasseswithmembernames class * {# Keep Native method not to be confused native <methods>;} -keepclasseswithmembers class * {# Keep custom control classes from being confused with public <init> (Android.content.Context, Android.util.AttributeSe t);} -keepclasseswithmembers class * {# Keep custom control classes from being confused with public <init> (Android.content.Context, Android.util.AttributeSe t, int);} -keepclassmembers class * extends Android.app.Activity {//Keep class member public void * (Android.view.View);}    -keepclassmembers enum * {# keep Enum enum class not confused public static **[] values (); public static * * VALUEOF (java.lang.String);} -keep class * Implements android.os.Parcelable {# keep Parcelable not be confused public static final Android.os.parcelable$creatoR *;}                              -keep class MyClass; # Keep your defined classes from being confused

Proguard Post-run analysis exception tracking 1, <project_root>/bin/proguard folder analysis:
Mapping.txt
This file is very important for the comparison of the code before and after the confusion. If your code is confusing and generates a bug, the log hint is a confusing code, and if you want to locate the source code, you can reverse it by mapping.txt. using Mapping.txt to restore exception stacks: Using Retrace.bat in <sdk_root>/tools/proguard/bin
retrace.bat|retrace.sh [-verbose] mapping.txt [<stacktrace_file>]for example:retrace.bat-verbose mapping.txt Obfuscated_trace.txt
Keep it up every time you publish it to facilitate the issue of this version of the log for troubleshooting, it can be based on the version number or the name of the publication time to save or put into the code version control.
Dump.txt
Describes the internal structure of all class files within the APK.
Seeds.txt
Lists the classes and members that are not confused.
Usage.txt
Lists the code in the source code that is deleted in the APK without being present.


Reference: http://blog.csdn.net/banketree/article/details/41928175
http://my.oschina.net/zxcholmes/blog/312627
http://blog.csdn.net/binyao02123202/article/details/18940715

Android Code obfuscation tool Proguard Learning

Related Article

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.