Android Code obfuscation

Source: Internet
Author: User

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 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.

The confusion in the Android project is very simple, and the reason for writing this summary is that the code of the company has recently been found to be not confusing, and the post-compilation code is readily available. Very shocked.

1.Modify 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 comments from the front of the Proguard.config

2.Modify 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 *;
    #}
```
If a third-party ' jar ' package is used in the program, causing an error after confusion, we need to configure it in proguard-project.txt so that it does not confuse the jar package when it is confused. The relevant configuration in the configuration file is explained as follows:
"' 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 * {"does not confuse native method names for all classes"
native <methods>;
    }
-keepclasseswithmembers class * {"does not confuse the method name of the specified method for all classes"
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 following specified method for 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, 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.** {*;} "There is no confusion about all classes under the Com.unionpay package, that is, the class name is not confused, and the method name and the variable name are not confused"
```        

After the above two parts 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, if the confusion can not be based on the configuration of the manifest file to find.
If for some of your own code to provide a way for others to call through reflection, we do not want to let some of the code is confused, or we use other people to provide third-party jar package, because the third-party jar package is generally confused, if we are confused will be error, So we have to make sure that this is not confusing, we just need to modify the 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.** {
*;
}

Android Code obfuscation

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.