Android Studio代码混淆

本文介绍了如何在Android Studio中进行代码混淆,包括在app的build.gradle文件中添加混淆配置,以及在proguard-rules.pro文件中编写混淆规则。详细讲解了代码混淆的参数解释,并通过实际例子展示了混淆代码的步骤和技巧。
摘要由CSDN通过智能技术生成

Android Studio代码混淆

一、app下的build.gradle文件中添加如下代码

buildTypes {
    release {
        minifyEnabled true //是否混淆  true:混淆  false:不混淆   默认false
        
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    
}
}

//proguard-android.txt 文件是本地sdk/tools/proguard文件夹下的默许文件

//proguard-rules.pro文件就是用来编写混淆代码的

二、接下来就是在prguard-rules.pro文件中编写混淆代码

1. 代码混淆详解(参数详解)

# 指定代码的压缩级别
-optimizationpasses 5  
# 是否使用大小写混合    
-dontusemixedcaseclassnames
# 是否混淆第三方jar                                                   
-dontskipnonpubliclibraryclasses     
# 混淆时是否做预校验                                              
-dontpreverify     
# 混淆时是否记录日志                                                             
-verbose      
# 混淆时所采用的算法(优化)
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

# 1句是导入第三方的类库,防止混淆时候读取包内容出错
-libraryjars libs/youjar.jar

         
      
# 保持哪些类不被混淆
-keep public class * extends android.app.Activity                                
-keep public class * extends android.app.Application  

-keep public class * extends android.app.Fragment                          
-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 com.android.vending.licensing.ILicensingService                
      
# 保持 native 方法不被混淆
-keepclasseswithmembernames class * {                                            
    native <methods>;  
}  

# 保持自定义控件类不被混淆      
-keepclasseswithmembers class * {                                                 
    public <init>(android.content.Context, android.util.AttributeSet);  
}  
  
# 保持自定义控件类不被混淆  
-keepclasseswithmembers class * {  
    public <init>(android.content.Context, android.util.AttributeSet, int);       
}  

# 保持自定义控件类不被混淆      
-keepclassmembers class * extends android.app.Activity {                          
    public void *(android.view.View);  
}  

# 保持枚举 enum 类不被混淆      
-keepclassmembers enum * {                                                        
    public static **[] values();  
    public static ** valueOf(java.lang.String);  
}  

# 保持 Parcelable 不被混淆      
-keep class * implements android.os.Parcelable {                                  
    public static final android.os.Parcelable$Creator *;  
}  

# 保持自己定义的类不被混淆      
-keep class MyClass;

 

2. 代码混淆举例一

#-----------------混淆配置设定--------------------------------------
#指定代码压缩级别
-optimizationpasses 5
#混淆时不会产生形形色色的类名                                                      
-dontusemixedcaseclassnames
#指定不忽略非公共类库                                                 
-dontskipnonpubliclibraryclasses
#不预校验,如果需要预校验,是-dontoptimize                                            
-dontpreverify
#屏蔽警告                                                              
-ignorewarnings
#混淆时记录日志                                                             
-verbose
#优化                                                                    
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*    

#----导入第三方包,但是在当前版本中使用会报 input jar file is specified twice 错误,所以注释掉
#-libraryjars libs/android.support.v4.jar
#-libraryjars libs/BaiduLBS_Android.jar
#-libraryjars libs/commons-httpclient-3.1.jar
#-libraryjars libs/jackson-annotations-2.1.4.jar
#-libraryjars libs/jackson-core-2.1.4.jar
#-libraryjars libs/jackson-databind-2.1.4.jar
#-libraryjars libs/xUtils-2.6.14.jar
#-----------------不需要混淆第三方类库------------------------------------------------------------------
#去掉警告
-dontwarn android.support.v4.**                                             
#过滤android.support.v4
-keep class android.support.v4.** { *; }                                    
-keep interface android.support.v4.app.** { *; }
-keep public class * extends android.support.v4.**
-keep public class * extends android.app.Fragment

#过滤commons-httpclient-3.1.jar
-keep class org.apache.**{*;}                                               

#过滤jackson-core-2.1.4.jar
-keep class com.fasterxml.jackson.**{*;}                                    

#去掉警告
-dontwarn com.lidroid.xutils.**                                             
#过滤xUtils-2.6.14.jar
-keep class com.lidroid.xutils.**{*;}                                       
#这是xUtils文档中提到的过滤掉注解
-keep class * extends java.lang.annotation.Annotation{*;}                   

#去掉警告
-dontwarn com.baidu.**                                                      
-dontwarn com.baidu.mapapi.**
#过滤BaiduLBS_Android.jar
-keep class com.baidu.** {*;}                                               
-keep class vi.com.gdi.bgl.android.**{*;}
-keep class com.baidu.platform.**{*;}
-keep class com.baidu.location.**{*;}
-keep class com.baidu.vi.**{*;}

#-----------------不需要混淆系统组件等-------------------------------------------------------------------
-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.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService

