At present, some tools, such as Apktool,dextojar, can be used to decompile our Android installation package to get the source code. In order to reduce being cracked, resulting in source code leakage, the program was stolen by others, and so on. We need to confuse the code, the Android SDK provides us with the Progrard tool that can confuse the code (usually renamed with a meaningless name), and remove unused code, optimize and compress the program, which can increase the difficulty you want. The most recent project I did was to configure the confusing configuration, so I researched it and shared it here.
How to enable the Proguardant project and Eclipse Project Enablement methods
Add code to the project's project.properties file
Proguard.config=proguard.cfg//proguard.cfg configuration file for Proguard
PROGUARD.CONFIG=/PATH/TO/PROGUARD.CFG//path is not in the project root, fill in the actual path
After filling in this configuration, the release will be packaged in accordance with our configuration to be confused, note that in our usual debug will not be confused.
Gradle Project (and Android Studio)
Configuring in Build.gradle
android {buildtypes {release {Runproguard true Proguardfi Les Getdefaultproguardfile (' proguard-android.txt '), ' some-other-rules.txt '//proguardfile ' some-other-rules.txt ' Configure a single file So}}}
As shown in the above code, we can use Runproguard true to turn on, and configure it with a confusing configuration, you can configure multiple files or individual files.
The Android SDK has provided us with two default profiles, which we can use, Proguard-android.txt and Proguard-android-optimize.txt.
Proguard Configuration
It says Android gives us two default profiles, in which we can see some of his syntax. Described in this section.
Retention options (Configure what is not processed)
-keep {Modifier} {class_specification} protects the specified class file and members of the class
-keepclassmembers {Modifier} {Class_ Specification} protects members of the specified class, and if this class is protected they will protect the members of the specified class and class for better
-keepclasseswithmembers {class_specification} protection. 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 names of members of the specified class (if they are not removed in the compression step)
-keepclasseswithmembernames {class_specification} protects the names of the members of the specified class and class, If all the specified class members are present (after the compression step)
-printseeds {filename} lists the members of the class and class-keep the list of options, standard output to the given file
Compression
-dontshrink does not compress the input class file
-printusage {filename}
-whyareyoukeeping {class_specification}
Optimization
-dontoptimize does not optimize the input class file
-assumenosideeffects {class_specification} Optimizations assume the specified method without any side effects
-allowaccessmodification allows access to and modification of members of classes and classes with modifiers when optimized
Confuse
-dontobfuscate does not confuse the input class file
-obfuscationdictionary {filename} with the keyword in the given file as the name of the method to be confused
- Overloadaggressively obfuscation when applying intrusive overloading
-useuniqueclassmembernames to determine the member name of a uniform obfuscation class to increase confusion
-flattenpackagehierarchy { Package_name} RePack all the renamed packages and put them in a given single package
-repackageclass {package_name} repack all the renamed class files in a given single package
- Dontusemixedcaseclassnames confusion does not produce a variety of class names
-keepattributes {attribute_name,...} protects a given optional property, such as Linenumbertable, Localvariabletable, SourceFile, Deprecated, synthetic, Signature, and innerclasses.
-renamesourcefileattribute {String} sets the string constant given in the source file
The following file name, class name, or package name can be replaced with a placeholder
? Represents a character
Can match multiple characters, but if it is a class, it does not match the package name in front of it
* multiple characters can be matched, matching the previous package name.
Activity,service,provider, Receviter, etc. in Android manifest files in Android cannot be confused. Some of the view configurations in XML are not confusing, as is available in the default configuration provided by Android.
Proguard output files and their usefulness
After confusing, will give us output some files, in Gradle mode is under the <project_dir>/build/proguard/ directory, Ant is in the < Project_dir>/bin/proguard directory, Eclipse is built in <project_dir>/proguard video. The
has the following files:
+ dump.txt describes the internal structure between all the class files in the apk file.
+ mapping.txt Lists the original classes, methods, and mappings between the field names and the code after the confusion.
+ seeds.txt lists non-confusing classes and members
+ Usage.txt lists the code that was removed from the APK
When the release version of our released program bug, you can use the above file (special time Mapping.txt) file to find the original location of the error, bug modification. At the same time, there may be errors at the beginning of the Proguard configuration, or through the error log, depending on the files, find which files should not be confused, thereby modifying the Proguard configuration.
Note: These files will be overwritten after the re-release is compiled, so it is best to keep a copy of the configuration file without having to eat the release program.
Debugging Proguard after confusing the program
The above said the output of several files, we can use when changing the bug, through the mapping.txt, through the mapping relationship to find corresponding classes, methods, fields and so on.
In addition, the Proguard file contains the retrace script, you can restore a confused stack trace information into a readable information, window Retrace.bat,linux and Mac is retrace.sh, in <sdk_root The >/tools/proguard/ folder. The syntax is:
retrace.bat|retrace.sh [-verbose] mapping.txt [<stacktrace_file>]
For example:
retrace.bat -verbose mapping.txt obfuscated_trace.txt
If you do not specify <stacktrace_file>, the retrace tool will read from the standard input.
Proguard configuration for some common packages
Here are some of the third-party packages I've used in my project that require a separate configuration of the obfuscation configuration
SHARESDK Confusion Note
-keep class android.net.http.SslError-keep class android.webkit.**{*;}-keep class cn.sharesdk.**{*;}-keep class com.sina.**{*;}-keep class m.framework.**{*;}
Gson confusing configuration
-keepattributes *Annotation*-keep class sun.misc.Unsafe { *; }-keep class com.idea.fifaalarmclock.entity.***-keep class com.google.gson.stream.** { *; }
Umeng SDK Obfuscation Configuration
-keepclassmembers class * {public <init> (org.json.JSONObject);} -keep class Com.umeng.**-keep public class com.idea.fifaalarmclock.app.r$*{public static final int *;} -keep public class Com.umeng.fb.ui.ThreadView {}-dontwarn com.umeng.**-dontwarn org.apache.commons.**-keep public Class * extends Com.umeng.**-keep class com.umeng.** {*;}
Regarding the configuration aspect, I wrote insufficiently detailed, may go to see the reference material second, Proguard Official document. You are also welcome to communicate the use of problems and experience.
Data reference:
1.http://proguard.sourceforge.net/
2.http://developer.android.com/tools/help/proguard.html
Original address: http://blog.isming.me/blog/2014/05/31/use-proguard/, welcome reprint, reproduced please indicate the source.