Does not work when use ProGuard

2019-04-03 01:28发布

The logcat is below. what is wrong?

07-24 10:54:13.529: E/AndroidRuntime(12130): FATAL EXCEPTION: AsyncTask #3

07-24 10:54:13.529: E/AndroidRuntime(12130): java.lang.RuntimeException: An error occured while executing doInBackground()

07-24 10:54:13.529: E/AndroidRuntime(12130): at android.os.AsyncTask$3.done(AsyncTask.java:299)

07-24 10:54:13.529: E/AndroidRuntime(12130): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)

07-24 10:54:13.529: E/AndroidRuntime(12130): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)

07-24 10:54:13.529: E/AndroidRuntime(12130): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)

07-24 10:54:13.529: E/AndroidRuntime(12130): at java.util.concurrent.FutureTask.run(FutureTask.java:137)

07-24 10:54:13.529: E/AndroidRuntime(12130): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)

07-24 10:54:13.529: E/AndroidRuntime(12130): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)

07-24 10:54:13.529: E/AndroidRuntime(12130): at java.lang.Thread.run(Thread.java:856)

07-24 10:54:13.529: E/AndroidRuntime(12130): Caused by: java.lang.AssertionError: illegal type variable reference

07-24 10:54:13.529: E/AndroidRuntime(12130): at org.apache.harmony.luni.lang.reflect.ImplForVariable.resolve(ImplForVariable.java:113)

07-24 10:54:13.529: E/AndroidRuntime(12130): at org.apache.harmony.luni.lang.reflect.ImplForVariable.getGenericDeclaration(ImplForVariable.java:127)

07-24 10:54:13.529: E/AndroidRuntime(12130): at org.apache.harmony.luni.lang.reflect.ImplForVariable.hashCode(ImplForVariable.java:46)

07-24 10:54:13.529: E/AndroidRuntime(12130): at java.util.Arrays.hashCode(Arrays.java:1260)

07-24 10:54:13.529: E/AndroidRuntime(12130): at com.google.gson.internal.$Gson$Types$ParameterizedTypeImpl.hashCode(SourceFile:475)

07-24 10:54:13.529: E/AndroidRuntime(12130): at com.google.gson.reflect.TypeToken.(SourceFile:64)

07-24 10:54:13.529: E/AndroidRuntime(12130): at com.gaozhi.wh.asynctask.MyAsyncTask$2$1.(SourceFile:78)

07-24 10:54:13.529: E/AndroidRuntime(12130): at com.gaozhi.wh.asynctask.MyAsyncTask$2.call(SourceFile:78)

07-24 10:54:13.529: E/AndroidRuntime(12130): at com.gaozhi.wh.asynctask.MyAsyncTask$2.call(SourceFile:1)

07-24 10:54:13.529: E/AndroidRuntime(12130): at com.gaozhi.wh.utils.async.AsyncTaskUtils$2.doInBackground(SourceFile:114)

07-24 10:54:13.529: E/AndroidRuntime(12130): at com.gaozhi.wh.utils.async.AsyncTaskUtils$2.doInBackground(SourceFile:1)

07-24 10:54:13.529: E/AndroidRuntime(12130): at android.os.AsyncTask$2.call(AsyncTask.java:287)

07-24 10:54:13.529: E/AndroidRuntime(12130): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 07-24 10:54:13.529: E/AndroidRuntime(12130): ... 4 more

I found that is caused by this line of code error

info = GsonUtils.getMutileBean(result,new TypeToken<JsonResult<T>>() {}.getType());

public static <T> T getMutileBean(String jsonData, Type type)
        throws Exception {
    return new Gson().fromJson(jsonData, type);
}

.

But I had add 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.** { *; }

2条回答
爷的心禁止访问
2楼-- · 2019-04-03 01:54

You may need to add

-keepattributes EnclosingMethod
查看更多
Explosion°爆炸
3楼-- · 2019-04-03 01:58

Adding the EnclosingMethod to proguard config did not help me.

However by keeping the attribute InnerClasses, I was able to build a properly functioning APK

查看更多
登录 后发表回答