這幾天在對程式進行混淆處理測試時,在程式中使用了友盟元件的地方會報錯,比如自動更新,報錯異常如:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.umeng.update.UpdateDialogActivity}: java.lang.IllegalArgumentException: ResClass is not initialized. Please make sure you have added neccessary resources. Also make sure you have com.example.R$* configured in obfuscation. field=umeng_update_dialog
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: ResClass is not initialized. Please make sure you have added neccessary resources. Also make sure you have com.example.R$* configured in obfuscation. field=umeng_update_dialog
at com.umeng.common.Res.a(Unknown Source)
at com.umeng.common.Res.d(Unknown Source)
at com.umeng.update.UpdateDialogActivity.onCreate(Unknown Source)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
解決思路:
使用了混淆, 請添加
-keepclassmembers class * {
public <init>(org.json.JSONObject);
}
這是由于SDK中的部分代碼使用反射來調用構造函數, 如果被混淆掉, 在運作時會提示"NoSuchMethod"錯誤。 另外,由于SDK需要引用導入工程的資源檔案,通過了反射機制得到資源引用檔案R.java,但是在開發者通過proguard等混淆/優化工具處理apk時,proguard可能會将R.java删除,如果遇到這個問題,請在proguard配置檔案中添加keep指令如:
-keep public class [您的應用包名].R$*{
public static final int *;
}
把[您的應用包名] 替換成您自己的包名,如com.yourcompany.example。 如果您使用了雙向回報功能,還需要添加下面代碼,以免我們自定義的UI被混淆:
-keep public class com.umeng.fb.ui.ThreadView {
}
在proguadr-android.txt檔案中,添加如下代碼:
-keepclassmembers class * {
public <init>(org.json.JSONObject);
}
-keep public class com.example.R$*{
public static final int *;
}
至此,問題解決