Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用proguard 2和GSON转换器时出错_Android_Proguard_Android Proguard - Fatal编程技术网

Android 使用proguard 2和GSON转换器时出错

Android 使用proguard 2和GSON转换器时出错,android,proguard,android-proguard,Android,Proguard,Android Proguard,我正在创建一个原生android应用程序,我想发布它 我正在使用改装和Gson转换器。还有一个搜索视图。 对于这个版本,我已经设置了proguard规则,正如改型建议的那样: # Add project specific ProGuard rules here. -dontwarn javax.** -keep class android.support.v7.widget.SearchView { *; } # Retrofit -dontwarn okhttp3.** -dontwarn

我正在创建一个原生android应用程序,我想发布它

我正在使用改装和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);

}
在调试模式下或proguard脱机时,它工作正常。 但是,当我使用proguard生成已签名的应用程序并使用改进版进行GET时,我得到以下错误:

java.lang.IllegalArgumentException: Unable to create converter for class br.com.adley.ipubg.a.a for method a.a
我试过很多proguard配置。我可能认为这个错误与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 *;}
更新

我已将GSON配置添加到pro guard:

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

在检查评论之后,我添加了一个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  ----------

谢谢大家的评论。

Gson也有您似乎缺少的proguard规则。谢谢您的回答,我会检查它。遗憾的是,仍然不起作用:(您需要为您的模型添加keep类,该问题与这些库无关,您的日志清楚地表明它无法找到您的模式,因为它将在启用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  ----------