Android 代碼混淆的實踐
開發 apk 的時候當然要考慮保護好自己的代碼,Android 環境就提供了 ProGuard 來進行代碼混淆,確實是一個非常有用的工具,但用起來也確實夠折騰的。
1. 基本配置
eclipse 下建立 android 工程,就會生成 proguard.cfg 和 project.properties,在后面的文件追加 proguard.config=proguard.cfg 即可讓前面的配置文件在 export 時生效。默認的那個文件有一些內容,這里給一個更通用點的。
##—————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 ;
}
-keepclasseswithmembernames class * {
public (android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembernames class * {
public (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 ;
}
# 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 其實還是一個東西,就是多了個設備的選擇。
4. 使用 gson 需要的配置
當 Gson 用到了泛型就會有報錯,這個真給郁悶了半天,提示“Missing type parameter”。最后找到一個資料給了一個解決辦法,參考:http://stackoverflow.com/questions /8129040/proguard-missing-type-parameter。
另外我又用到了 JsonObject,提交的 Object 里面的 members 居然被改成了a。所以上面給的東西還不夠,還要加上
# 用到自己拼接的 JsonObject
-keep class com.google.gson.JsonObject { *; }
我個人建議減少這些依賴包混淆帶來的麻煩,干脆都全部保留不混淆。例如
-keep class com.badlogic.** { *; }
-keep class * implements com.badlogic.gdx.utils.Json*
-keep class com.google.** { *; }
5. 使用 libgdx 需要的配置
參考 http://code.google.com/p/libgdx-users/wiki/Ant
6. 驗證打包效果
我是利用了 apktool 的反編譯工具,把打包文件又解壓了看了一下,如果包路徑、類名、變量名、方法名這些變化和你期望一致,那就 OK 了。命令:
apktool.bat d xxx.apk destdir
總結
這個東西用起來也不是很簡單,特別是你程序用到的高級特性多,就更容易出問題。另外 proguard 的參數看起來確實也有點不好理解,打包過程慢,測試也比較浪費時間。東西雖好,但真不是那么容易上手。