為什么要把代碼混淆呢抠璃??
為了防止別銀反編譯,竊取你的勞動(dòng)成果。
代碼混淆就是在保證應(yīng)用的功能不變的情況下 把代碼格式打亂 字母刪減 ?比如:用a代替abc ?用b代替sss 各種亂 ?使別人根本無法看懂 ? 下面就來說說如何操作
代碼混淆其實(shí)很簡單 ,在項(xiàng)目的proguard-rules.pro文件里面添加基本的配置 和你項(xiàng)目中使用到j(luò)ar包的配置 或者依賴的庫的配置 ?具體配置看自己項(xiàng)目里都用了什么 ?網(wǎng)上也有一大堆基本配置可以參考孵睬,配置好之后直接開始打包簽名就可以了
下面是我寫的一個(gè)小項(xiàng)目里的proguard-rules.pro文件里的所有內(nèi)容 直接粘貼?
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in D:\Android\AndroidSDK\newsdk/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 *;
#}
# Glide圖片庫的混淆處理
-keeppublic class * implements com.bumptech.glide.module.GlideModule
-keeppublic enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
**[] $VALUES;
public *;
}
-keepattributesSignature
# Gson混淆腳本
#-keepattributes Signature-keepattributes *Annotation*
-keepclass sun.misc.Unsafe {*;}
-keepclass com.google.gson.stream.** {*;}
#使用Gson時(shí)需要配置Gson的解析對象及變量都不混淆。不然Gson會找不到變量伶跷。
#將下面替換成自己的實(shí)體類
-keepclass com.example.administrator.duguodong.bean.** {*;}
# OkHttp3
-dontwarncom.squareup.okhttp3.**
-keepclass com.squareup.okhttp3.** {*;}
-dontwarnokio.**
# Okio
-dontwarncom.squareup.**
-dontwarnokio.**
-keeppublic class org.codehaus.* {*;}
-keeppublic class java.nio.* {*;}
#上面是導(dǎo)入的model掰读。同樣的其他的也是如此
-keepclass com.github.** {*;}
#上面這個(gè)是避免混淆你導(dǎo)入的maven類庫之類的
-dontwarncom.example.administrator.duguodong.**
#生成對應(yīng)的混淆信息
#注意的是fastJson要特殊對待
#指定代碼的壓縮級別
-optimizationpasses5
#包明不混合大小寫
-dontusemixedcaseclassnames
#不去忽略非公共的庫類
-dontskipnonpubliclibraryclasses
#優(yōu)化? 不優(yōu)化輸入的類文件
-dontoptimize
#不做預(yù)校驗(yàn)
-dontpreverify
#混淆時(shí)是否記錄日志
-verbose
#混淆時(shí)所采用的算法
-optimizations!code/simplification/arithmetic,!field/*,!class/merging/*
#忽略警告
-ignorewarning
#apk包內(nèi)所有class的內(nèi)部結(jié)構(gòu)
#? -dump class_files.txt
#未混淆的類和成員
#? -printseeds seeds.txt
#列出從apk中刪除的代碼
#? -printusage unused.txt
#混淆前后的映射
#? -printmapping mapping.txt
# -------------系統(tǒng)類不需要混淆--------------------------
-keeppublic class * extends android.app.Fragment
-keeppublic class * extends android.app.Activity
-keeppublic class * extends android.app.Application
-keeppublic class * extends android.app.Service
-keeppublic class * extends android.content.BroadcastReceiver
-keeppublic class * extends android.content.ContentProvider
-keeppublic class * extends android.app.backup.BackupAgentHelper
-keeppublic class * extends android.preference.Preference
-keeppublic class * extends android.support.**
-keeppublic class com.android.vending.licensing.ILicensingService
#保留support下的所有類及其內(nèi)部類
-keepclass android.support.** {*;}
#保留R下面的資源
-keepclass **.R$* {*;}
-keepclasseswithmembernamesclass * {#保持native方法不被混淆
native ;
}
-keepclasseswithmembersclass * {#保持自定義控件類不被混淆
public (android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembersclass * {#保持自定義控件類不被混淆
public (android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembersclass * extends android.app.Activity {#保持自定義控件類不被混淆
public void *(android.view.View);
}
-keepclassmembersenum * {#保持枚舉enum類不被混淆
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keepclass * implements android.os.Parcelable {#保持Parcelable不被混淆
public static final android.os.Parcelable$Creator *;
}