Android Code obfuscation

Source: Internet
Author: User
Tags jboss

Obfuscation (Proguard)
---

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 project. Therefore, when your application is sensitive to security (demanding), such as when you authorize an 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 will only execute when the application is built in the publish mode, so you don't have to deal with confusing code when building the program in debug mode. Making the obfuscation executable is optional, but recommended.

The confusion in the Android project is easy, and the reason for writing this summary is that the code of the company is not confusing, and the code is readily available after the anti-compilation. Was shocked.

1.Change Project.Properties
"' XML
# This file was automatically generated by Android Tools.
# Do not modify the this file--YOUR changes 'll 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
"' XML
# To enable Proguard in your project, edit project.properties
# to define, the Proguard.config property as described. file.
    #
# ADD Project specific Proguard rules here.
# By default, 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
# include property in Project.Properties.
    #
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# ADD any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class Fqcn.of.javascript.interface.for.webview {
# 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 proguard-project.txt so that it does not confuse the corresponding jar package when it is confused. The relevant configuration in the config file is explained in the following example:
"' Java
-keep public class * extends Android.app.Activity "Do not confuse class names, keep their 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.asqw.android{
Public void Start (java.lang.String); "No confusion with this method"
    }
-keepclasseswithmembernames class * {"native method names are not confused for all classes"
native <methods>;
    }
-keepclasseswithmembers class * {"The method name of the specified method for all classes is not confused"
Public <init> (Android.content.Context, android.util.AttributeSet);
    }
-keepclassmembers class * extends Android.app.Activity {"The method name of the specified method for all classes is not confused"
Public void * (Android.view.View);
    }
-keepclassmembers enum * {"Does not confuse the method name of the specified method under all classes of enum type enum"
Public static **[] values ();
Public Static * * VALUEOF (java.lang.String);
    }
-keep class * Implements Android.os.Parcelable {"Does not confuse the classes name of all classes that implement the Parcelable interface, and its member variable is parcelable$ Variable names for member variables of the creator type are not confused "
Public static Final Android.os.parcelable$creator *;
    }
-keepclasseswithmembers class Org.jboss.netty.util.internal.LinkedTransferQueue {"Does not confuse variable names for specified variables of a specified class"
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 the variable name are not confused"
```        

After the above two units after the anti-compilation can be confused, but the four components are still, why the four components are still there, because the four components are configured in the manifest file, assuming that the confusion can not be based on the configuration of the manifest file to find.
Let's say we don't want to confuse some of our code with a third-party jar package that someone else provides, because the third-party jar packages are generally confused, and if we get confused, we'll get an error if we confuse them. So we have to make sure that this is not confusing, here we just need to change this file, and then add the following sentence, he will not confuse the content we give
"' XML
-keepattributes *annotation*
-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 * 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.