1. Obfuscation Overview
The obfuscation works by removing unused code and renaming classes, fields, and methods with obscure names, compressing, optimizing, and confusing the code, and generating a smaller. apk file, which is more difficult to reverse engineer and is an important means of protection.
The obfuscation is integrated into the Android build system, so you don't have to call it manually. The obfuscation will only run when the application is built in release mode, so you don't have to deal with confusing code when building the program in debug mode. This document describes how to enable and configure the obfuscation, and use the Trace (retrace) tool to decode the confusing stack trace information (stack traces).
2. The start-up confuses the device
When you create a new Android project, a proguard.cfg file is created automatically in the project's root directory. This file defines how the obfuscation optimizes and confuses your code, so it's important to know how to customize it to your needs. The default profile only covers the usual situation, so you might want to edit it depending on your needs. The next step is to configure the obfuscation by customizing the obfuscation configuration file.
In order to enable the obfuscation as part of the ant or Eclipse build process, you can set the Proguard.config property in the <project_root>/default.properties file. The path can be either an absolute path or a relative path to the project root directory.
If you let the Proguard.cfg file in the default location (the project's root directory), you can specify the location like this: Proguard.config=proguard.cfg, thedebug mode obfuscation is invalid and is valid through export.
3. The meaning of the resulting file after the obfuscation
(1). Dump.txt
Describes the internal structure of all class files in the. apk package.
(2). Mapping.txt
Lists the mappings between the source code and the Obfuscated class, method, and property name. This file is useful for bug reports that are made after the build, because it translates the confusing stack trace information into classes, methods, and member names in the source code. For more information, see Decoding the obfuscated stack trace information.
(3). Seeds.txt
Lists the classes and members that are not confused.
(4). Usage.txt
Lists the code stripped from the. apk.
Note: These files are overwritten by the latest files each time they are built in release mode. So every time you publish a program, save a copy of these files in order to counter-confuse bug reports generated from the build. These resulting proguard files need to be saved.
4. Confusing code stack trace information
When confusing the code and outputting a stack debug message, the names of these methods are confusing, although debugging can be difficult. Fortunately, whenever the obfuscation is running, it will output to the file <project_root>/bin/proguard/ Mapping.txt, the file contains a mapping from the original class, method, and property name to the obfuscated name, which should be kept in mind each time it is packaged. Use the <sdk_root>/tools/proguard/retrace. sh script command to convert the confusing stack debug information into a stack debug message that can be obfuscated before.
retrace.bat|retrace.sh [-verbose] mapping.txt [<stacktrace_file= program bug generated stack information file;]
$sh/applications/adt/sdk/tools/proguard/bin/retrace.sh -verbose/users/zf/documents/workspace/gridtest/ Proguard/mapping.txt/users/zf/desktop/bug.txt
5. Common Obfuscation Codes
(1). Android Engineering Confusion
# This was a configuration file for proguard.# http://proguard.sourceforge.net/index.html#manual/usage.html# 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 no all optimizations performed by# Proguard works on all Versions of Dalvik. The following flags turn# off various optimizations known to has issues, but the list may not# is complete or up to date. (the "Arithmetic" optimization can be# used if you is 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, the other file has# flags To turn off Optimization).-dontusemixedcaseclassnames-dontskipnonpubliclibraryclasses-verbose-keepattributes *Annotation*-keep public class Com.google.vending.licensing.ilicensingservice-keep public class com.android.vending.licensing.ilicensingservice# for native methods, see http://proguard.sourceforge.net/manual/ Examples.html#native-keepclasseswithmembernames class * {native <methods>;} # Keep setters in views so it animations can still work.# see Http://proguard.sourceforge.net/manual/examples.html#beans -keepclassmembers public class * extends Android.view.View {void set* (* * *); Get* ();} # We want to keep methods in Activity that could is used in the XML attribute onclick-keepclassmembers class * extends and roid.app.Activity {public void * (Android.view.View);} # for enumeration classes, see Http://proguard.sourceforge.net/manual/examples.html#enumerations-keepclassmembers 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 <fields>;}
(2). Introduction of Gson Package
# #---------------Begin:proguard configuration for Gson ---------- # Gson uses generic type information stored in A class file when working. 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 com.google.gson.stream.** {*;} -keep class com.google.gson.examples.android.model.** {*;} -keep class com.google.gson.** {*;} # #---------------End:proguard configuration for Gson
Android_proguard Code Obfuscation