android 混淆和去除無用資源
阿新 • • 發佈:2019-01-03
1 android studio 清除無用資原始檔
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
debug {
minifyEnabled true // 是否混淆
shrinkResources true // 是否去除無效的資原始檔
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro' // 混淆規則
}
}
混淆規則配置
proguard-rules.pro 此目錄app資料夾下,需新建
常用混淆規則(copy至proguard-rules.pro)
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in F:\android_studio_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 # 是否使用大小寫混合
-dontpreverify # 混淆時是否做預校驗
-verbose # 混淆時是否記錄日誌
-dontskipnonpubliclibraryclasses
-keepattributes SourceFile,LineNumberTable
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/* # 混淆時所採用的演算法
-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 # 保持哪些類不被混淆
-keepclasseswithmembernames class * { # 保持 native 方法不被混淆
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);
}
-keepclassmembers enum * { # 保持列舉 enum 類不被混淆
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable { # 保持 Parcelable 不被混淆
public static final android.os.Parcelable$Creator *;
}
#如果引用了v4或者v7包
-dontwarn android.support.**
-dontwarn android.support.v4.**
-keep class android.support.v4.** { *; }
-keep public class * extends android.support.v4.**
-keep public class * extends android.app.Fragment
#如果引用了xutils架包
-dontwarn com.lidroid.xutils.**
-keep class com.lidroid.xutils.**{*;}
#使用個推
-dontwarn com.igexin.**
-keep class com.igexin.**{*;}
#使用picasso
-dontwarn com.squareup.picasso.**
-keep class com.squareup.picasso.**{*;}
#使用yuv
-dontwarn com.yuv.**
-keep class com.yuv.**{*;}
#針對xutils2.0包在SDK 23下的問題
-dontwarn org.apache.http.**
-keep class org.apache.http.**{*;}
##如果使用了gson
#-dontwarn com.google.gson.**
#-keep class com.google.gson.**{*;}
#-keep class sun.misc.Unsafe {*;}
##百度地圖
#-dontwarn com.baidu.**
#-keep class com.baidu.**{*;}
##支付寶
#-dontwarn com.alipay.**
#-keep class com.alipay.**{*;}
#
#-dontwarn com.ta.utdid2.**
#-keep class com.ta.utdid2.**{*;}
#
#-dontwarn com.ut.device.**
#-keep class com.ut.device.**{*;}
##nineoldandrois包
#-dontwarn com.nineoldandroids.**
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
debug {
minifyEnabled true // 是否混淆
shrinkResources true // 是否去除無效的資原始檔
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro' // 混淆規則
}
}
混淆規則配置
proguard-rules.pro 此目錄app資料夾下,需新建
常用混淆規則(copy至proguard-rules.pro)
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in F:\android_studio_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 # 是否使用大小寫混合
-dontpreverify # 混淆時是否做預校驗
-verbose # 混淆時是否記錄日誌
-dontskipnonpubliclibraryclasses
-keepattributes SourceFile,LineNumberTable
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/* # 混淆時所採用的演算法
-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 # 保持哪些類不被混淆
-keepclasseswithmembernames class * { # 保持 native 方法不被混淆
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);
}
-keepclassmembers enum * { # 保持列舉 enum 類不被混淆
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable { # 保持 Parcelable 不被混淆
public static final android.os.Parcelable$Creator *;
}
#如果引用了v4或者v7包
-dontwarn android.support.**
-dontwarn android.support.v4.**
-keep class android.support.v4.** { *; }
-keep public class * extends android.support.v4.**
-keep public class * extends android.app.Fragment
#如果引用了xutils架包
-dontwarn com.lidroid.xutils.**
-keep class com.lidroid.xutils.**{*;}
#使用個推
-dontwarn com.igexin.**
-keep class com.igexin.**{*;}
#使用picasso
-dontwarn com.squareup.picasso.**
-keep class com.squareup.picasso.**{*;}
#使用yuv
-dontwarn com.yuv.**
-keep class com.yuv.**{*;}
#針對xutils2.0包在SDK 23下的問題
-dontwarn org.apache.http.**
-keep class org.apache.http.**{*;}
##如果使用了gson
#-dontwarn com.google.gson.**
#-keep class com.google.gson.**{*;}
#-keep class sun.misc.Unsafe {*;}
##百度地圖
#-dontwarn com.baidu.**
#-keep class com.baidu.**{*;}
##支付寶
#-dontwarn com.alipay.**
#-keep class com.alipay.**{*;}
#
#-dontwarn com.ta.utdid2.**
#-keep class com.ta.utdid2.**{*;}
#
#-dontwarn com.ut.device.**
#-keep class com.ut.device.**{*;}
##nineoldandrois包
#-dontwarn com.nineoldandroids.**
#-keep class com.nineoldandroids.**{*;}
// 注使用json時,混淆會導致json無法解析,應避免混淆json相關類庫