Android obfuscation code Summary

Source: Internet
Author: User

To prevent your work results from being stolen by others, obfuscation code can effectively prevent decompilation. The following describes the steps for obfuscation of code:

1. You may also see a new project with a proguard-project.txt file under the project. This is very important for obfuscation code. If you accidentally delete it, it doesn't matter. Copy one from other places.

2. The most important statement is to add a obfuscation statement in proguard-project.txt:

A. Declare all your jar packages. For example:
-Libraryjars libs/apns_1.0.6.jar
-Libraryjars libs/armeabi/libBaiduMapSDK_v2_3_1.so
-Libraryjars libs/armeabi/liblocSDK4.so
-Libraryjars libs/baidumapapi_v2_3_1.jar
-Libraryjars libs/core. jar
-Libraryjars libs/gesture-imageview.jar
-Libraryjars libs/gson-2.0.jar
-Libraryjars libs/infogracesound. jar
-Libraryjars libs/locSDK_4.0.jar
-Libraryjars libs/ormlite-android-4.48.jar
-Libraryjars libs/ormlite-core-4.48.jar
-Libraryjars libs/universal-image-loader-1.9.0.jar

B. Declare the parts you do not need to be confused, because some classes will cause program compilation to fail, as shown below:

-Keep public class * extends android. app. Fragment
-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. BackupAgentHelper
-Keep public class * extends android. preference. Preference
-Keep public class * extends android. support. v4 .**
-Keep public class com. android. vending. licensing. ILicensingService

-- All of the above are classes in the API. It is best to avoid confusion.

For some special items, such as Baidu map, you need to add the following statement:

-Keep class com. baidu .**{*;}
-Keep class vi.com. gdi. bgl. android .**{*;}

Based on my experience, it is generally best to avoid confusion between models (model is irrelevant and does not matter much if it is not obfuscated), for example:

-Keep class com. bank. pingan. model .**{*;}
The following is a statement about Umeng sharing statistics to avoid confusion.

-Dontwarn android. support. v4 .**
-Dontwarn org.apache.commons.net .**
-Dontwarn com. tencent .**

-Keepclasseswithmembernames class *{
Native ;
}

-Keepclasseswithmembernames class *{
Public (Android. content. Context, android. util. AttributeSet );
}

-Keepclasseswithmembernames class *{
Public (Android. content. Context, android. util. AttributeSet, int );
}

-Keepclassmembers enum *{
Public static ** [] values ();
Public static ** valueOf (java. lang. String );
}

-Keep class * implements android. OS. Parcelable {
Public static final android. OS. Parcelable $ Creator *;
}

-Keepclasseswithmembers class *{
Public (Android. content. Context );
}

-Dontshrink
-Dontoptimize
-Dontwarn com. google. android. maps .**
-Dontwarn android. webkit. WebView
-Dontwarn com. umeng .**
-Dontwarn com. tencent. weibo. sdk .**
-Dontwarn com. facebook .**

-Keep enum com. facebook .**
-Keepattributes Exceptions, InnerClasses, Signature
-Keepattributes * Annotation *
-Keepattributes SourceFile, LineNumberTable

-Keep public interface com. facebook .**
-Keep public interface com. tencent .**
-Keep public interface com. umeng. socialize .**
-Keep public interface com. umeng. socialize. sensor .**
-Keep public interface com. umeng. scrshot .**

-Keep public class com. umeng. socialize .*{*;}
-Keep public class javax .**
-Keep public class android. webkit .**

-Keep class com. facebook .**
-Keep class com. umeng. scrshot .**
-Keep public class com. tencent .**{*;}
-Keep class com. umeng. socialize. sensor .**

-Keep class com. tencent. mm. sdk. openapi. WXMediaMessage {*;}

-Keep class com. tencent. mm. sdk. openapi. ** implements com. tencent. mm. sdk. openapi. WXMediaMessage $ IMediaObject {*;}

-Keep class im. yixin. sdk. api. YXMessage {*;}
-Keep class im. yixin. sdk. api. ** implements im. yixin. sdk. api. YXMessage $ YXMessageData {*;}

-Keep public class [your_pkg]. R$ *{
Public static final int *;
}

3. After the above work is completed, the obfuscation work is completed by more than half. The last thing to do is to add your obfuscation file declaration in the project. properties file, as shown below:

Proguard. config =$ {sdk. dir}/tools/proguard/proguard-android.txt: proguard-project.txt

