我使用Gson libs进行序列化/反序列化对象,当我在调试模式下使用proguard禁用时,app工作正常。然后我将build variant更改为release并将minifyEnabled设置为true。 之后我得到了例外
E/AndroidRuntime: FATAL EXCEPTION: main
Process: test.android, PID: 30168
java.lang.ExceptionInInitializerError
at test.android.storage.Prefs.restoreInternalStudent(Unknown Source)
at test.android.activities.a.e(Unknown Source)
at test.android.activities.MainActivity.r(Unknown Source)
at test.android.activities.MainActivity.w(Unknown Source)
at test.android.activities.e.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Caused by: java.lang.RuntimeException: Missing type parameter.
at com.google.gson.reflect.TypeToken.getSuperclassTypeParameter(Unknown Source)
at com.google.gson.reflect.TypeToken.<init>(Unknown Source)
at com.google.gson.q.<init>(Unknown Source)
at com.google.gson.Gson.<clinit>(Unknown Source)
at test.android.storage.Prefs.restoreInternalStudent(Unknown Source) ...
proguard rules
##---------------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*
-keepattributes EnclosingMethod
-keepattributes InnerClasses
# Gson specific classes
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }
# Also you must note that if you are using GSON for conversion from JSON to POJO representation, you must ignore those POJO classes from being obfuscated.
# Here include the POJO's that have you have created for mapping JSON response to POJO for example.
-keep class test.android.api.models.** { *; }
-keep class test.android.api.request.** { *; }
-keep class test.android.models.** { *; }
-keepclasseswithmembers class test.android.storage.Prefs {*;}
-keepclasseswithmembers class com.google.gson.** {*;}
-keep class com.google.gson.examples.android.model.** { *; }
# Prevent proguard from stripping interface information from TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer
##---------------End: proguard configuration for Gson ----------
Prefs.java
...
public static void storeInternalStudent(InternalStudent internalStudent) {
Gson gson = new Gson();
String json = gson.toJson(internalStudent);
shared.edit().putString(SELECTED_INTERNAL_STUDENT_KEY, json).apply();
}
public static InternalStudent restoreInternalStudent() {
Gson gson = new Gson();
String json = shared.getString(SELECTED_INTERNAL_STUDENT_KEY, "");
return gson.fromJson(json, InternalStudent.class);
}
...
的build.gradle
...
jackOptions {
enabled true
}
...
compileOptions {
incremental true
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
...
compile 'com.google.code.gson:gson:2.8.0'
有人可以帮我解决这个问题吗?我做错了什么?
答案 0 :(得分:0)
问题的原因是使用Jack工具链进行Java8。当我拒绝杰克并回到retrolambda时,GSON反射方法的问题就消失了。