您的位置:首页 > 移动开发 > Android开发

eclipse和Android Studio 代码混淆

2016-07-06 18:14 260 查看
一.eclipse和Android stuido代码混淆基本类似

android studio代码混淆

1.在build.grandle添加,其中规则写在proguard-rules.pro中,也可以自定义一个文件,将其代替,比如eclipse常用的 proguard-project.txt

buildTypes {
release {
//开启混淆
minifyEnabled true
proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘
}
}
2,在proguard-rules.pro中加入混淆的规则即可。
这里写说Android工程中哪些可以混淆,哪些不能混淆,先了解一下混淆里各个参数代表什么意思吧。
proguard 配置
-keepclasseswithmembers 指定的类和类成员被保留,假如指定的类成员存在的话。
-dontwarn 缺省proguard 会检查每一个引用是否正确,但是第三方库里面往往有些不会用到的类,没有正确引用。如果不配置的话,系统就会报错。
-keep 指定的类和类成员被保留作为 入口 。
-keepclassmembers 指定的类成员被保留。

proguard 参数
-include {filename} 从给定的文件中读取配置参数
-basedirectory {directoryname} 指定基础目录为以后相对的档案名称
-injars {class_path} 指定要处理的应用程序jar,war,ear和目录

-outjars {class_path} 指定处理完后要输出的jar,war,ear和目录的名称

-libraryjars {classpath} 指定要处理的应用程序jar,war,ear和目录所需要的程序库文件

-dontskipnonpubliclibraryclasses 指定不去忽略非公共的库类。

-dontskipnonpubliclibraryclassmembers 指定不去忽略包可见的库类的成员。

[/code]

不能混淆的代码

下面这样代码混淆的时候要注意保留。

Android系统组件,系统组件有固定的方法被系统调用。

被Android Resource 文件引用到的。名字已经固定,也不能混淆,比如自定义的View

Android Parcelable ,需要使用android 序列化的。

Java序列化方法,系统序列化需要固定的方法。

枚举 ,系统需要处理枚举的固定方法

本地方法,不能修改本地方法名

annotations 注释

数据库驱动

有些resource 文件

用到反射的地方

其他Anroid 官方建议 不混淆的,如

android.app.backup.BackupAgentHelper

android.preference.Preference

com.android.vending.licensing.ILicensingService

保留选项

-keep {Modifier} {class_specification} 保护指定的类文件和类的成员

-keepclassmembers {modifier} {class_specification} 保护指定类的成员,如果此类受到保护他们会保护的更好

-keepclasseswithmembers {class_specification} 保护指定的类和类的成员,但条件是所有指定的类和类成员是要存在。

-keepnames {class_specification} 保护指定的类和类的成员的名称(如果他们不会压缩步骤中删除)

-keepclassmembernames {class_specification} 保护指定的类的成员的名称(如果他们不会压缩步骤中删除)

-keepclasseswithmembernames {class_specification} 保护指定的类和类的成员的名称,如果所有指定的类成员出席(在压缩步骤之后)

-printseeds {filename} 列出类和类的成员-keep选项的清单,标准输出到给定的文件

压缩

-dontshrink 不压缩输入的类文件

-printusage {filename}

-whyareyoukeeping {class_specification}

优化

-dontoptimize 不优化输入的类文件

-assumenosideeffects {class_specification} 优化时假设指定的方法,没有任何副作用

-allowaccessmodification 优化时允许访问并修改有修饰符的类和类的成员
如果Android studio工程中引用了第三方jar,代码混淆如何忽略第三方jar包

1.打开app目录下的lib文件夹,将各第三方jar下的包名在proguard-rules.pro 文件中过滤。



code:

<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Consolas';font-size:12.0pt;"><span style="color:#cc7832;"><strong>-keep </strong></span>class com.baidu.**{<span style="color:#9876aa;">*;</span>}
<span style="color:#cc7832;"><strong>-keep </strong></span>class vi.com.gdi.bgl.andriod.java.**{<span style="color:#9876aa;">*;</span>}
<span style="color:#cc7832;"><strong>-keep </strong></span>class com.google.gson.**{<span style="color:#9876aa;">*;</span>}



