Using Proguard puzzle Android code

Source: Internet
Author: User

Currently some tools such as Apktool,dextojar are able to decompile our Android installation package. Get the source code. In order to reduce by others to crack, resulting in source code leaks, programs are stolen in lieu of code, and so on. We need to confuse the code. The Android SDK provides us with the Progrard tool. The ability to obfuscate the code (usually renamed with a meaningless name), and to remove unused code, optimizes and compresses the program. This will add to the difficulty you want. Recently I did the project, is I go to configure the confusion configuration, so study a bit, here to share.

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 folder, fill in the actual path

After filling out this configuration, the release will be packaged in accordance with our configuration to confuse, note that in our usual debug is not 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 seen in the code above. We are able to use runproguard true to turn on, and configure it to be confusing configuration, to be able to configure multiple files or individual files.

Two default profiles have been provided for us in the Android SDK. We were able to use them, proguard-android.txt and Proguard-android-optimize.txt.

Proguard Configuration

The above mentioned Android provides us with two default configuration files, among them. We can see some of his grammar. This section is a descriptive narrative.

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, assuming that the class is protected and that they are protected by the better
-keepclasseswithmembers {class_specification} protects the members of the specified class and class. However, the condition is that all specified classes and class members are to be present.
-keepnames {class_specification} protects the names of the specified classes and members of the class (assuming they are not removed in the compression step)
-keepclassmembernames {class_specification} Protects the names of members of the specified class (assuming they are not removed in the compression step)
-keepclasseswithmembernames {class_specification} protects the names of the members of the specified class and class, Assuming that all the specified class members are present (after the compression step),
-printseeds {filename} lists the-keep options for the members of the class and class. 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} If the specified method is optimized. No, no matter what side effects
-allowaccessmodification Consent to access and modify members of classes and classes with modifiers

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 add confusion
-flattenpackagehierarchy { Package_name} wraps all the renamed packages again and puts them in a given single package
-repackageclass {package_name} again wraps 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 attribute, such as linenumbertable, Localvariabletable, SourceFile, Deprecated, synthetic, Signature, and innerclasses.
-renamesourcefileattribute {String} sets the string constant given in the source file

The following file names, class names, or package names can be replaced with placeholders
? Represents a character
Ability to match multiple characters, but assumed to be a class that does not match the package name in front of it
*
ability to match multiple characters, matching the previous package name.

The Activity,service,provider in Android manifest file. Receviter, and so can not 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 the confusion, we will output some files, in the Gradle mode under the <project_dir>/build/proguard/ folder, Ant is in the <project_dir>/bin/ Proguard folder. Eclipse is built in the <project_dir>/proguard folder image.
The following files are available respectively:
+ Dump.txt describes the internal structure of all types of files in the apk file.
+ Mapping.txt lists the original classes. method, and the mapping between the field name and the obfuscated code.
+ Seeds.txt lists classes and members that are not confused
+ usage.txt lists the code removed from the APK

When a bug occurs when a program for release version number is released. Be able to find the wrong original location through the file (special time mapping.txt) file, make bug change.

At the same time, there may be errors at the beginning of the Proguard configuration. Also be able to pass the error log. Based on these files, find which files should not be confused, thereby altering the configuration of the Proguard.

Note: Once again release is compiled. These files will be overwritten, so it is best to keep a copy of the configuration file without having to eat the advertised program.

Debugging Proguard after confusing the program

Above said the output of several files, we can use when changing the bug, through the mapping.txt, through the mapping relationship to find the corresponding classes, methods, fields and so on.

In addition, the Proguard file includes the retrace script, capable of restoring a garbled stack trace to a readable message, window Retrace.bat,linux and Mac are retrace.sh, <sdk_root The >/tools/proguard/ directory. The syntax is:

retrace.bat|retrace.sh [-verbose] mapping.txt [<stacktrace_file>]

Like what:

retrace.bat -verbose mapping.txt obfuscated_trace.txt

Assuming you don't specify <stacktrace_file>, the retrace tool reads from the standard input.

Some proguard configurations that often use packages

The following is a list of some of the third-party packages that I used in my project need to be individually configured for confusing 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.** {*;}  

About configuration aspects. I am not specific enough to read the second article, Proguard official documents.

You are also welcome to communicate the use of problems and experience.

Data references:
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.

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Using Proguard puzzle Android code

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.