Code obfuscation for Android application development _android

Source: Internet
Author: User
Tags volatile jboss

Obfuscation Device (Proguard)

Obfuscation compresses, optimizes, and confuses code by removing unused code and renaming classes, fields, and methods with obscure names. The result is a smaller. apk file, which is more difficult to reverse project. Therefore, when your application is sensitive to security (high requirements), such as when you authorize the application, the obfuscation is an important means of protection.

The obfuscation is integrated into the Android build system, so you don't have to call it manually. At the same time the obfuscation is performed only when the application is built in advertisement mode, so you do not have to deal with confusing code when you build the program in debug mode. It is optional to allow the obfuscation to execute, but the recommended choice.

1. Change Project.Properties

  # This file was automatically generated by Android Tools.
  # don't modify this file--YOUR CHANGES would be erased!
  # This
  file must is checked in Version control Systems.
  #
  to customize properties used by the ' Ant build system edit
  # ' Ant.properties ', 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.txt

  # project target.
  Target=android-19

Remove the gaze from the front of the Proguard.config

2. Change Proguard-project.txt

 # to enable Proguard in your project, edit Project.Properties # to define the Proguar
  D.config property as described in that file.
  # ADD Project specific Proguard rules here. 
  # By default, the flags in this file are appended to flags specified # in ${sdk.dir}/tools/proguard/proguard-android.txt
  # You can edit the ' include path and order by changing the ' Proguard # include in Project.Properties. # for more details, and # http://developer.android.com/guide/developing/tools/proguard.html # ADD any project SP Ecific Keep options here: # If your project uses WebView with JS, uncomment the following # and specify the fully qua Lified class name to the JavaScript interface # class: #-keepclassmembers class Fqcn.of.javascript.interface.for.webvi
  EW {# public *; #}

    Assuming that a third party ' jar ' package is used in the program, causing an error after confusion, we need to make the corresponding configuration in the proguard-project.txt so that it does not confuse the corresponding jar bundle in the confusion. An explanation of the related configuration in the change configuration file is as follows:

' Java-keep public class * extends android.app.Activity ' class without confusing class name, keep its original class name and package name '-keep public abstract interface com . asqw.android.listener{public protected <methods>; "all public protected method names are not confused"}-keep public class COM.ASQ w.android{public void Start (java.lang.String); "Do not confuse this method"}-keepclasseswithmembernames class * {"native method name for all classes is not
  To confuse "native <methods>; -keepclasseswithmembers class * {"does not confuse the method name of the specified method for all classes" public <init> (Android.content.Context, Android.util.Att
  Ributeset);
  -keepclassmembers class * Extends Android.app.Activity {"Does not confuse the method name of the specified method for all classes" public void * (Android.view.View);
  -keepclassmembers enum * {"" public static **[] values () is not confused with the method name of the method specified below for all classes of enum type Enum.
  public static * * VALUEOF (java.lang.String); -keep class * Implements Android.os.Parcelable {"Does not confuse the name of all classes that implement the Parcelable interface, and its member variable is parcelable$
  The variable name of the Creator type's member variable is not confused "public static final Android.os.parcelable$creator *; }-keepclasseswithmeMbers class Org.jboss.netty.util.internal.LinkedTransferQueue {"The variable name of the specified variable for the specified class is not confused" volatile transient
    Org.jboss.netty.util.internal.linkedtransferqueue$node Head;
    volatile transient org.jboss.netty.util.internal.linkedtransferqueue$node tail;

  volatile transient int sweepvotes; -keep public class com.unionpay.** {*;}    

 "All classes under the Com.unionpay package are not confused, that is, the class name is not confused, and the method name and variable name" "are not confused.

After the above two parts after the decompile can be confused, but the four components are still, why the four components are still, because the four components are in the inventory file configuration, the assumption is confusing can not be based on the list file configuration to look for.

Suppose, for some of your own code, that you want to provide a way for others to invoke by reflection, we don't want to confuse parts of the code, or we use Third-party jar packages that are provided by other people, and because third party jar packs are generally confusing, we'll get an error if we confuse them. So we have to make sure that the content is not confused, here we just need to change the file, and then add the following sentence, he will not confuse what we give

-keepattributes *annotation*     
-keep public class * extends android.app.Activity-keep public
class * extends Andro Id.app.Application
-keep public class * extends Android.app.Service
-keep public class * extends Android.content . Broadcastreceiver
-keep public class * extends Android.content.ContentProvider
-keep public class * extends Android.app.backup.BackupAgent
-keep public class * extends Android.preference.Preference
-keep public class * Extends Android.support.v4.app.Fragment
-keep public class * extends Android.app.Fragment
-keep public class Com.android.vending.licensing.ILicensingService
-keep class Com.itheima.mobilesafe.engine.AppInfoProvider
-keep class net.youmi.android.** {
*;
}

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.