2014年8月29日

proguard and gson

今天「股海快訊」安卓 v1.4.1 上架後,即發現會有以下的錯誤:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.RuntimeException: Missing type parameter.
at com.google.c.c.a.getSuperclassTypeParameter(Unknown Source)
at com.google.c.c.a.<init>(Unknown Source)
at tw.doitwell.xxx



而這個錯誤只有在 RELEASE 版會出錯,DEBUG 下不會有問題,問題出在 proguard 會使 gson 出問題,所以,要在 proguard-rules.txt 內加上以下這幾行即可。
# 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

# 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.** { *; }


Orignal From: proguard and gson