Android Proguard uses the experience and encounters a number of problems as well as solution ideas.

Source: Internet
Author: User
Tags xml attribute

today decided to use the obfuscation tool Proguard for the project. So I started to find out where Proguard was placed in the SDK, and sure enough, I found the Proguard folder in the Tools folder in the SDK directory.

Open the Android project you want to confuse and locate the Project.Properties file.
after add on proguard.config=android.pro
Now let me explain this, the android.pro behind is the Proguard configuration file, mainly to tell Proguard how to confuse the file So where is this configuration file?

This file is the android.pro behind me. That's what I did, copy the file to the project, and then change the name to Android.pro.
so that is to tell you that the name of this file does not require hard, as long as the correct configuration file.

Then began to sign the file immediately, and then self-compiled files, found inside are ABCD class name, can not help but very happy.

immediately after the installation began, sure enough to start the error. Here we analyze the source of the error in order to correctly start adding our own configuration code to the configuration file. Some of the machines are directly in the crash, then we can not see the error report, some of the system comes with report reports, we could see related stack errors. So how to solve the crash of direct crashes, there is no stack log situation? We can connect the machine to the computer, see the device in Eclipse, and then let it crash, then we can also see the crash in the Logcat.

here we have to figure out that we're not in debug mode, so we're going to make the same mistake.

the back is unkonw source. Because we have no related code associated with it, it is different from the development pattern and is not visible in the first few lines. But fortunately we can know what the name of the method has been abnormal. So we can decompile the obfuscated code, view and compare the code without confusion. Discover the problem, and then we can add our own confusing configuration. Next, I want to illustrate the problems encountered, and the solution of the ideas for your reference.

1. Resolve case One: Take a look at some of the code below.

Public abstract class Model {@Column (name = "Id") private Long mId = null;//The following function is the private Field Getidfield inside another class (class< ;? > Type) {if (Type.equals (Model.class)) {try {return Type.getdeclaredfield ("MId");} catch (Nosuchfieldexception e) {log.e ("impossible!", E);}} else if (type.getsuperclass () = null) {return Getidfield (Type.getsuperclass ());} return null;}

If we are directly confused, then the mid will certainly become a kind of ABCD and other words.
The reflection method appears in the following method.

Type.getdeclaredfield ("MId");

so we'll make an error after the confusion. and returns a null, the author then reported a null pointer exception, the main function of the program is to find the specified class, discover its ID, and put a class of that nature into the collection.
Well, we found this error. Let's start by adding the following configuration:
-keepclassmembernames class *******************. model{
Java.lang.Long MId;
}


-keepclassmembernames class *******************. model{
long mId;
}
later viewed Proguard Folders below the examples folder under the many pro files, found their commonalities, found that not the basic type can not be written.

later, after confusion, a folder Proguard was found in the project. This folder is a related log file that is generated during the obfuscation process.
you can then find out in the Mapping.txt file what Proguard is confusing to you and what it retains. I tried a search on the model class above to see if mid was confusing or reserved.

***********. Model-COM.A.D:
Java.lang.Long mid-, mid

the results are searched for these statements
you can see that the model class has been mixed up into COM.A.D. And we want to keep the mid, and it's really not confusing.

2. Resolve Case Two:
because the author's project uses the Fastjson framework, it is found that an exception occurred when parsing data from the server.
we all know that the member variables in the JSON data have specific meanings, such as {username: ' xiaoming ', Password: ' dddddddd '};
and I wrote the model, inside the corresponding username must be confused. So we're going to parse the JSON data, and certainly don't want it to be confused.
so we can add the following in the configuration file:
-keep class com.xxxx.json.model.**{
*;
}
keep all the entity classes under the JSON package.

Two solution cases are described above: for more configuration examples, see the examples directory under the Proguard folder of the SDK.

next explain several configuration parameters to deepen your understanding of the Proguard configuration :

Keep all local native methods from being confused. This is important when we do the NDK development.
-keepclasseswithmembernames class * {
    native <methods>;
"


Keep all of the set and get start methods.
-keepclassmembers public class * extends Android.view.View {
" Span style= "FONT-SIZE:14PX;" >   void set* (* * *);
   *** get* ();
}


# We want to keep methods in Acti Vity that could is used in the XML attribute OnClick

The method parameter that we keep in the activity is the method of view, so that we write the onclick in the XML will not be affected.
-keepclassmembers class * extends Android.app.Activity {
Public void * (Android.view.View);
}

The following is attached to the Proguard configuration explanation, which I collected from the Internet.

-dontwarn Default Proguard checks to see if each reference is correct, but there are often classes that are not used in third-party libraries and are not referenced correctly. If not configured, the system will error. -keep the specified class and class members are reserved as portals. -keepclassmembers The specified class member is reserved.

-keepclasseswithmembers the specified class and class members are retained, if the specified class member exists

Retention Options -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 be better protected
-keepclasseswithmembers {class_specification} protects the specified class and members of the class, but 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 (if they are not removed in the compression step)
-keepclassmembernames {class_specification} protects the names of the members of the specified class (if they are not removed in the compression step)
-keepclasseswithmembernames {class_specification} protects the name of a member 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 options, standard output to the given file
Compression -dontshrink does not compress the input class file
-printusage {filename}
-whyareyoukeeping {class_specification}
Optimized -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
confusing -dontobfuscate does not confuse the input class file
-printmapping {filename}
-applymapping {filename} reuse mappings Add confusion
-obfuscationdictionary {filename} uses the keyword in the given file as the name of the method to be confused
-overloadaggressively obfuscation when applying intrusive overloads
-useuniqueclassmembernames Determining the member names of a unified obfuscation class to increase confusion
-flattenpackagehierarchy {package_name} repack all renamed packages and place them in a given single package
-repackageclass {package_name} repack all renamed class files in a given single package
-dontusemixedcaseclassnames Confusion does not produce a variety of class names
-keepattributes {attribute_name,...} Protects the given optional attributes, such as linenumbertable, Localvariabletable, sourcefile, Deprecated, synthetic, Signature, and innerclasses.
-renamesourcefileattribute {String} sets the given string constant in the source file
Finally, thank you for your reading.



Android Proguard uses the experience and encounters a number of problems as well as solution ideas.

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.