Code obfuscation using proguard on Android

Source: Internet
Author: User

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:

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

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

  1. -Libraryjars libs/a. Jar
  2. -Libraryjars libs/B. Jar
  3. -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:

  1. Warning: COM. Google. Android. Maps. mapview: Can't Find referenced class com. Android. mkstubs. stubber. methodstubber
  2. Warning: COM. Google. Android. Maps. mapview $1: Can't Find referenced class com. Android. mkstubs. stubber. methodstubber

Copy code

That is:

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

  1. -Dontwarn % class_full_name %

Copy code

The preceding example is as follows:

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

  1. Warning: Library class android. content. res. xmlresourceparser extends or implements program class org. xmlpull. v1.xmlpullparser
  2. 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.

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.