4. OK, In the hosts file

------- The above is the whole process of obfuscation code.

Paste all the code of proguard-project.txt to the end:

# To enable ProGuard in your project, edit project.properties# to define the proguard.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 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 *;#}-optimizationpasses 5-dontusemixedcaseclassnames-dontskipnonpubliclibraryclasses-dontpreverify-verbose-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*-keepattributes *Annotation*-keepattributes Signature-libraryjars libs/apns_1.0.6.jar-libraryjars libs/armeabi/libBaiduMapSDK_v2_3_1.so-libraryjars libs/armeabi/liblocSDK4.so-libraryjars libs/baidumapapi_v2_3_1.jar-libraryjars libs/core.jar-libraryjars libs/gesture-imageview.jar-libraryjars libs/gson-2.0.jar-libraryjars libs/infogracesound.jar-libraryjars libs/locSDK_4.0.jar-libraryjars libs/ormlite-android-4.48.jar-libraryjars libs/ormlite-core-4.48.jar-libraryjars libs/universal-image-loader-1.9.0.jar-keep class com.baidu.** { *; } -keep class vi.com.gdi.bgl.android.**{*;}-keep public class * extends android.app.Fragment  -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.BackupAgentHelper-keep public class * extends android.preference.Preference-keep public class * extends android.support.v4.**-keep public class com.android.vending.licensing.ILicensingService-keep class com.google.gson.stream.** { *; }-keep class com.google.gson.examples.android.model.** { *; }-keep class com.uuhelper.Application.** { *; }-keep class net.sourceforge.zbar.** { *; }-keep class com.google.android.gms.** { *; } -keep class com.bank.pingan.model.** { *; }-keep public class * extends com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper-keep public class * extends com.j256.ormlite.android.apptools.OpenHelperManager -keep class com.android.vending.licensing.ILicensingService-keep class android.support.v4.** { *; }  -keep class org.apache.commons.net.** { *; }  -keep class com.tencent.** { *; }   -keep class com.umeng.** { *; }  -keep class com.umeng.analytics.** { *; }  -keep class com.umeng.common.** { *; }  -keep class com.umeng.newxp.** { *; }   -keep class com.j256.ormlite.** { *; }  -keep class com.j256.ormlite.android.** { *; }  -keep class com.j256.ormlite.field.** { *; }  -keep class com.j256.ormlite.stmt.** { *; } -dontwarn android.support.v4.**  -dontwarn org.apache.commons.net.** -dontwarn com.tencent.**  -keepclasseswithmembernames class * {    native 
 
  ;}-keepclasseswithmembernames class * {    public 
  
   (android.content.Context, android.util.AttributeSet);}-keepclasseswithmembernames class * {    public 
   
    (android.content.Context, android.util.AttributeSet, int);}-keepclassmembers enum * {    public static **[] values();    public static ** valueOf(java.lang.String);}-keep class * implements android.os.Parcelable {  public static final android.os.Parcelable$Creator *;}-keepclasseswithmembers class * {    public 
    
     (android.content.Context);}-dontshrink-dontoptimize-dontwarn com.google.android.maps.**-dontwarn android.webkit.WebView-dontwarn com.umeng.**-dontwarn com.tencent.weibo.sdk.**-dontwarn com.facebook.**-keep enum com.facebook.**-keepattributes Exceptions,InnerClasses,Signature-keepattributes *Annotation*-keepattributes SourceFile,LineNumberTable-keep public interface com.facebook.**-keep public interface com.tencent.**-keep public interface com.umeng.socialize.**-keep public interface com.umeng.socialize.sensor.**-keep public interface com.umeng.scrshot.**-keep public class com.umeng.socialize.* {*;}-keep public class javax.**-keep public class android.webkit.**-keep class com.facebook.**-keep class com.umeng.scrshot.**-keep public class com.tencent.** {*;}-keep class com.umeng.socialize.sensor.**-keep class com.tencent.mm.sdk.openapi.WXMediaMessage {*;}-keep class com.tencent.mm.sdk.openapi.** implements com.tencent.mm.sdk.openapi.WXMediaMessage$IMediaObject {*;}-keep class im.yixin.sdk.api.YXMessage {*;}-keep class im.yixin.sdk.api.** implements im.yixin.sdk.api.YXMessage$YXMessageData{*;}-keep public class [your_pkg].R$*{    public static final int *;}
    
   
  
 


OK, That's all.

-- Thank you!

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.