Obfuscation of Android code

Source: Internet
Author: User
2.3sdk has two new features:

1. when I first installed 2.3, I checked the SDK directory and found that a folder "proguard" was added under <sdk_path> \ tools, as shown in, I was wondering if Google had finally taken proguard into consideration. Theoretically, obfuscation of Java is acceptable, but the key lies in how to compile the proguard obfuscation script.

 

2. After sdk2.3 is used, a file "proguard. cfg" will be added to the new project compared with the previous one ". The proguard script is required for obfuscation.

For example:

 

The Code is as follows:

View plaincopy to clipboardprint?
-Optimizationpasses 5
-DontusemixedcaseclassnmEs 
-DontskipnonpubliclibraryClasses 
-Dontpreverify 
-Verbose 
-Optimizations! Code/simplification/arithmetic ,! Field /*,! Class/merging /* 
-Keep public class * extends Android. App. Activity 
-Keep public class * extends Android. App. Application 
-Keep public class * extends Android. App. Service 
-Keep public class * extends Android. content. broadcastreceiver 
-Keep public class * extends Android. content. contentprovider 
-Keep public class com. Android. Vending. Licensing. ilicensingservice 
-KeepclasseswithmembernamEs class *{ 
   Native <Methods>; 

-KeepclasseswithmembernamEs class *{ 
   Public <init> (android. content. Context, android. util. AttributeSet ); 

-KeepclasseswithmembernamEs class *{ 
   Public <init> (android. content. Context, android. util. AttributeSet, int ); 

-Keepclassmembers enum *{ 
   Public static ** [] values (); 
   Public static ** valueOf (java. lang. String ); 

-Keep class * implements android. OS. Parcelable { 
 Public static final android. OS. Parcelable $ Creator *; 
}
-Optimizationpasses 5
-DontusemixedcaseclassnmEs
-DontskipnonpubliclibraryClasses
-Dontpreverify
-Verbose
-Optimizations! Code/simplification/arithmetic ,! Field /*,! Class/merging /*
-Keep public class * extends android. app. Activity
-Keep public class * extends android. app. Application
-Keep public class * extends android. app. Service
-Keep public class * extends android. content. BroadcastReceiver
-Keep public class * extends android. content. ContentProvider
-Keep public class com. android. vending. licensing. ILicensingService
-KeepclasseswithmembernamEs class *{
   Native <methods>;
}
-KeepclasseswithmembernamEs class *{
   Public <init> (android. content. Context, android. util. AttributeSet );
}
-KeepclasseswithmembernamEs class *{
   Public <init> (android. content. Context, android. util. AttributeSet, int );
}
-Keepclassmembers enum *{
   Public static ** [] values ();
   Public static ** valueof (Java. Lang. String );
}
-Keep class * implements Android. OS. parcelable {
 Public static final Android. OS. parcelable $ creator *;
}

As you can see from the script, obfuscation retains the basic components inherited from activity, service, application, broadcastreceiver, contentprovider, and so on.

All native variable names and class names are retained. All classes Use constructors with fixed parameter formats, such as enumeration. (For more information, see examples and comments in <proguard_path> \ examples .)

Well, it's almost done. Let's take a look at how to generate a obfuscated APK. Please note that the new features of the SDK are available in the document, so the document is very important.

View the sdk2.3 documentation, as described in "enabling proguard" in the path "<androidsdk_path>/docs/GUIDE/developing/tools/proguard.html:

To enable proguard so that it runs as part of an ant or eclipse build, set the proguard. config property in the <project_root>/default. properties file. the path can be an absolute path or a path relative to the project's root.

Okay, so do it.

Add the following phrase "proguard. config = proguard. cfg" to "default. properties" of the project, for example:

 

In this way, the ADT obfuscation operation has been set. The next step is the normal packaging and signature ..

It is the Notepad that comes with the SDK Demo:

 

Note:
1. The package after obfuscation will be a little smaller than the package before obfuscation. Be sure to pay attention to this.
If the obfuscation fails, set proguard. config = proguard. change cfg to proguard. config = E: \ Mobile_Develop \ Google_Android \ publicGoldenBeach_new \ proguard. cfg is similar to the absolute path. Note that the folder name in the absolute path cannot contain spaces. If there are spaces, replace them "_".

 

2. when android uses proguard for obfuscation, you can use the built-in configuration file of the system to maintain most of the classes that need to be referenced externally, such as Activity and view extension, however, some external lib introduced in some cases may encounter various problems if obfuscated. If you do not want to confuse these packages, you must add

-Keep class packagename .**{*;}

In this way, the original class can be fully maintained.

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