天天看點

Android studio--代碼混淆

版權聲明:本文為部落客原創文章,轉載請标明出處。 https://blog.csdn.net/chaoyu168/article/details/79003860

         混淆就是對釋出出去的程式進行重新組織和處理,使得處理後的代碼與處理前代碼完成相同的功能,而混淆後的代碼很難被反編譯,即使反編譯成功也很難得出程式的真正語義。被混淆過的程式代碼,仍然遵照原來的檔案格式和指令集,執行結果也與混淆前一樣,隻是混淆器将代碼中的所有變量、函數、類的名稱變為簡短的英文字母代号,在缺乏相應的函數名和程式注釋的況下,即使被反編譯,也将難以閱讀。同時混淆是不可逆的,在混淆的過程中一些不影響正常運作的資訊将永久丢失,這些資訊的丢失使程式變得更加難以了解。

過程大緻如下:在app下的build.gradle檔案中添加如下代碼(minifyEnabled 表示是否混淆,預設是false,這裡要記得設定成true):

其中proguard-android.txt檔案是本地sdk/tools/proguard檔案夾下的預設檔案;prguard-rules.pro檔案就是用來編寫混淆代碼的;接下來就是在prguard-rules.pro檔案中編寫混淆代碼:

# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in C:\Users\sdt1\AppData\Local\Android\sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# 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                                                              #不預校驗,如果需要預校驗,是-dontoptimize
-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.**                                             #去掉警告
-keep class android.support.v4.** { *; }                                    #過濾android.support.v4
-keep interface android.support.v4.app.** { *; }
-keep public class * extends android.support.v4.**
-keep public class * extends android.app.Fragment

-keep class org.apache.**{*;}                                               #過濾commons-httpclient-3.1.jar

-keep class com.fasterxml.jackson.**{*;}                                    #過濾jackson-core-2.1.4.jar等

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

-dontwarn com.baidu.**                                                      #去掉警告
-dontwarn com.baidu.mapapi.**
-keep class com.baidu.** {*;}                                               #過濾BaiduLBS_Android.jar
-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);
}           

如果混淆build提示 java.io.FileNotFoundException: ...\proguard-rules\release\aapt_rules.txt (系統找不到指定的路徑),解決辦法:在菜單欄選擇build

--> Clean Project ,然後在build --> Make Project即可,然後就能在這個路徑下找到這個aapt_rules.txt檔案了,再次build apk就不會報錯了。

繼續閱讀