Android Java obfuscation (ProGuard)

Source: Internet
Author: User

ProGuard Introduction
ProGuard is a well-known open-source project on SourceForge. Official Website: http://proguard.sourceforge.net /.

Java bytecode is generally very easy to decompile. To protect the Java source code, we often confuse compiled class files. ProGuard is mainly used for obfuscation. Of course, it can also reduce the size and optimize the bytecode, but those are secondary features for us.

The official ProGuard article is introduced 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
Before Android 2.3, obfuscation of Android code can only be implemented by manually adding proguard, which is inconvenient. After 2.3, Google has added this tool to the SDK tool set. Specific path: SDKoolsproguard. When creating a new Android project, a proguard configuration file proguard. cfg appears under the root path of the project directory. That is to say, we can use ProGuard to confuse the Android project directly in our elipse project through simple configuration.

The specific obfuscation steps are very simple. First, add a sentence in the project description file default. properties to enable ProGuard. 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, Proguard can be used. When we export the Application Package through Android Tools normally, Proguard is automatically enabled to optimize and confuse 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 changed, the functions of the program cannot be implemented normally. So how does Proguard know what can be renamed, But what cannot be changed?

This is configured by using the proguard. cfg file. The default automatically generated proguard. cfg in the Android project has been configured for the general situation of Android. We open this configuration file. 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 child classes inherited from Activity, Application, Service, BroadcastReceiver, ContentProvider, BackupAgentHelper, Preference, and ILicensingService. These subclasses may be called externally.

In addition, it retains the classes containing native methods and the classes constructed by constructor from xml (generally the subclass of View) the values and valueOf static methods in the enumerated type, and the cross-process data class that inherits Parcelable.

In an actual project, the configuration automatically generated by Google may not be competent for our obfuscation work. Therefore, we often need to write some ProGuard configurations by ourselves. This information is detailed in Manual-> Usage on the official website. You can study it.

 

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.