Android code obfuscation tool Proguard learning, androidproguard

Source: Internet
Author: User

Android code obfuscation tool Proguard learning, androidproguard
Overview Proguard code obfuscation tool: supports code redundancy compression, code optimization, and code obfuscation. The main application in Android is obfuscation of code: Class Name, method name, Field name into a, B, c, 1, 2, 3 and other difficult to read and understand names, to prevent reverse engineering and decompilation.
Use Proguard to enable Eclipse: There are two files under the project root path: project.propertiesand proguard-project.txt in the project. the following is a description in 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 cancel the comments of proguard. config to enable it.
Under AndroidStudio: There are two files under the project root path: build. gradle and the proguard-rules.pro have the following snippets in build. gradle, which enables proguard.

android{......  buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }}
Codes that cannot be obfuscated
System Interface
Jni Interface
Other components, such as four main components and views
Third-party class libraries such as android-support
Configuration syntax
-Include {filename}: Read the Configuration Parameter-basedirectory {directoryname} from the specified file and specify the base directory as the file name-injars {class_path, war, ear, and directory-outjars {class_path} specify the jar, war, ear, and directory name to be output after processing-libraryjars {classpath} specify the application jar to be processed, the library file-dontskipnonpubliclibraryclasses required by war, ear, and directory specifies that non-public library classes are not ignored. -Dontskipnonpubliclibraryclassmembers specifies that the visible Library Class Members of the package are not ignored. Keep option-keep {Modifier} {class_specification} to protect the members of the specified class files and class-keepclassmembers {modifier} {class_specification, if these classes are protected, they will be better protected-keepclasseswithmembers {class_specification} protects the members of the specified classes and classes, but the condition is that all specified classes and Class Members must exist. -Keepnames {class_specification} protects the names of members of the specified class and class (if they are not compressed and deleted in the step) -keepclassmembernames {class_specification} protects the names of members of the specified class (if they are not removed in the compression step)-keepclasseswithmembernames {class_specification} protects the names of members of the specified class and class, if all specified class members are present (after the compression step)-printseeds {filename} lists the Member-keep options of the Class and Class, standard output to a given File compression-dontshrink does not compress the input class file-printusage {filename}-whyareyoukeeping {class_specification} Optimization assume that the specified method, no side effects-classes and class members with modifiers can be accessed and modified during allowaccessmodification Optimization-dontobfuscate class files without obfuscation-printmapping {filename}-applymapping {filename} Reuse confusion added by ing-obfuscationdictionary {filename} use the keyword in the given file as the name of the method to be obfuscated-during overloadaggressively obfuscation, the application intrusive overload-useuniqueclassmembernames determines the name of the same Obfuscation -flattenpackagehierarchy {package_name} repacks all renamed packages and places them in a given single package-repackageclass {package_name} repacks all renamed class files and places them in a given package-dontusemixedcaseclassnames obfuscated -keepattributes {attribute_name ,...} protects specified optional attributes, such as LineNumberTable, LocalVariableTable, SourceFile, Deprecated, Synthetic, Signature, and InnerClasses. -renamesourcefileattribute {string}: Set the given string constant in the source file.
Application Instances (for more configuration examples, see the examples directory under the sdk's proguard folder) keep all local native methods not obfuscated. This is important when we develop ndk.
-Keepclasseswithmembernames class *{
Native <methods>;
}
All methods starting with set and get are retained.
-Keepclassmembers public class * extends android. view. View {
Void set *(***);
* ** Get *();
}
# We want to keep methods in Activity that cocould be used in the XML attribute onClick
The method parameter we keep in the activity is the view method. In this way, writing onClick in 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 are my package names.
This is what I wrote at the beginning.
-Keepclassmembernames class **********************. Model {
Long mId;
}
Later, I checked many pro files under the examples folder under the proguard folder. We recommend that you refer to the android. pro file for writing.And discover their commonalities, This is not the basic type.
Retain 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.** { *;}
Use Proguard to delete Log Code
-assumenosideeffectsclass android.util.Log {    public static *** e(...);    public static *** w(...);    public static *** wtf(...);    public static *** d(...);    public static *** v(...);}
Demo instance:
-Ignorewarnings # ignore warnings, avoid certain warnings during packaging-optimizationpasses 5 # specify the compression level of the code-dontusemixedcaseclassnames # whether to use case-sensitive hybrid-dontskipnonpubliclibraryclasses # whether to use pre-check-verbose when obfuscation # # Check whether logs are recorded during obfuscation-optimizations! Code/simplification/arithmetic ,! Field /*,! Class/merging/* # algorithm used for obfuscation-libraryjars libs/treecore. jar-dontwarn android. support. v4. ** # by default, proguard checks whether each reference is correct. However, third-party libraries often have classes that are not used and are not correctly referenced. If not configured, the system reports an error. -Dontwarn android. OS. **-keep class android. support. v4. ** {*;} # keep classes that are not Obfuscated-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 methods not obfuscated native <methods> ;} -keepclasseswithmembers class * {# Keep the custom control class not obfuscated with public <init> (android. content. context, android. util. attributeSet);}-keepclasseswithmembers class * {# Keep the custom control class not obfuscated with public <init> (android. content. context, android. util. attributeSet, int);}-keepclassmembers class * extends android. app. activity {// keep the class member public void * (android. view. view);}-keepclassmembers enum * {# Keep The enum class from being confused public static ** [] values (); public static ** valueOf (java. lang. string);}-keep class * implements android. OS. parcelable {# Keep Parcelable not to be confused public static final android. OS. parcelable $ Creator *;}-keep class MyClass; # keep the class defined by yourself not confused

Analysis of exceptions after running proguard 1. <project_root>/bin/proguard folder analysis:
Mapping.txt
Indicates the code comparison table before and after obfuscation. This file is very important. If your code is uploaded to the bug, the logprompt indicates the code behind the Code. If you want to locate the code, you can use mapping.txt to push back the code. Use mapping.txt to restore the exception Stack: Use retrace. bat to locate 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
Every release should keep it so that logs can be called up for troubleshooting when there is a problem with the version. It can be saved or put into the code version control based on the version number or release time name.
Dump.txt
Describes the internal structure of all class files in the apk.
Seeds.txt
Lists the classes and members that are not obfuscated.
Usage.txt
Lists the codes that are deleted from the source code and do not exist in the apk.


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

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.