使用Proguard配置来使用Retrofit

3

我的应用在调试模式下运行良好,但在创建发布版的apk时出现以下错误。

Process: neocom.dealerbook, PID: 9044
    java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
            at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
            at java.util.ArrayList.get(ArrayList.java:308)
            at neocom.dealerbook.controller.k.a(MapActivity.java:103)
            at neocom.dealerbook.controller.k.success(MapActivity.java:98)
            at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

这里是MapActivity.java的第103行:
Callback<ClienteConfiguracao> callback = new Callback<ClienteConfiguracao>() {
                int failCount = 0;
            @Override
            public void success(ClienteConfiguracao clienteConfiguracao, Response response) {
                currentClient = clienteConfiguracao.getDealerships().get(0); /* Line 103 */
                setupToggles(currentClient);

                List<String> mAuthorization = clienteConfiguracao.getAuthorization();
                toAllowPermissions = new HashMap<>();
                Set<String> allImplementedKeys = PermissionManager.ALL_PERMISSIONS.keySet();
                for (String key : mAuthorization) {
                    if (allImplementedKeys.contains(key)) {
                        List value = PermissionManager.ALL_PERMISSIONS.get(key);
                        toAllowPermissions.put(key, value);
                    }
                }

这是我构建的proguard文件,用来搜索我得到的错误,包括Retrofit网站和其他内容。
-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 retrofit.**
-dontwarn com.squareup.okhttp.**
-dontwarn okio.**
-dontwarn retrofit.appengine.UrlFetchClient
-keep class retrofit.** { *; }
-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }
-keepattributes Signature
-keepattributes Exceptions
-keepattributes *Annotation*
-keepattributes SourceFile
-keepattributes LineNumberTable


-keepclasseswithmembers class * {
    @retrofit.http.* <methods>;
}

有没有关于这方面的小技巧?


你在哪一行代码出现了错误? - Lajos Arpad
在第103行,第二个代码片段的行末有一条注释。 - wviana
3个回答

13

在使用retrofit和gson时,重要的是为那些被序列化反序列化的类添加Progard规则。

例如:

-keep class com.example.model.** { *; }

PS:另一个建议是使用此存储库中的规则片段(备用链接)。这里有许多不同库的规则。


你是一个救命稻草。 - Zohar

0

问题出在这里

currentClient = clienteConfiguracao.getDealerships().get(0);

clienteConfiguracao.getDealerships() 是空的。你需要检查它是否有一些元素。

if (clienteConfiguracao.getDealerships().size() > 0) {
    clienteConfiguracao.getDealerships().get(0);
} else {
    //handle empty ArrayList
}

如果它变为空了,那就是GSON或者Proguard中的Retrofit问题。因为我确信如果Retrofit成功返回,那么一定会有一个值。而且当没有生成签名/混淆APK时,这个错误永远不会发生。 - wviana

0

只需添加 -keep class com.google.gson.** { *; }


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接