做Android開發的應該都知道,如果APK沒有混淆打包,裡面的代碼反編譯之後就跟明文沒什麼區別了,最近用proguard混淆用的比較多,這裡記錄下,以免以後忘了查看
1. 基本配置
eclipse下建立android工程,就會生成proguard.cfg和project.properties,在後面的文件追加proguard.config=proguard.cfg即可讓前面的配置文件在export時生效。默認的那個文件有一些內容,這裡給一個更通用點的。
[html]
##---------------Begin: proguard configuration common for all Android apps ----------
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-keepattributes *Annotation*
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
# 以下兩個命令配合讓類的路徑給刪除了
-allowaccessmodification
-repackageclasses ''
# 記錄生成的日志數據,在proguard目錄下
-dump class_files.txt
-printseeds seeds.txt
-printusage unused.txt
-printmapping mapping.txt
# 異常都可以忽略就打開
#-dontwarn
-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
-dontnote com.android.vending.licensing.ILicensingService
-keepnames class * implements java.io.Serializable
# Explicitly preserve all serialization members. The Serializable interface
# is only a marker interface, so it wouldn't save them.
-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();
}
# Preserve all native method names and the names of their classes.
-keepclasseswithmembernames class * {
native <methods>;
}
-keepclasseswithmembernames class * {
public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembernames class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}
# Preserve static fields of inner classes of R classes that might be accessed
# through introspection.
-keepclassmembers class **.R$* {
public static <fields>;
}
# Preserve the special static methods that are required in all enumeration classes.
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
# 如果你的工程是對外提供方法調用就打開
#-keep public class * {
# public protected *;
#}
##---------------End: proguard configuration common for all Android apps ----------
2. 解決export打包的報錯
這個時候export提示“conversion to Dalvik format failed with error 1”錯誤,網上說法有好多種,最後我還是把proguard從4.4升級到4.8就解決了。官方地址是http://proguard.sourceforge.net。上面的配置文件參數可以在這裡查閱。
升級辦法很簡單,就是把android sdk目錄下的tool/proguard目錄覆蓋一下即可。
3. 打包出來的程序如何調試
一旦打包出來,就不能用eclipse的logcat去看了,這裡可以用android sdk中ddms.bat的tool來看,一用就發現和logcat其實還是一個東西,就是多了個設備的選擇。
在android上最好去下載一個logcat閱讀器,這樣在手機上運行崩潰了,不用連電腦也能查看日志了。可以再這裡下載http://static.apk.hiapk.com/html/2012/03/438120.html。
4. 使用 gson 需要的配置
當Gson用到了泛型就會有報錯,這個真給郁悶了半天,提示“Missing type parameter”。最後找到一個資料給了一個解決辦法,參考:http://stackoverflow.com/questions/8129040/proguard-missing-type-parameter。
另外我又用到了JsonObject,提交的Object裡面的members居然被改成了a。所以上面給的東西還不夠,還要加上
[html]
# 用到自己拼接的JsonObject
-keep class com.google.gson.JsonObject { *; }
我個人建議減少這些依賴包混淆帶來的麻煩,干脆都全部保留不混淆。例如
[html]
-keep class com.badlogic.** { *; }
-keep class * implements com.badlogic.gdx.utils.Json*
-keep class com.google.** { *;
注意:就算這樣配置了,在混淆打包的時候,gson依然可能會報錯,如果還報錯,請加上如下代碼:
[html] www.2cto.com
##---------------Begin: proguard configuration for Gson ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature
# For using GSON @Expose annotation
-keepattributes *Annotation*
# Gson specific classes
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }
# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }
##---------------End: proguard configuration for Gson ----------