Android_Proguard code obfuscator

Source: Internet
Author: User
Tags xml attribute

Android_Proguard code obfuscator

1. obfuscators Overview

Is an important protection method.

Obfuscators are integrated into android build systems, so you do not have to call them manually. At the same time, obfuscators run only when building applications in release mode, so you do not have to deal with obfuscation code when building a program in debug mode. This document describes how to enable and configure obfuscators, and decode the stack trace information (stack traces) by using the trace (retrace) tool.

2. Enable obfuscators

After you create an Android project, a proguard. cfg file is automatically created under the root directory of the project. This file defines how obfuscators optimize and confuse your code, so it is very important to know how to customize according to your needs. The default configuration file only covers the general situation, so you may need to edit it as needed. The following content is about how to configure the obfuscator by customizing the obfuscator configuration file.

To enable the obfuscator as part of the Ant or Eclipse build process, you can In the/default. properties file, set the property of proguard. config. The path can be an absolute path or a relative path of the Project root directory.

If you want the proguard. cfg file to be in the default location (the root directory of the project), you can specify the location as follows: proguard. config = proguard. cfg,Invalid obfuscation in debug mode, Which is valid through export.

3. Meanings of files generated after obfuscators


The internal structure of all class files in the pai.apk package.


Lists the mappings between source code and obfuscated classes, methods, and attribute names. This file is useful for bug reports after construction, because it translates obfuscated stack trace information into classes, methods, and member names in the source code. For more information, view the decoded stack trace information.


List unobfuscated classes and members.


Extract the code from .apk.

Note: These files will be overwritten by the latest files each time they are built in release mode. Therefore, each time you release a program, you must save a copy of these files to prevent obfuscation of bug reports generated during build. You need to save the generated Proguard files.

4. obfuscated code stack trace information

When code is obfuscated and a stack debugging information is output, these methods are confused. Although debugging can be performed, debugging becomes difficult. Fortunately, every time the obfuscator runs, it will output to the file In/bin/proguard/mapping.txt, the file contains the ing from the original class, method, and attribute name to the name after obfuscation. You should save the file every time you pack the package. Use /Tools/proguard/ RetraceThe. sh script command can convert the stack debugging information after obfuscation to the stack debugging information before obfuscation.

Retrace. bat | retrace. sh [-verbose] mapping.txt [ ]

$sh /Applications/ADT/sdk/tools/proguard/bin/  -verbose /Users/zf/Documents/workspace/GridTest/proguard/mapping.txt /Users/zf/Desktop/bug.txt

5. Common obfuscation code
(1). Android engineering Obfuscation

# This is a configuration file for ProGuard.# Optimizations: If you don't want to optimize, use the# proguard-android.txt configuration file instead of this one, which# turns off the optimization flags.  Adding optimization introduces# certain risks, since for example not all optimizations performed by# ProGuard works on all versions of Dalvik.  The following flags turn# off various optimizations known to have issues, but the list may not# be complete or up to date. (The "arithmetic" optimization can be# used if you are only targeting Android 2.0 or later.)  Make sure you# test thoroughly if you go this route.-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*-optimizationpasses 5-allowaccessmodification-dontpreverify# The remainder of this file is identical to the non-optimized version# of the Proguard configuration file (except that the other file has# flags to turn off optimization).-dontusemixedcaseclassnames-dontskipnonpubliclibraryclasses-verbose-keepattributes *Annotation*-keep public class public class For native methods, see class * {    native 
   ;}# keep setters in Views so that animations can still work.# see public class * extends android.view.View {   void set*(***);   *** get*();}# We want to keep methods in Activity that could be used in the XML attribute onClick-keepclassmembers class * extends {   public void *(android.view.View);}# For enumeration classes, see enum * {    public static **[] values();    public static ** valueOf(java.lang.String);}-keep class * implements android.os.Parcelable {  public static final android.os.Parcelable$Creator *;}-keepclassmembers class **.R$* {    public static 

(2) introduce the Gson package
##---------------Begin: proguard configuration for Gson  ----------  # Gson uses generic type information stored in a class file when working with fields. Proguard  # removes such information by default, so configure it to keep all of it.  -keepattributes Signature    # Gson specific classes  -keep class sun.misc.Unsafe { *; }  -keep class** { *; }  -keep class** { *; }   -keep class** { *;}    ##---------------End: proguard configuration for Gson  ---------- 

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