Android Studio Code obfuscation (Are you really going to confuse it)

Source: Internet
Author: User

First, preface

Today to pack new products, suddenly forget the confusion of the parameters is how to write, although previously also confused, but the specific configuration of the parameter code some do not remember, so decided to spend some time to write a blog record, convenient to find and their own memories.

Second, the Android Studio code confusion Basic configuration

In the project directory, locate the Proguard-rules.pro file, which is the file you want to write a confusing configuration of, and write the obfuscation rules in this file

Of course, before this, you also need to refer to this obfuscation file in your module's Build.gradle file:

Above in the Proguard-android.txt file, which is the system default for confusing files, specifically in: /sdk/tools/proguard/directory, which contains the most basic Android confusion, generally do not need to change, we need to configure the project in the app's Proguard-rules.pro file

Iii. Rules of Confusion

1. Basic rules

Two common obfuscation commands, note that a star indicates that only the class name under the package is kept, and the class name under the sub-package is still confused; the two stars hold the class names under the package and the included sub-packages;

-keep class cn.hadcn.test.**-keep class cn.hadcn.test.*

Using the above method to maintain the class, you will find that the class name is not confused, but the specific method and variable name or change, at this time if you want to maintain the class name, and want to keep the contents of the content is not confused, we need the following methods

-keep class com.example.bean.** { *; }

Based on this, we can also use Java's basic rules to protect certain classes from being confused, such as the Java rules that we can use extend,implement and so on. The following example avoids the confusion of all classes that inherit activity

# Keep The four components we use, custom application, etc. these classes are not confused # because these subclasses can be called externally-keep publicClass *ExtendsAndroid.App.Activity-keep PublicClass *ExtendsAndroid.App.Appliction-keep PublicClass *ExtendsAndroid.App.Service-keep PublicClass *ExtendsAndroid.Content.Broadcastreceiver-keep PublicClass *ExtendsAndroid.Content.Contentprovider-keep PublicClass *extends android. App.backup. Backupagenthelper-keep public class * extends android. Preference. preference-keep public class * extends android. View. view-keep public class com.android. Vending. licensing. ilicensingservice            

2. When not to be confused

Generally the following conditions are not confused:
1. Using custom controls so make sure they don't get involved in the confusion
2. Enumeration is used to ensure that enumerations are not confused
3. Do not confuse classes in third-party libraries
4. A class that uses reflection is not confused
5. Tools such as Gson are used to make the JavaBean class the entity class not to be confused
6. When referring to a third-party library, it is generally noted that the confusion rules of the library, it is recommended to use the confusion rules added, so as not to find the last
7. Useful to WebView JS call also need to ensure that the writing interface method is not confused, the reason and the same as the first article
Subclasses of 8.Parcelable and Creator static member variables are not confused, otherwise a Android.os.BadParcelableException exception is generated

Three, basic confusion template

Finally provide a basic confusion template, of course, third-party library, or the above mentioned place, according to their actual needs to confuse

############################################### for some basic instructions to add # # # # ############################################ code obfuscation compression ratio, between 0~7, the default is 5, generally do not modify-optimizationpasses5# blending does not use case blending, the mixed class name is lowercase-dontusemixedcaseclassnames# Specifies that the class of non-public libraries is not ignored-dontskipnonpubliclibraryclasses# This phrase can confuse our project to produce a mapping file# contains a mapping relationship of class names, such as the name of the category, after confusion-verbose# Specifies not to ignore class members of non-public libraries-dontskipnonpubliclibraryclassmembers# no pre-calibration, preverify is one of the four steps of Proguard, Android does not need preverify, removing this step can speed up the confusion. -dontpreverify# Keep Annotation not confuse-keepattributes *annotation*,innerclasses# Avoid confusing generics-keepattributes Signature# Keep Code line number when exception is thrown-keepattributes sourcefile,linenumbertable# Specifies that obfuscation is the algorithm used, and the following parameter is a filter# This filter is the Google recommended algorithm, generally do not make changes-optimizations!code/simplification/cast,!field/*,!class/merging/*############################################### some of the common parts of Android development that need to be retained########################################### Keep The four components we use, custom application, etc. these classes are not confused# because these subclasses can be called externally-keep publicClass *ExtendsAndroid.App.Activity-keep PublicClass *ExtendsAndroid.App.Appliction-keep PublicClass *ExtendsAndroid.App.Service-keep PublicClass *ExtendsAndroid.Content.Broadcastreceiver-keep PublicClass *ExtendsAndroid.Content.Contentprovider-keep PublicClass *ExtendsAndroid.App.Backup.Backupagenthelper-keep PublicClass *ExtendsAndroid.Preference.Preference-keep PublicClass *ExtendsAndroid.View.View-keep PublicClassCom.Android.Vending.Licensing.Ilicensingservice# All classes and their internal classes under support are reserved-keepClassAndroid.support.** {*;}# Keep the inherited-keep publicClass *ExtendsAndroid.Support.V4.**-keep PublicClass *ExtendsAndroid.Support.V7.**-keep PublicClass *ExtendsAndroid.Support.annotation.**# Keep the resources under R-keepClass * *.r$* {*;}# Keep Local native method not to be confused-keepclasseswithmembernamesClass * {Native <methods>;}# method parameters that remain in the activity are methods of view,# So since the onclick we wrote in layout is not going to be affected-keepclassmembersClass *ExtendsAndroid.App.activity{Publicvoid * (Android.view.View);}# Keep Enum class not to be confused-keepclassmembersEnum * {public static **[] values (), public static * * VALUEOF (java.lang.String);}# Keep our custom controls (inherited from view) not to be confused-keep publicClass *ExtendsAndroid.View.view{* * * get* ();void set* (* * *); Public <init> (Android.content.Context); Public <init> (Android.content.Context, Android.util.AttributeSet); Public <init> (Android.content.Context, android.util.AttributeSet, int);}# Keep Parcelable serialization classes are not confused-keepClass *ImplementsAndroid.Os.parcelable {public static final Android.os.parcelable$creator *;}# classes that preserve serializable serialization are not confused-keepclassmembersClass *ImplementsJava.Io.Serializable {static final long serialversionuid; private static final java.io.objectstreamfield[] Serialpersistentfields,!static!transient <fields>;!private <fields>;!private <methods>; Privatevoid WriteObject (Java.io.ObjectOutputStream); Privatevoid ReadObject (Java.io.ObjectInputStream); Java.lang.Object Writereplace (); Java.lang.Object readresolve ();}# for Onxxevent, **on*listener with callback functions, cannot be confused-keepclassmembersClass * {void * (**on*event);void * (**on*listener);}# WebView processing, the project is not used to WebView ignored can be-keepclassmembersClassFQCN. of. javascript. interface. for. webview {public *;} -keepclassmembers class * extends Span class= "Hljs-title" >android. webkit. webviewclient {public void * (Android.webkit.WebView, Java.lang.String, Android.graphics.Bitmap); public Boolean * (Android.webkit.WebView, java.lang.String);} -keepclassmembers class * extends Span class= "Hljs-title" >android. webkit.webviewclient {public void * (Android.webkit.webView, jav.lang.String);}   

Android Studio Code obfuscation (Are you really confused)

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.