#过滤掉自己编写的实体类
-keep class com.classtc.test.entity.**{*;}                                   


#------保护指定的类和类的成员,但条件是所有指定的类和类成员是要存在-----------
-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

 

 

3. 代码混淆举例二

#指定代码的压缩级别
-optimizationpasses 5

#包明不混合大小写
-dontusemixedcaseclassnames

#不去忽略非公共的库类
-dontskipnonpubliclibraryclasses

 
#优化  不优化输入的类文件
-dontoptimize

#不混淆输入的类文件
-dontobfuscate

 
#预校验
-dontpreverify

 
# 有了verbose这句话,混淆后就会生成映射文件
 # 包含有类名->混淆后类名的映射关系
 # 然后使用printmapping指定映射文件的名称
 
-verbose
 -printmapping
priguardMapping.txt

 # 混淆时所采用的算法
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

#保护注解
-keepattributes *Annotation*

# 抛出异常时保留代码行号
-keepattributes SourceFile,LineNumberTable

# 保持哪些类不被混淆
-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 com.android.vending.licensing.ILicensingService
#如果有引用v4包可以添加下面这行
-keep public class * extends android.support.v4.app.Fragment

#如果引用了v4或者v7
-dontwarn android.support.**

# 保留Activity中的方法参数是view的方法,
    # 从而我们在layout里面编写onClick就不会影响
    
-keepclassmembers class * extends android.app.Activity {
        public void * (android.view.View);
    
}

    -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*(...);
            *** get* ();
        
}

        #保持 native 方法不被混淆
    
-keepclasseswithmembernames class * {
            native <methods>;
        
}

        #保持自定义控件类不被混淆
    
-keepclasseswithmembers class * {
            public <init>(android.content.Context, android.util.AttributeSet);
        
}
    #保持 Parcelable 不被混淆
    
-keep class * implements android.os.Parcelable {
      public static final android.os.Parcelable$Creator *;
    
}

    #保持 Serializable 不被混淆
    
-keep public class * implements java.io.Serializable {
        public *;
    
}

    -keepclassmembers class * implements java.io.Serializable {
        static final long serialVersionUID;
        private static final java.io.ObjectStreamField[] serialPersistentFields;
        !static !transient <fields>;
        !private <fields>;
        !private <methods>;
        private void writeObject(java.io.ObjectOutputStream);
        private void readObject(java.io.ObjectInputStream);
        java.lang.Object writeReplace();
        java.lang.Object readResolve();
    
}

    #不混淆资源类
    
-keepclassmembers class **.R$* {
        public static <fields>;
    
}

    # 对于带有回调函数onXXEvent的,不能混淆
    
-keepclassmembers class * {
        void *(**On*Event);
    
}

#移除log 测试了下没有用还是建议自己定义一个开关控制是否输出日志
-assumenosideeffects class android.util.Log {
    public static boolean isLoggable(java.lang.String, int);
    public static int v(...);
    public static int i(...);
    public static int w(...);
    public static int d(...);
    public static int e(...);
}

-dontwarn com.squareup.**
-keep class com.squareup.**{*; }

-dontwarn org.apache.**
-keep class org.apache.**{*;}

-dontwarn com.tencent.**
-keep class com.tencent.**{*;}

-dontwarn com.sina.**
-keep class com.sina.**{*;}

-dontwarn com.baidu.**
-keep class com.baidu.**{*;}

-dontwarn com.alipay.**
-keep class com.alipay.**{*;}

-dontwarn org.json.alipay.**
-keep class org.json.alipay.**{*;}

-dontwarn org.apache.http.entity.mime.**
-keep class org.apache.http.entity.mime.**{*;}

-dontwarn com.rongkecloud.**
-keep class com.rongkecloud.**{*;}

-dontwarn com.rongke.**
-keep class com.rongke.**{*;}

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

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

-dontwarn chat.demo.entity.**
-keep class chat.demo.entity.**{*;}

-dontwarn av.demo.entity.**
-keep class av.demo.entity.**{*;}

 -dontwarn com.google.zxing.**
 -keep class com.google.zxing.**{*;}

-dontwarn com.lking.demo.entity.**
-keep class com.lking.demo.entity.**{*;}

-keep class com.youth.banner.** {
    *;
 
}

-keepattributes *JavascriptInterface*
-keepclassmembers class com.lking.demo.ui.find.JstoAndroid$JsInterface {
  public *;
}


-keepattributes *JavascriptInterface*
-keepclassmembers class com.lking.androidlib.widget.WebView$*{
    *;
}

#facebook fresco
-keep,allowobfuscation @interface com.facebook.common.internal.DoNotStrip

# Do not strip any method/class that is annotated with @DoNotStrip
-keep @com.facebook.common.internal.DoNotStrip class *
-keepclassmembers class * {
    @com.facebook.common.internal.DoNotStrip *;
}
# Keep native methods
-keepclassmembers class * {
    native <methods>;
}
-dontwarn okio.**
-dontwarn javax.annotation.**
-dontwarn com.android.volley.toolbox.**

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值