欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 混淆代码详解及实例

程序员文章站 2024-03-05 19:54:43
为了防止自己的劳动成果被别人窃取,混淆代码能有效防止被反编译,下面来总结以下混淆代码的步骤: 1. 大家也许都注意到新建一个工程会看到项目下边有这样proguard-...

为了防止自己的劳动成果被别人窃取,混淆代码能有效防止被反编译,下面来总结以下混淆代码的步骤:

1. 大家也许都注意到新建一个工程会看到项目下边有这样proguard-project.txt一个文件,这个对混淆代码很重要,如果你不小心删掉了,没关系,从其他地方拷贝一个过来

2. 最重要的就是在proguard-project.txt添加混淆的申明了:

 a. 把所有你的jar包都申明进来,例如:

 -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. 将你不需要混淆的部分申明进来,因为有些类经过混淆会导致程序编译不通过,如下:

-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

--以上都是api里边的类,最好都要避免混淆

有些很特殊的,例如百度地图,你需要添加以下申明:

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

根据我的经验,一般model最好避免混淆(model无关紧要,不混淆也没多大关系)如:

-keep class com.bank.pingan.model.** { *; }

下面在贴上关于umeng分享统计的避免混淆的申明

-dontwarn android.support.v4.** 
-dontwarn org.apache.commons.net.**
-dontwarn com.tencent.**
-keepclasseswithmembernames class * {
    native <methods>;
}
-keepclasseswithmembernames class * {
    public <init>(android.content.context, android.util.attributeset);
}
-keepclasseswithmembernames class * {
    public <init>(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 <init>(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.以上工作完成,混淆工作就完成了一大半了,最后需要做的就是在project.properties文件中加上你的混淆文件申明了,如下:
   proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

4. ok, 最后一步,打签名包测试,如果有问题,仔细看下log也许有得类不能混淆,那么你得加入到proguard-project.txt文件中

-------以上就是混淆代码的全过程了 

最后贴上proguard-project.txt的全部代码:

# 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 <methods>; 
} 
 
-keepclasseswithmembernames class * { 
  public <init>(android.content.context, android.util.attributeset); 
} 
 
-keepclasseswithmembernames class * { 
  public <init>(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 <init>(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 *; 
} 

2015-05-25更新 

1. 依赖工程的jar 申明: 

以下是邮件发送的实例 

-libraryjars ../klowerbase/libs/mail-activation.jar 
-libraryjars ../klowerbase/libs/mail-additionnal.jar 
-libraryjars ../klowerbase/libs/mail.jar 

2. 解决邮件发送时报的以异常 

 -keep class javax.mail.**{*;} 
 -keep class com.sun.mail.**{*;} 
-keep class javax.activation.**{*;} 
-keep class org.apache.harmony.**{*;} 
-keep class java.security.**{*;} 
#解决:can't find referenced class java.beans.beans 
-dontwarn org.apache.commons.collections.beanmap 
-dontwarn java.beans.** 
#解决:can't find referenced class javax.security.sasl.sasl 
-dontwarn com.sun.mail.imap.protocol.** 

通过本文希望能帮助 android开发应用使用混淆代码的朋友,谢谢大家对本站的支持!