The function, use and bug analysis of "Turn" Proguard

Source: Internet
Author: User

Original address: http://blog.csdn.net/forlong401/article/details/23539123.

http://www.trinea.cn/android/proguard-use/

This article mainly proguard the function, the use and the bug analysis.
1. Proguard Effect
Proguard the ability to obfuscate, compress, and optimize code by removing useless code, renaming the class name, method name, and property name in code to obscure names, similar to JavaScript obfuscation and compression.
Compression and optimization make the APK package smaller after compilation.
Confusion can ensure that the code is hard to read after being recompiled, preventing reverse engineering. This is one of the reasons why we need to Proguard before the app is released.

2, the use of Proguard
(1). System Application:

In the Android.mk file under the project root directory, add

XHTML<textarea class="crayon-plain print-no" style="z-index: 0; opacity: 0; overflow: hidden; line-height: 15px !important;" readonly="" data-settings="dblclick">local_proguard_flag_files: = proguard.cfglocal_proguard_enabled: =</textarea> Full
12 LOCAL_proguard_flag_files := proguard. CFG LOCAL_proguard_enabled := full

This application does not need to set up a separate Proguard configuration file, the system will be compiled with a unified system proguard.cfg the application to Proguard, the system proguard.cfg in the system root directory \build\core.

(2). Non-system applications:
A. Open the Proguard switch

Configure Proguard in the Project.Properties file in the project root directory, adding the following code:

XHTML<textarea class="crayon-plain print-no" style="z-index: 0; opacity: 0; overflow: hidden; line-height: 15px !important;" readonly="" data-settings="dblclick">proguard.config=proguard.cfg</textarea>
1 Proguard. config=proguard. CFG

This way, before packaging the APK in release mode, the Proguard will process the application bytecode with proguard.cfg as the rule. For release mode, section C below will be introduced

B. Writing your own Proguard config file
The default is to confuse all code, if you need some confusion, you can modify the Proguard.cfg file yourself
Syntax and standard configuration for Proguard config visible: Proguard syntax and common proguard.cfg code Snippets

Note the following classes cannot be confused:
(1), the class that the reflection uses
(2), classes configured in Androidmanifest (subclasses of Activity, service, and framework classes are not confused by default)
(3), the class called in JNI

C. Introduction to Running Proguard and the files it generates
Proguard is automatically run when the APK is packaged in release mode, where the release mode refers to the ant release command or eclipse project->android tools- >export signed (unsigned) application package generates APK. In debug mode, Proguard is not called for faster debugging.

If the ant command is packaged, the Apk,proguard information file is stored in the <project_root>/bin/proguard folder, and if packaged with the Eclipse Export command, it is <project_root In the >/proguard folder. It contains the following files:

Mapping.txt represents a comparison of the code before and after the confusion, this file is very important. 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.
Dump.txt describes the internal structure of all class files within the APK
seeds.txt lists classes and members that are not confused
usage.txt lists code that is deleted in the source code that does not exist in the APK

For mapping.txt part of the content, and before and after the confusion of code comparison:

From this you can see that the code is mostly ABCDEFG after the confusion.

Note: develop the habit of preserving mapping.txt. Proguard will overwrite the original file each time it is run, so save the Mapping.txt every time you publish, so that you can retrieve the log when there is a problem with the version. Mapping.txt can be saved or put into code versioning depending on the version number or the name of the publication time.

D. Whether the Proguard has been successfully checked

You can check whether Proguard is successful by deserializing the APK, and if the success code is similar to the above, most of the class names and member names are shaped like a.b.c .... For anti-compilation Please refer to: Android APK anti-compilation

3, Proguard Confusion after the bug analysis
(1) bug in the code itself

The code in the bug tip is confusing after code, Class A. B. C ..., if you need to troubleshoot, you have to reverse the code snippet in the actual code according to the Mapping.txt file to solve the problem
PS: The $ representation in the code is the anonymous inner class, followed by outclassname$1 in the order of the Code, outclassname$2

(2) bugs caused by confusion
The application may cause some bugs because Proguard confuses the code that should not be confused, the most common is classnotfoundexception, and badparcelableexception, etc.
For ClassNotFoundException, a class is found based on the Mapping.txt file and then not confused in proguard.cfg

Java<textarea class="crayon-plain print-no" style="z-index: 0; opacity: 0; overflow: hidden; line-height: 15px !important;" readonly="" data-settings="dblclick">-keep class Packagename.classname {*;}</textarea>
1 -Keep class packagename. ClassName { *; }

For android.os.BadParcelableException:Parcelable protocol requires a Parcelable.creator object called Creator, Add in Proguard.cfg

Java<textarea class="crayon-plain print-no" style="z-index: 0; opacity: 0; overflow: hidden; line-height: 15px !important;" readonly="" data-settings="dblclick">-keep class * Implements android.os.Parcelable {public static final Android.os.parcelable$creator *;}</textarea>
123 -keep class * implements android. Os. Parcelable { Public static final android. Os. Parcelable$Creator *; }

For ANDROID.SUPPORT.V4 can ' t find superclass or Interface,can ' t find referenced method,proguard.cfg add

Java<textarea class="crayon-plain print-no" style="z-index: 0; opacity: 0; overflow: hidden; line-height: 15px !important;" readonly="" data-settings="dblclick">-libraryjars libs/android-support-v4.jar-dontwarn android.support.v4.**-keep class android.support.v4.** {*;} -keep interface android.support.v4.app.** {*;}</textarea>
1234 -libraryjars Libs/android-support-v4. Jar -dontwarn androidsupport. V4. **&NBSP;&NBSP;&NBSP;&NBSP; -keep class android. Support. V4. ** { * }&NBSP;&NBSP; -Keep interface android. Support. V4. App. ** { *; }

Proguard confusion after other issues are visible: http://proguard.sourceforge.net/index.html#manual/troubleshooting.html

You may also be interested in:

Proguard syntax and common proguard.cfg code Snippets

Android Performance Tuning Summary

Android system Download management Downloadmanager feature introduction and use Example

Android Download Management Downloadmanager enhancements and bug fixes

The function, use and bug analysis of "Turn" Proguard

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.