After the Android 2.3 SDK is released, Google has added proguard to Android SDK tools. proguard is a pair. java files are obfuscated with code to a certain extent. It is very convenient to use proguard. If there are no other external jar packages in your project. in the properties file, add a line:
- Proguard. Config = proguard. cfg
Copy code
Code, and then export the APK through Android tools (right-click the project name). If the SDK version used by the project is earlier than 2.3, you only need to run the % android_dir %/tools/lib directory, copy proguard. export the APK file to the root directory of the project.
This is normal, that is, there is no third-party jar package. If there is a third-party jar package, open the proguard. cfg file for editing and add the following code to it:
- -Libraryjars % lib_jar_path %
Copy code
A few jar packages are added several times. For example, the libs directory of the project contains three jar packages: A. jar, B. jar, and C. jar:
- -Libraryjars libs/a. Jar
- -Libraryjars libs/B. Jar
- -Libraryjars libs/C. Jar
Copy code
In this way, you can use Android tools to export the APK.
In addition, there are some special circumstances that may cause exceptions in the export, depending on the specific exception, modify the proguard. cfg file.
For example, the following exception occurs:
- Warning: COM. Google. Android. Maps. mapview: Can't Find referenced class com. Android. mkstubs. stubber. methodstubber
- Warning: COM. Google. Android. Maps. mapview $1: Can't Find referenced class com. Android. mkstubs. stubber. methodstubber
Copy code
That is:
- Warning: % class_full_name %: Can't Find referenced Class % class_full_name %
Copy code
In this exception, add the following code in the proguard. cfg file:
- -Dontwarn % class_full_name %
Copy code
The preceding example is as follows:
- -Dontwarn com. Google. Android. Maps .*
Copy code
In this case, modify the proguard. cfg file. In other special cases, modify the imported jar package as follows:
- Warning: Library class android. content. res. xmlresourceparser extends or implements program class org. xmlpull. v1.xmlpullparser
- Warning: Library class android. View. layoutinflater depends on program class org. xmlpull. v1.xmlpullparser
Copy code
This is because the referenced jar package contains the xmlpull class library, and the Android system class library already contains the xmlpull class library. This causes confusion, the solution is to remove the existing classes that conflict with the system library. The conflicting classes can be seen in the console output.
You can use-keep to specify classes, methods, and variables that you do not want to confuse. For details, refer to the method in the proguard. cfg file.