上面的代码以 [-keep class com.baidu.* { ; }**] 为例,意思是不混淆com.baidu包下的各个类及类里面的所以域和方法。把上面的包名换成自己想要忽略的jar下面暴露的包名即可。

知道这些参数代表什么意思就可以写混淆规则了,eclipse里在proguard-project.txt里即可。

这里贴一下我写好的混淆规则。

# 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 *;
#}
################################自己的配置开始########################
-keep class com.newrelic.** { *; }
-dontwarn com.newrelic.**
-keepattributes Exceptions, Signature, InnerClasses

#-dontobfuscate
#-dontoptimize
-optimizationpasses 5                                                        # 指定代码的压缩级别
-dontusemixedcaseclassnames                                        # 是否使用大小写混合
-dontskipnonpubliclibraryclasses                                 # 是否混淆第三方jar
-dontpreverify                                              # 混淆时是否做预校验
-verbose                                                   # 混淆时是否记录日志
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*   # 混淆时所采用的算法
#-allowaccessmodification # 优化时允许访问并修改有修饰符的类和类的成员 混淆  Specifies that the access modifiers of classes and class members may be broadened during processing. This can improve the results of the optimization step. For instance, when inlining a public getter, it may be necessary to make the accessed field public too. Although Java's binary compatibility specifications formally do not require this (cfr. The Java Language Specification, Third Edition, Section 13.4.6), some virtual machines would have problems with the processed code otherwise. Only applicable when optimizing (and when obfuscating with the -repackageclasses option).
#-overloadaggressively #不要使用Specifies to apply aggressive overloading while obfuscating. Multiple fields and methods can then get the same names, as long as their arguments and return types are different, as required by Java bytecode (not just their arguments, as required by the Java language). This option can make the processed code even smaller (and less comprehensible). Only applicable when obfuscating.
#-repackageclasses ''#把执行后的类重新放在某一个目录下,后跟一个目录名 Specifies to repackage all class files that are renamed, by moving them into the single given package. Without argument or with an empty string (''), the package is removed completely. This option overrides the -flattenpackagehierarchy option. It is another example of further obfuscating package names. It can make the processed code even smaller and less comprehensible. Its deprecated name is -defaultpackage. Only applicable when obfuscating.
#忽略警告 也可以用
-ignorewarnings
-dontwarn
#所有native的方法不能去混淆.
-keepclasseswithmembernames class * {
native <methods>;
}
#=================聪明投的开始==============
-keep class com.qiyuan.congmingtou.network.** { *; }
-keep class com.qiyuan.congmingtou.wxapi.**{*;}
-keep class com.qiyuan.congmingtou.view.**{*;}
#=================聪明投的结束==============
#动画
-keep class com.facebook.**{*;}
# volley
-dontwarn com.android.volley.jar.**
-keep class com.android.volley.**{*;}
# 保持哪些类不被混淆
-keep class android.** {*; }
-keep public class * extends android.view
-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.pm
-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 com.android.vending.licensing.ILicensingService
-keep public class * extends java.io.Serializable
-keep class * extends android.app.Dialog
#枚举类不能去混淆.
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
#aidl文件不能去混淆.
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
# 保持自定义控件类不被混淆
-keep public class * extends android.view.View {
public <init>(android.content.Context);
public <init>(android.content.Context, android.util.AttributeSet);
public <init>(android.content.Context, android.util.AttributeSet, int);
public void set*(...);
}
# 保持 Parcelable 不被混淆
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
#不混淆资源类
-keepclassmembers class **.R$* {
public static <fields>;
}
-keepclassmembers class * {
public <methods>;
public static <fields>;
private static <fields>;
}
# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans -keepclassmembers public class * extends android.view.View {
void set*(***);
*** get*();
}

# We want to keep methods in Activity that could be used in the XML attribute onClick
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
#将不能混淆的申明出来
-keep class com.baidu.** {*;}
-keep class vi.com.** {*;}
-dontwarn com.baidu.**
-dontwarn com.baidu.mapapi.**
-keep class com.baidu.mapapi.**{*;}
-keep class com.baidu.location.**{*;}

