2016-11-11 83 views
0

proguard的麻煩我使用的序列化/反序列化對象GSON庫,當我建立在調試模式下禁用的ProGuard然後應用工作fine.Then我改變了建變種釋放和minifyEnabled設置爲true。 後,我得到了連接異常GSON:缺少類型參數 - 在Android應用

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的規則

 ##---------------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' 

誰能幫我解決這個問題一世ssue?我做錯了什麼?

回答

0

問題的原因是使用千斤頂工具鏈Java8。當我拒絕傑克並回到retrolambda時,GSON反射方法的問題消失了。