Java code obfuscation Proguard

Source: Internet
Author: User

Official website Usagehttp://proguard.sourceforge.net/index.html#/manual/examples.html
Proguard main three parts functionreduce code, optimize code, and confuse code. The three-part feature can be configured in the configuration file without enabling this feature.
#Shrink Options
#不缩减代码
-dontshrink

#Optimization Options
#优化代码
-dontoptimize

#Obfuscate Options
#-does not confuse the input class file
#-dontobfuscate
usageClass specifications specification that defines the retention rules for classes, interfaces, enumerations, members, methods, and so on-keep options and-assumenosideeffects option.
Match * In classes, interfaces, enumerations, members, methods of use.
1. The name of the class referenced in the configuration file, such as XML, cannot be confused with renaming. Because the class needs to be loaded through the configuration file path.
For example, activity configuration in Android-keep public class * extends Android.app.Activity (the class name is preserved only, the class name does not confuse renaming, but its members and methods are confused) there are Activity, application, Service, Broadcastreceiver, Custom views such as ContentProvider, Fragment, Context, LinearLayout, view, and so on.
2. Keep Some of the content that comes with the SDK system.
-keepattributes exceptions,innerclasses,signature,deprecated,sourcefile,linenumbertable,*annotation*, Enclosingmethod
For example,-keepattributes *annotation* will retain the activity's @override annotated method OnCreate, OnDestroy method, and so on.
3. Resource class variables need to be preserved
-keepclassmembers class * *. r$* {
public static <fields>;
}
4. Keep all classes of third-party jar packages and their members and methods, such as {*;} All members and methods within the class are matched.
#jar Config
-dontwarn org.apache.log4j.**
-keep class org.apache.log4j.** {*;}
5. By default, Proguard will confuse all of the code, but there are several cases where the Java element name cannot be changed, otherwise it will cause a program error.
(1) We use the place of reflection.
(2) Our code relies on the interface of the system, such as the callback method called by the system code, which is the most complicated situation.
(3) is our Java element name is configured in the configuration file.
6. Handling of the class that is having the problem. -keep Usage Differences
-dontwarn default Proguard checks to see if each reference is correct, but there are often classes that are not used in third-party libraries and are not referenced correctly. If not configured, the system will error.
-keep the specified class and class members are reserved as portals.
-keepclassmembers the specified class member is reserved.
-keepclasseswithmembers the specified class and class members are retained, if the specified class member exists.
(1) Keep a class name from being confused
-keep public class Com.app.common.bean.Customer
(2) the reserved class and all its members are not confused
-keep public class Com.app.common.bean.Customer {*;}
Or
-keepclasseswithmembers class Com.app.common.bean.Customer {
<init>; #匹配所有构造函数
<fields>; #匹配所有成员
<methods>; #匹配所有方法
(3) Retain only the class name and some of its members are not confused
-keep public class Com.ebt.app.common.bean.Customer {
Static final<fields>;
private void get* ();
}
(4) Keep all classes under a package
-keep class com.google.** {*;}
7.proguard Problems and risks
Code obfuscation has the advantage of confusing optimizations, but it often leads to several problems
1, confusion, use to third-party library, you must tell Proguard do not check, or Proguard will error.
2, run error, when code can not be confused, we must be properly configured, or the program will run an error, this situation is the most problematic.

3, debugging suffering, error, error stack is confused after the code, I can not read.

8. Debugging methods for problems after confusion
(1) Print log, keep exception, source file line number information.
-printmapping Out.map
-renamesourcefileattribute sourcefile
-keepattributes Exceptions,sourcefile
(2) Determine which row is in which class is faulted.
You can also use log or System.out to print test logs before and after the line that might be faulted to determine which class is the specific error. To decide whether to keep.

Using Gradle to build an Android engineering configuration obfuscation method
minifyenabled true
Proguardfiles getdefaultproguardfile (' proguard-android.txt '), ' proguard-project.txt '

Scan the code to pay attention to the public, together to learn progress.



Java code obfuscation Proguard

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.