What is code obfuscation
Java is a cross-platform, interpreted language in which Java source code is compiled into an intermediate "bytecode" stored in a class file. Because of the cross-platform requirements, Java bytecode contains a lot of source code information, such as variable name, method name, and through these names to access variables and methods, these symbols with a lot of semantic information, it is easy to decompile the Java source code. To prevent this, we can use Java obfuscation to confuse Java bytecode.
Confusion is the re-organization and processing of the published program, so that the processed code and the pre-processing code to complete the same function, and the confusing code is difficult to decompile, even if the anti-compilation success is difficult to derive the true semantics of the program. The obfuscated program code, still in accordance with the original file format and instruction set, the execution result is the same as before, but the confusion of all the variables, functions, classes in the code to the name of the short English letter code, in the absence of the corresponding function name and program comments, even if it is anti-compilation, it will be difficult to read. At the same time confusion is irreversible, in the process of confusion, some information that does not affect the normal operation will be lost permanently, the loss of this information makes the program more difficult to understand.
The role of the obfuscation is not only to protect the code, it also has the function of simplifying the size of the compiled program. Because of the shortened variables and function names described above, and the reasons for missing some information, the size of the jar file can be reduced by approximately 25%, which makes sense for the current cost of the wireless network transmission is more expensive.
Confusing file proguard.cfg parameters
-optimizationpasses 5 # Specifies the compression level of the Code-dontusemixedcaseclassnames # Whether to use case mixed-dontskipnonpubliclibraryclasses # whether to confuse third-party jar-dontpreverify # When doing pre-calibration-verbose # whether to log-optimizations when confused!code/simplifica tion/arithmetic,!field/*, the algorithm used in!class/merging/* # obfuscation-keep public class * Extends Android.app.Activity # keeps what Class is not confused-keep public class * extends Android.app.Application # Keep What classes are not confused-keep public class * Exten DS Android.app.Service # keeps what classes are not confused-keep public class * extends Android.content.BroadcastRec Eiver # Keep What classes are not confused-keep public class * extends Android.content.ContentProvider # Keep what classes do not Be confused-keep public class * extends Android.app.backup.BackupAgentHelper # Keep What classes are not confused-keep public class * extends Android.preference.Preference # Keep What classes are not confused-keep public class com.android.vending.licensing.ILicensing Service # keeps what classes are not confused-keepclasseswithmembernames class * {# keeps Nativ E-method is not confused native <methods>;} -keepclasseswithmembers class * {# Keep custom control classes from being confused with public <iNit> (Android.content.Context, android.util.AttributeSet);} -keepclasseswithmembers class * {public <init> (Android.content.Context, android.util.AttributeSet, int); # Keep custom control classes from being confused}-keepclassmembers class * extends android.app.Activity {# Keep custom control classes from being confused with public voi D * (Android.view.View);} -keepclassmembers enum * {# keep Enum enum class not confused public static **[] VA Lues (); public static * * VALUEOF (java.lang.String);} -keep class * Implements android.os.Parcelable {# Keep Parcelable not confused public static final Android.os.parcelable$creator *;} -keep class MyClass; # Keep your defined classes from being confused
Method of code obfuscation
Depending on the version of the SDK, there are 2 different code obfuscation, the above proguard.cfg parameter in the details of the information is in the lower version of the SDK under the confusion of the script, in fact, in the high version of the SDK is confused with the principle and parameters are similar to the lower version, just in different confusing scripts are introduced differently in the SDK version environment. Here's how:
- low version SDK, the project contains both proguard.cfg and project.properties file, just under project.properties file end add proguard.config= proguard.cfg export.
- High version sdk, the project contains both proguard-project.txt and project.properties file, which needs to be Span style= "FONT-FAMILY:ARIAL,HELVETICA,SANS-SERIF; font-size:13px; " > proguard-project.txt file is configured with the following information, and then Then the project export. The following is a demonstration of a real file.
# This file was automatically generated by Android tools.# does not modify this file--YOUR changes would be erased!## this F Ile must is checked in Version Control systems.## to customize properties used by the Ant build system edit# "Ant.properti Es ", and override values to adapt the script to your# project structure.## to enable Proguard to shrink and obfuscate your Code, uncomment this (available Properties:sdk.dir, user.home): #proguard. config=${sdk.dir}/tools/proguard/ proguard-android.txt:proguard-project.txtproguard.config=${sdk.dir}/tools/proguard/proguard-android.txt: proguard-project.txt# Project target.target=android-16
The above configuration information is the contents of the project.properties file, and the blue text is the configuration information we need to add during the code obfuscation, where:sdk.dir is your installation path to the SDK on the current machine . If you want to keep the files under a package from being confused, you can include a statement that retains the corresponding package name in the Proguard-project.txt file.
# to enable Proguard in your project, edit project.properties# to define the PROGUARD.C Onfig property as described in, file.## ADD project specific Proguard rules here.# by default, the "flags in" this file is appended to flags specified# in ${sdk.dir}/tools/proguard/proguard-android.txt# You can edit the include path and Orde R by changing the proguard# include property in project.properties.## for more details, see# http://developer.android.co m/guide/developing/tools/proguard.html# ADD any project specific keep options Here:- Dontwarn com.cnki.android.cnkireader.** -keep class com.cnki.android.cnkireader.** {* ; }# If your project uses WebView with JS, uncomment the following# and specify the fully qualified class name to the JAVASC Ript interface# Class:#-keepclassmembers class Fqcn.of.javascript.interface.for.webview {# public *;#} /span>
Code obfuscation under Android programming