Proguard is a free tool for compressing, optimizing, and confusing Java bytecode files, which can remove useless classes, fields, methods, and properties. You can delete unused annotations to maximize the optimization of bytecode files. It can also use short, meaningless names to rename existing classes, fields, methods, and properties. The result is a smaller jar, apk ..., and more difficult to decompile.
The Proguard is already integrated by default in the Android build environment, but only in release mode will the compilation run. In this state, it will automatically check whether the Proguard.config property is set when it is used with the ant release or Eclipse's Export Wizard, and if set, Proguard will automatically process the program's bytecode before it is made into a package. In debug mode, it is not triggered because the confusing code makes debugging more cumbersome.
Preparatory work:
1. Switch to release mode
Eclipse default Java Compile environment is debug mode, switch to release mode just right click on the project, select Properties, in the "Properties for ProjectName" window, "Java Compile" and " Classfile Generation "Debug option removed, click on" Apply ", click" OK "to close the window to take effect.
2, Prepare KeyStore
Android rules that all programs must be signed or not installed. So there will be a signature option in the process of confusing packaging, which requires keystore. If the keystore already exists, or ADT has been automatically generated with Keytool in the process, it does not need to be regenerated, and if not, the Keytool in the Jdk/bin directory can generate one yourself, as in the following example:
CMD code
- D:\Program Files\java\jdk1.6.0_17\bin>keytool-genkey-alias android.keystore-keyalg rsa-validity 20000-keystore Android.keystore
Where the parameter-validity is valid for the certificate number of days, here we write 10,000 days larger. There is no echo when you enter the password (even if you lose it) and backspace, tab and so on are the password content, this password is required for the. apk file signature.
To set the Proguard.config property:
When you create an Android project, the Proguard.cfg file is automatically created at the root directory, which defines how Proguard optimizes and confuses the code. Its default configuration covers only a few common scenarios, but it also satisfies the basic requirements, and for further customization, refer to the relevant configuration documentation.
By default, the Proguard.config property is not set and, if enabled, is set in the Default.properties file under the project root, and its path can be either a relative path or an absolute path.
The form of writing can be such as:
Default.properties Code
- Proguard.config=proguard.cfg
You can also place the Proguard.cfg file elsewhere, and then specify the path:
Default.properties Code
- Proguard.config=/path/to/proguard.cfg
Confusing packaging:
In release mode, there are several ways to export:
1. Right-click on Project->android Tools->export signed Android package to generate signed packages,
2. Right-click Project-->export Select Export Type
3. Ant Release
After the confusion succeeds, the Proguard folder is generated in the root directory of the project or in the Bin folder under the root directory, in addition to the specified type of obfuscation package, which contains four files of Dump.txt, Mapping.txt, Seeds.txt, and Usage.txt.
Dump.txt //Describe the internal structure between all class files in the. apk file
Mapping.txt //Lists the original class, method and field names, and the mappings between the code after the confusion. This file is important, and you can use it to translate the obfuscated code when you receive a bug report from the release release.
Seeds.txt //lists classes and members that are not confused
Usage.txt //lists the code removed from the. APK
Configuration Proguard:
In some cases, the default configuration in the Proguard.cfg file is sufficient. However, there are situations where proguard is difficult to analyze correctly, and it may delete code that it deems unnecessary, but is actually what your program needs.
For example:
Classes referenced only in the Androidmanifest.xml file
Methods that are called by JNI
Fields and methods for dynamic referencing
The default proguard.cfg file tries to overwrite the general situation, but it is possible that you will encounter an exception such as ClassNotFoundException, which happens because Proguard removed the entire class.
You can fix the error caused by Proguard Removing the code, just add a line "-keep" to the Proguard.cfg file. For example:
Java code
- -keep public class <MyClass>
There are some options and suggestions when using the-keep option, so it is highly recommended that you read the Proguard manual to learn more about custom profiles. "Overview of Keep Options" and "Examples section" will be very useful
Android's Proguard obfuscation device