-keep class android.support.v4.** {*;}
-keep class android.support.annotation.**{*;}
-keep class com.davemorrissey.labs.subscaleview.**{*;}
-keep class com.sensorsdata.analytics.android.sdk.**{*;}
-keep class okio.**{*;}
-keep class okhttp3.**{*;}
-dontwarn net.sourceforge.pinyin4j.**
-keep class net.sourceforge.pinyin4j.**{*;}
-dontwarn demo.**
-keep class demo.**{*;}
-keep class * extends android.support.v4.app.Fragment
-keep class * extends android.app.Activity
-keep class * extends android.support.v4.app.FragmentActivity
-keep class * extends android.app.Application
-keep class * extends java.lang.annotation.Annotation { *; }

# Gson specific classes
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.examples.android.model.** { *; }
-keep public class * implements java.io.Serializable { *; }
-keepclassmembers class * implements java.io.Serializable {
static final long serialVersionUID;
private static final java.io.ObjectStreamField[] serialPersistentFields;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
java.lang.Object readResolve();
}

#-keep public class * implements java.io.Serializable {*;}

##---------------End: proguard configuration for Gson  ----------

-keep class com.tencent.tauth.** {*;}
-keep class org.apache.http.entity.mime.** {*;}
-keep class android.net.http.** {*;}
-keep class com.weibo.sdk.android.** {*;}
-keep class com.sina.sso.** {*;}
#-keep class com.yintong.secure.activity.PayIntro$LLJavascriptInterface{*;}
-keep class com.alipay.android.app.IAliPay{*;}
-keep class com.alipay.android.app.IAlixPay{*;}
-keep class com.alipay.android.app.IRemoteServiceCallback{*;}

##---------------End: proguard configuration for Gson  ----------

#记录生成的日志数据,gradle build时在本项目根目录输出
#apk 包内所有 class 的内部结构
#-dump class_files.txt
#未混淆的类和成员
#-printseeds seeds.txt
#列出从 apk 中删除的代码1
#-printusage unused.txt
#混淆前后的映射
#-printmapping mapping.txt
################################自己的配置结束########################

#####################################友盟分享混淆开始######################################
-dontshrink
 -dontoptimize
 -dontwarn com.google.android.maps.**
-dontwarn android.webkit.WebView
-dontwarn com.umeng.**
-dontwarn com.tencent.weibo.sdk.**
-dontwarn com.facebook.**
-keep public class javax.**
-keep public class android.webkit.**
-dontwarn android.support.v4.**
-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 class com.facebook.**
-keep class com.facebook.** { *; }
-keep class com.umeng.scrshot.**
-keep public class com.tencent.** {*;}
-keep class com.umeng.socialize.sensor.**
-keep class com.umeng.socialize.handler.**
-keep class com.umeng.socialize.handler.*
-keep class com.tencent.mm.sdk.modelmsg.WXMediaMessage {*;}
-keep class com.tencent.mm.sdk.modelmsg.** implements com.tencent.mm.sdk.modelmsg.WXMediaMessage$IMediaObject {*;}

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

-dontwarn twitter4j.**
-keep class twitter4j.** { *; }

-keep class com.tencent.** {*;}
-dontwarn com.tencent.**
-keep public class com.umeng.soexample.R$*{
public static final int *;
}
-keep public class com.umeng.soexample.R$*{
public static final int *;
}
-keep class com.tencent.open.TDialog$*
-keep class com.tencent.open.TDialog$* {*;}
-keep class com.tencent.open.PKDialog
-keep class com.tencent.open.PKDialog {*;}
-keep class com.tencent.open.PKDialog$*
-keep class com.tencent.open.PKDialog$* {*;}

-keep class com.sina.** {*;}
-dontwarn com.sina.**
-keep class  com.alipay.share.sdk.** {
*;
}
-keepnames class * implements android.os.Parcelable {
public static final ** CREATOR;
}

-keep class com.linkedin.** { *; }
-keepattributes Signature
##################################################友盟分享混淆结束##################################
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: