Use of Proguard tools in Eclipse and Android source code

Source: Internet
Author: User

Due to work needs, these two days and colleagues in the study of the use of Proguard tools under Android, by looking at the Android website on the introduction of the tool and other relevant information on the network, coupled with their own hands-on practice, is a basic understanding. The following will be their own understanding and understanding of a brief note, there are objections or puzzled, you can directly leave a message.

What is the Proguard tool?

Proguard is a free tool provided by Android that removes some useless code from the project, or uses semantically cryptic names to rename classes, fields, and functions in the code, to compress, optimize, and obfuscate the code. Specifically, using the Proguard tool, you can achieve the following two purposes:

    • Removed the part of the code that was not called in the source file, maximizing the size of the bytecode file, making the resulting apk file smaller.
    • The use of semantically confusing naming replaces classes, fields, and functions in code so that others cannot decompile to get the source code, which protects the code.

I see a lot of people on the net according to the role of Proguard tools, directly called it "obfuscation Code tool", this article also temporarily used the word abbreviation.

For more understanding, you can refer to the official document of the Proguard tool address: http://developer.android.com/tools/help/proguard.html

Integration and use environment of proguard tools

In fact, Proguard tools are already integrated into our Android system, so there is no need for users to integrate manually. One thing to note, however, is that Proguard only works when the program is in release mode, whereas in debug mode it is not possible to confuse the code with Proguard.

According to the specific use environment of proguard, I divide in the Eclipse tool and the Android source code two kinds of compiling environment to discuss Proguard's use method.

Use of Proguard in the Eclipse environment

Take My computer's android4.0 environment as an example, when we create a new project in Eclipse, or import an existing project (to ensure that the current project has no syntax errors), at the root of the project, two proguard of confusing files are generated automatically: Proguard-project.txt and Proje Ct.properties (in the old version of ADT, only a file called Proguard.cfg is generated). Let's take a look at the file project.properties:

 This file--YOUR changes'll be erased!  "ant.properties" This (available Properties:sdk.dir, user.home): #proguard. config =${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt# project Target.target= Android-16

Look at the following comment: To enable Proguard to shrink and obfuscate your code, uncomment this (available Properties:sdk.dir, user.home), meaning means to let the proguard to compress and confuse the code, this sentence to remove the comment! So, we just need to cancel the following comment, as follows:

 This (available Properties:sdk.dir, user.home):p roguard.config=${sdk.dir}/tools/proguard/ Proguard-android.txt:proguard-project.txt

We carefully look at this part of the code: This place is by setting the Proguard.config property to specify the obfuscation code of the configuration file to the native SDK directory under the Proguard-android.txt file, as well as to develop a confusing personalization profile for the current project (under Eclipse) root directory P Roguard-project.txt file, and this file is exactly the other file that we just saw that was automatically generated in the root directory!

Actually open this place, we can already confuse the code, but here to note: Can not attempt to run eclipse in the running as and Debug as menu to generate confusion code, must be shown by the method to export the APK, of course, you can choose "signature" or "Do not sign":

After this one-step operation, the code obfuscation is complete. So how can we test that we really confuse the code? First, we were able to see the new production of a folder Proguard in the root of the project, which contained four files with the following contents:

    • Dump.txt: Describes the internal structure of all class files in the APK. (describes the internal structure of all the class files in the. apk file)
    • Mapping.txt: Lists the original classes, methods, and names that are mapped with the confusing code. (Lists the mapping between the original and Obfuscated class, method, and field names. )
    • Seeds.txt: Lists the classes and methods that are not confused. (Lists the classes and members of that is not obfuscated)
    • Usage.txt: Lists the code that was removed in congapk. (Lists the code that is stripped from the. apk)

At the same time using the anti-compilation software to decompile the newly generated apk will find that the class name, methods and variables, etc., have become simple A, B, C, D and other meaningless letters, so that the purpose of confusion is achieved:

But in the actual use of the process, we will find that the current apk, some methods and classes are for external use, and when the name is confused, external calls will be error, then how to solve this problem? At this point we need to use the confusing personalization file Proguard-project.txt We just mentioned, in which to configure classes, methods and variables that do not require confusion. For a detailed configuration of the confusing file, see the last heading below for details.

Proguard use in Android source environment

In the Google release of the Android source code, the face of so many codes and file directories, at this time how to confuse the code and configuration confused files?

The default in Android is to confuse the code Proguard off, in the alps/build/core/proguard.flags has the following sentence, meaning will not be confused by default, do not need code deletion, we will comment on this sentence, the code to confuse the role of compiling.

# Don ' t obfuscate. We only need dead code Striping.-dontobfuscate

It can be said that the code in the Android project is confused with the total switch, however, after commenting the above code, the entire project is already a code confusion? No, there's a file alps/build/core/package.mk, in this file:

ifndef Local_proguard_enabledifneq ($ (filter user Userdebug, $ (target_build_variant)),)     default  for user & Userdebug build    #LOCAL_PROGUARD_ENABLED:=fullendifendif

Remember : When we need to confuse the entire project with code, we take the #LOCAL_PROGUARD_ENABLED: =full comment out and become a valid macro. If you don't want to confuse the entire project code, but rather confuse it with a module, don't move the code here.

It is recommended that the whole project be new again, and then you can set up and customize the file for the specific apk file. Let's take alps/packages/apps/music as an example of how to confuse a particular module with code:

In the music directory, we see a usually not too concerned about, but today must be very concerned about the file name: Proguard.flags, right, this file is music confusion configuration file, you can open to see (some places do not have this file, users can create their own manually, The best name is also called Proguard.flags,android under the default is the name). Of course, setting up the configuration file is not enough, also need to be in the same directory of ANDROID.MK set the following two sentences:

local_proguard_enabled  : = fulllocal_proguard_flag_files:= proguard.flags

This is the only way to make the obfuscation code valid and to associate the confusing configuration file. (some modules do not have these two sentences, you manually add)

Conversely, if the user has opened the whole project in ALPS/BUILD/CORE/PACKAGE.MK to confuse the compiled control points, and in the case of a module do not want to confuse the compilation? This is simple, set the ANDROID.MK under the module to **local_proguard_enabled: = disabled**.

In this way, we generated by the MM compiled code of the APK, or the new project generated after the burning machine code, has been added to the corresponding configuration of the confusion code.

After anti-compilation, in addition to the proguard.flags in the customization of the code does not need to be confused, the other is confused, is the Android music module confusion after the anti-compilation results:

Confusing the configuration of files

In the actual use of the process, we will find that the current apk, some methods and classes are for external use, and when the name is confused, external calls will be error, then how to solve this problem? At this point we need to configure the confusing personalization file Proguard-project.txt (in Eclipse Environment) or proguard.flags (Android source Environment) in which to configure the classes, methods and variables that do not need to be confused. About the specific configuration method of confusing files, you can go to search, I have a section of the network to share the configuration code, this configuration code retains the project file activity, application, Service, Broadcastreceiver, ContentProvider, Backupagenthelper, preference, and Ilicensingservice subclasses, and retains all the native variable names and class names, all of which are part of the constructor that sets the fixed parameter format. enumeration, and so on, to prevent external calls to error, you can draw on to later configure their own files:

-optimizationpasses 5-Dontusemixedcaseclassnames-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 * Extends Android.app.backup.backupagenthelper-keep public class * extends Android.preference.Preference- Keepclasseswithmembernames class * {native <methods>;} -keepclasseswithmembers class * {public <init> (Android.content.Context, android.util.AttributeSet);} -keepclasseswithmembers class * {public <init> (Android.content.Context, android.util.AttributeSet, int);} -keepclassmembers class * extends android.app.Activity {public void * (Android.view.View);}    -keepclassmembers enum * {public static **[] values (); public static * * VALUEOF (java.lang.String);} -keep class * Implements android.os.Parcelable {public static final Android.os.parcelable$creAtor *;} 

Use of Proguard tools in Eclipse and Android source code

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.