Android Java obfuscation (proguard)

Source: Internet
Author: User
Proguard Introduction

ProguardIsSourceForgeOn a well-known open-source project. Official Website:Http://proguard.sourceforge.net/.

JavaThe bytecode is generally very easy to decompile. For good protectionJavaSource code, We often compileClassFile for obfuscation.ProguardThe main function is obfuscation. Of course, it can also reduce the size and optimize the bytecode, but those are secondary features for us.

ReferenceProguardThe official introduction is as follows:

Proguard is a free Java class file shrinker, optimizer, obfuscator, and preverifier. it detects and removes unused classes, fields, methods, and attributes. it optimizes bytecode and removes unused instructions. it renames the remaining classes, fields, and methods using short meaningless names. finally, it preverifies the processed code for Java 6 or for Java Micro Edition.


Android eclipse Development Environment and proguard

In For Android 2.3 Previously, obfuscation Android CodeManual addition only Add Proguard It is inconvenient to implement code obfuscation. While 2.3 Later, Google This tool has been added SDK . Specific path: SDK \ tools \ proguard . When you create a new Android In the project directory, Proguard Configuration File Proguard. cfg . In other words, through simple configuration Elipse Direct use in projects Proguard Obfuscation Android Project.

The specific obfuscation steps are very simple. First, we need to go to the project description fileDefault. Properties, Add a sentence, enableProguard. As follows:

1 # This file is automatically generated by Android tools.
2 # Do not modify this file -- your changes will be erased!
3 #
4 # This file must be checked in version control systems.
5 #
6 # To customize properties used by the ant build system use ,
7 # " Build. Properties " , And override values to adapt the script to your
8 # Project structure.
9 # Indicates whether an APK shoshould be generated for each density.
10 Split. Density = False
11 # Project target.
12 Target = Android- 10
13 Proguard. config = Proguard. cfg
14

In this way,ProguardYou can use it. When we passAndroid toolsExportApplication Package,ProguardIt is automatically enabled to optimize obfuscation of your code.

 


After the export is successful, you can decompile to see the obfuscation effect. Some class names, method names, and variable names are converted into meaningless letters or numbers. The obfuscation is successful!


Proguard. cfg Configuration

If you think about the obfuscation result, you will find that some classes, methods, variables, and other names provided to the outside are changedProgramIts functions cannot be implemented normally. SoProguardHow can we know what can be changed, but what cannot be changed?

This depends onProguard. cfgFile.AndroidAutomatically Generated by default in the projectProguard. cfgForAndroidThe configuration file is opened. The content is roughly as follows:

1 -Optimizationpasses 5
2 -Dontusemixedcaseclassnames
3 -Dontskipnonpubliclibraryclasses
4 -Dontpreverify
5 -Verbose
6 -Optimizations! Code/simplification/Arithmetic , ! Field /* , ! Class/merging /*
7 -Keep public class * extends Android. App. Activity
8 -Keep public class * extends Android. App. Application
9 -Keep public class * extends Android. App. Service
10 -Keep public class * extends Android. content. broadcastreceiver
11 -Keep public class * extends Android. content. contentprovider
12 -Keep public class * extends Android. App. Backup. backupagenthelper
13 -Keep public class * extends Android. Preference. Preference
14 -Keep public class com. Android. Vending. Licensing. ilicensingservice
15
16 -Keepclasseswithmembernames class *{
17 Native <Methods> ;
18 }
19
20 -Keepclasseswithmembernames class *{
21 Public <init> (Android. content. Context , Android. util. attributeset) ;
22 }
23
24 -Keepclasseswithmembernames class *{
25 Public <init> (Android. content. Context , Android. util. attributeset , INT) ;
26 }
27
28 -Keepclassmembers Enum *{
29 Public static ** [] Values () ;
30 Public static ** valueof (Java. Lang. String) ;
31 }
32
33 -Keep class * implements Android. OS. parcelable {
34 Public static final Android. OS. parcelable $ creator * ;
35 }
36

It mainly retains Activity , Application , Service , Broadcastreceiver , Contentprovider , Backupagenthelper , Preference And Ilicensingservice . These subclasses may be called externally.

In addition, it retainsNativeMethods and constructorXMLConstructed class (generallyViewInValuesAndValueofStatic methods, inheritanceParcelable.

In an actual projectGoogleThe automatically generated configuration cannot be used for obfuscation. Therefore, we often need to write someProguardConfiguration. This information is available on the official website.Manual-> usage. You can study it.


References

《ProguardOfficial"

《For Android 2.3GenerationCode obfuscationProguardTechnical Introduction"

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.