Android Proguard Detailed

Source: Internet
Author: User

General business projects require code encryption, and Android provides a way to code obfuscation, as described below:

First, Introduction

1. Introduction

The obfuscation compresses, optimizes, and confuses code by removing code that has never been used and renaming classes, fields, and methods with obscure names. The result is a smaller. apk file, which is more difficult to reverse engineer. Therefore, when your application is sensitive to security (high demand), such as when you authorize an application, the obfuscation is an important protection tool.

The obfuscation is integrated into the Android build system, so you don't have to call it manually. The obfuscation will only run when the application is built in release mode, so you don't have to deal with confusing code when building the program in debug mode. Making the obfuscation run is optional, but recommended.

2. Precautions

When creating a project in a new version of ADT, the mixed files are no longer proguard.cfg, but Project.Properties and Proguard-project.txt.

If you need to make a global mix of items, you only need to do one step:

The project.properties in the

The "#" of "# Proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt" will be removed.


If some code can not be confused, such as the need to add the so file, need to call the inside of the method, then call JNI Access so file method can not be mixed code. When exporting, you may not get an error. But when running on the phone, you need to call so file, it will be reported that such a method cannot be found. We need to use proguard-project.txt at this time.

Second, enable Proguard in eclipse

1. Determine if the Proguard folder exists in the Android SDK directory
such as D:\Environment\android-sdk-windows\tools\proguard
2. Configuration
1) Check to see if there is a proguard.cfg (or proguard-project.txt) file under the root of the project, and if it does not exist (the current project is 2.3 previous), you need to create a new 2.3 Android project from which the project will PROGUARD.CF g files are copied to your project root directory without changes (unless you understand how to customize your own obfuscation strategy), continue with the steps below.
2) If present, locate the default.properties[under the project root if you are using ADT14 or a later version, Then there is no default.properties file in the project, only project.properties file, find project.properties file can] file, add a configuration in the file: proguard.config= Proguard.cfg
This completes the configuration

3. Grammar rules

# Android Original obfuscation template #-------------------------------------#----------------------------------# can be executed by a specified number of optimizations #-op    timizationpasses N #-----------------------------------optimizationpasses 5 #----------------------------------#  Confusion does not produce a variety of class names #-dontusemixedcaseclassnames #----------------------------------#-dontusemixedcaseclassnames # ----------------------------------# Specifies not to ignore non-public Library classes #-dontskipnonpubliclibraryclasses #-------------------------  ---------#-dontskipnonpubliclibraryclasses #----------------------------------# no pre-check #-dontpreverify # ----------------------------------#-dontpreverify #----------------------------------# Output Build Info #-ver bose® #-----------------------------------Verbose #----------------------------------# Optimization Options # Optimizat ions {optimization_filter} #-----------------------------------Optimizations!code/simplification/arithmetic,! field/*,!class/merging/*-KEEP public class * extends Android.app.activity-keep public class * extends Android.app.application-keep public class * Extends Android.app.service-keep public class * extends Android.content.broadcastreceiver-keep public class * Extend  s android.content.contentprovider-keep public class * extends Android.app.backup.backupagenthelper-keep public class * Extends Android.preference.preference-keep public class Com.android.vending.licensing.ilicensingservice-keepclasse  Swithmembernames class * {native <methods>; #-----------------# Modify Modify Merge #------------------Keep public class * extends Android.view.View {public &      Lt;init> (Android.content.Context);      Public <init> (Android.content.Context, Android.util.AttributeSet);      Public <init> (Android.content.Context, Android.util.AttributeSet, int.);  public void set* (...);      }-keepclassmembers enum * {public static **[] values (); public static * * VALUEOF (Java.lang.String);  }-keep class * Implements android.os.Parcelable {public static final android.os.parcelable$creator *;  #--------------------------# Protection Type-keepattributes Description # Exceptions, Signature, Deprecated, SourceFile, SourceDir, Linenumbertable, Localvariabletable, # localvariabletypetable, synthetic, Enclosingmethod, RuntimeVisibleAnnotations , Runtimeinvisibleannotations, # runtimevisibleparameterannotations, Runtimeinvisibleparameterannotations, and Annotationdefault #---------------------keepattributes * *-libraryjars <java.home>/lib/rt.jar #------------   ----------# does not compress the specified file #-dontshrink #-----------------------Dontshrink #----------------------# does not optimize the specified file # -dontoptimize #------------------------Dontoptimize #-----------------------# do not confuse the specified file #-dontobfuscate # -----------------------#-----Confusing package path--------repackageclasses '-flattenpackagehierarchy '-target 1.6 #---- ----The following is the use of Roboguice-1.1.2.jar and GuIce-2.0-no_app.jar functions need to be protected by fields and classes related---------keep class Com.google.inject.binder-keepclassmembers class * {@com. Goo  Gle.inject.Inject <init> (...);   }-keepclassmembers class * {void * (**on*event); }-keepclassmembers Class * *.  r$* {public static <fields>; # #------The jar package required to compile-libraryjars D:/dev_rc/android-sdk-windows/add-ons/addon_google_apis_google_inc_11/libs/map S.jar #------Protect Google Third party jar package, interface effects-----------keep class android.support.v4.**-dontwarn android.support.v4.** #  ------Protect Baidu Address jar package---------Keep class com.baidu.mapapi.** {*;} -dontwarn com.baidu.mapapi.** #---When packaging ignores warnings for the following classes---Dontwarn Com.classpackage.AA #-keepnames class * Implements JAV A.io.serializable #---------Protect field names in all entities-----------Keepclassmembers class * Implements Java.io.Serializable {&lt  ;fields>;  } #---------All method names in the protection class-------------keepclassmembers class * {public <methods>;   }

Reference: http://huangyuan.iteye.com/blog/1472732

If you have any questions, please leave a message, reproduced the source.

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.