Android Code Obfuscation

Source: Internet
Author: User

One, code obfuscation definition

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.

obfuscation is not just about protecting code, it also has the effect 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.

Second, confusion file proguard.cfg parameter detailed

<span style= "FONT-SIZE:14PX;"                                                      >-optimizationpasses 5 # Specifies the compression level of the Code-dontusemixedcaseclassnames                                                # Whether to use case mixed-dontskipnonpubliclibraryclasses # Is it confusing if third party jar-dontpreverify # is confusing when doing Pre-check if-verbose # is confused when logging-optimizations!code/simplif                               ication/arithmetic,!field/*,!class/merging/* # The algorithm used to confuse-keep public class * extends Android.app.Activity # Keep What classes are not confused-keep public class * extends Android.app.Application # Keep which classes are not mixed Xiao-keep public class * extends Android.app.Service # Keep What classes are not confused-keep public class * extends a Ndroid.content.BroadcastReceiver # Keep What classes are not confused-keep public class * extends Android.content.ContentPRovider # Keep What classes are not confused-keep public class * extends Android.app.backup.BackupAgentHelper # Keep Which classes are not confused-keep public class * extends Android.preference.Preference # Keep What classes are not confused-keep public class COM.A                                           Ndroid.vending.licensing.ILicensingService # Keep What classes are not confused-keepclasseswithmembernames class * { # Keep native method not to be confused native <methods>;} -keepclasseswithmembers class * {# Keep custom control classes from being confused with public <init> (Android Oid.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 </span>

Third, the method of code confusion

Depending on the version of the SDK, there are 2 different ways of confusing code, the information involved in the PROGUARD.CFG parameter details above is a confusing script under the lower version of the SDK , in fact, in the higher version of the SDK The principle and parameters of the confusion are similar to those of the lower versions, except that the confusing scripts are introduced in different versions of the SDK . Here's how:

  • Low version SDK , the project contains both proguard.cfg  And project.properties File, you only need to project.properties file Add at the end proguard. config= proguard.cfg to export the project again.
  • High version SDK , the project contains both Proguard-project.txt And  project.properties file, you need to proguard-project.txt Configuration of the following information in the file, and thenthen export the project to。 The following is a demonstration of a real file.

<span style= "FONT-SIZE:14PX;" ># This file was automatically generated by Android tools.# does not modify this file--YOUR changes would be erased!## Th Is file must are checked in Version Control systems.## to customize properties used by the Ant build system edit# "Ant.prop Erties ", 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</span>
Iv. Retention of certain packages is not confusing

The above configuration information is The contents of the project.properties file, the blue text is the configuration information we need to add during the code obfuscation, where: Sdk.dir the installation path for your 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.

<span style= "FONT-SIZE:14PX;" ># to enable Proguard in your project, edit project.properties# to define the Proguard.config property as described in That file.## ADD project specific Proguard rules here.# by default, the "The flags in" This file is appended to the flags specified # in ${sdk.dir}/tools/proguard/proguard-android.txt# You can edit the include path and order by changing the Proguard# Inc Lude project.properties.## For more details, see#   http://developer.android.com/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 JAVASCR IPT interface# Class:#-keepclassmembers class Fqcn.of.javascript.interface.for.webview {# public   *;#}</span >

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.