2017-10-15 108 views
1

我正在創建一個原生的android應用程序,我想發佈它。錯誤使用proguard retrofit2和GSON轉換器

我正在使用改造和Gson轉換器。也是一個搜索視圖。 因爲我已經設置了ProGuard的規則,通過改造的建議發佈:

# Add project specific ProGuard rules here. 
-dontwarn javax.** 
-keep class android.support.v7.widget.SearchView { *; } 

# Retrofit 
-dontwarn okhttp3.** 
-dontwarn okio.** 
# Platform calls Class.forName on types which do not exist on Android to determine platform. 
-dontnote retrofit2.Platform 
# Platform used when running on Java 8 VMs. Will not be used at runtime. 
-dontwarn retrofit2.Platform$Java8 
# Retain generic type information for use by reflection by converters and adapters. 
-keepattributes Signature 
# Retain declared checked exceptions for use by a Proxy instance. 
-keepattributes Exceptions 

在改造我用: ApiUtils.java

public static PBTService getPBTService(String BASE_URL) { 
     return RetrofitClient.getClient(BASE_URL).create(PBTService.class); 
    } 

Service.java

import br.com.adley.ipubg.data.Player; 
import retrofit2.Call; 
import retrofit2.http.GET; 
import retrofit2.http.Headers; 
import retrofit2.http.Path; 

public interface PBTService { 
    String API_KEY = "MYKEY"; 
    @Headers(API_KEY) 
    @GET("profile/pc/{nickname}") 
    Call<Player> getPlayerStatsByNickname(@Path("nickname") String nickname); 

} 

RetrofitClient.java

import retrofit2.Retrofit; 
import retrofit2.converter.gson.GsonConverterFactory; 

public class RetrofitClient { 

    private static Retrofit retrofit = null; 

    public static Retrofit getClient(String baseUrl) { 
     if (retrofit==null) { 
      retrofit = new Retrofit.Builder() 
        .baseUrl(baseUrl) 
        .addConverterFactory(GsonConverterFactory.create()) 
        .build(); 
     } 

     return retrofit; 
    } 
} 

在調試模式下或者當proguard處於脫機狀態時,它可以很好地工作。 但是,當我生成使用ProGuard簽署的應用程序和使用改裝做東西,我得到了以下錯誤:

java.lang.IllegalArgumentException: Unable to create converter for class br.com.adley.ipubg.a.a for method a.a 

我已經嘗試了很多的ProGuard CONFIGS的。我可能認爲這個錯誤與GSON轉換器有關。

我已經嘗試過:

#My retrofit service package 
-keep public class br.com.adley.ipubg.data.** {public private protected *;} 
-keep public class br.com.adley.ipubg.activities.MainActivity.** {public private protected *;} 
#others configs 
-keep class com.google.gson.** { ; } 
-keep class sun.misc.Unsafe { *; } 
-keep class com.google.gson.stream.** { *; }: 
-keep public class com.google.gson.** 
-keep public class com.google.gson.** {public private protected *;} 

UPDATE

我已經在GSON CONFIGS添加到親衛隊:

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

# Gson specific classes 
-dontwarn sun.misc.** 
#-keep class com.google.gson.stream.** { *; } 

# Application classes that will be serialized/deserialized over Gson 
-keep class br.com.adley.ipubg.data.models.MatchHistory.** { *; } 
-keep class br.com.adley.ipubg.data.models.Player.** { *; } 
-keep class br.com.adley.ipubg.data.models.Season.** { *; } 
-keep class br.com.adley.ipubg.data.models.Stats.** { *; } 

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

但仍得到一個錯誤:

java.lang.IllegalArgumentException: Unable to create converter for class br.com.adley.ipubg.data.models.Player for method a.a 
+1

Gson也有你似乎缺少的proguard規則https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg –

+0

感謝您的回答,我會檢查它。 – Adley

+0

不幸的是,仍然沒有工作:( – Adley

回答

0

檢查完評論後,我添加了GSON proguard規則和翻新。

另外,如果你有使用你的模型的包裝,要小心,它會導致轉換錯誤。

我最後的親衛隊的規則,現在的工作是:

# Add project specific ProGuard rules here. 
-assumenosideeffects class android.util.Log { 
    public static boolean isLoggable(java.lang.String, int); 
    public static int v(...); 
    public static int i(...); 
    public static int w(...); 
    public static int d(...); 
    public static int e(...); 
} 

-dontwarn org.ini4j.** # Ignore warning for missing classes in ini4j 
-dontwarn javax.** 
-keep class android.support.v7.widget.SearchView { *; } 

# Retrofit 
-dontwarn okhttp3.** 
-dontwarn okio.** 
-dontnote retrofit2.Platform 
-dontwarn retrofit2.Platform$Java8 
-keepattributes Signature 
-keepattributes Exceptions 

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

# Gson specific classes 
-dontwarn sun.misc.** 
#-keep class com.google.gson.stream.** { *; } 

# Application classes that will be serialized/deserialized over Gson 
-keep class br.com.adley.ipubg.data.** { *; } 
-keep class br.com.adley.ipubg.wrapper.** { *; } 

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

感謝所有球員的意見。