java - 使用 proguard 混淆时使用 Retrofit 的自定义错误处理给出 java.lang.reflect.UndeclaredThrowableException

标签 java android proguard obfuscation retrofit

为改造编写了自定义错误处理。当 minifyEnabled false 时,代码可以完美运行。当我启用 proguard 时,出现以下异常

12-17 10:14:07.688  18568-19041/com.mobility.cariq.carscore W/System.err﹕ java.lang.reflect.UndeclaredThrowableException
12-17 10:14:07.688  18568-19041/com.mobility.cariq.carscore W/System.err﹕ at $Proxy9.updatesForModel(Native Method)
12-17 10:14:07.688  18568-19041/com.mobility.cariq.carscore W/System.err﹕ at com.mobility.cariq.carscore.rest.service.UpdateDatabaseService.jiijijliillliliilllil(UpdateDatabaseService.java:119)
12-17 10:14:07.688  18568-19041/com.mobility.cariq.carscore W/System.err﹕ at com.mobility.cariq.carscore.rest.service.UpdateDatabaseService.onHandleIntent(UpdateDatabaseService.java:75)
12-17 10:14:07.689  18568-19041/com.mobility.cariq.carscore W/System.err﹕ at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
12-17 10:14:07.689  18568-19041/com.mobility.cariq.carscore W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
12-17 10:14:07.689  18568-19041/com.mobility.cariq.carscore W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
12-17 10:14:07.689  18568-19041/com.mobility.cariq.carscore W/System.err﹕ at android.os.HandlerThread.run(HandlerThread.java:61)
12-17 10:14:07.689  18568-19041/com.mobility.cariq.carscore W/System.err﹕ Caused by: com.mobility.cariq.carscore.rest.error.UnauthorizedException
12-17 10:14:07.690  18568-19041/com.mobility.cariq.carscore W/System.err﹕ at com.mobility.cariq.carscore.rest.error.RestErrorHandler.handleError(RestErrorHandler.java:40)
12-17 10:14:07.690  18568-19041/com.mobility.cariq.carscore W/System.err﹕ at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:242)

错误处理程序

public class RestErrorHandler implements ErrorHandler {

@Override
public Throwable handleError(RetrofitError cause) {
    Response r = cause.getResponse();

    if (r != null && r.getStatus() == 400) {

        try {
            RestError mRestError = (RestError) cause.getBodyAs(RestError.class);
            final String exception = mRestError.getMessages().get(0);
            return new UnauthorizedException(exception);
        } catch (Exception e) {
            e.printStackTrace();
            LogUtility.NoteLog(e);
        }
    }
    return cause;
}

混淆器

-obfuscationdictionary keywords.txt
-classobfuscationdictionary keywords.txt
-packageobfuscationdictionary keywords.txt

-dontwarn android.telephony.**
-keepattributes SourceFile,LineNumberTable

-dontwarn butterknife.internal.**
-keep class **$$ViewInjector { *; }
-keepnames class * { @butterknife.InjectView *;}

-keepattributes Signature
-keepattributes *Annotation*
-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }
-dontwarn com.squareup.okhttp.**

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

-keep class sun.misc.Unsafe { *; }
-keep class com.mobility.cariq.carscore.rest.model.** { *; }
-keep class com.mobility.cariq.carscore.rest.error.** { *; }
-dontwarn java.nio.file.**
-dontwarn org.codehaus.mojo.**

-dontwarn org.joda.convert.**

我对使用 proguard 和 retrofit 还很陌生。我无法理解如何处理异常。

最佳答案

Proguard 默认移除 Exception 属性。

-keepattributes Exceptions 将确保您的 Throwable 在混淆后保留在您的代码中。

关于java - 使用 proguard 混淆时使用 Retrofit 的自定义错误处理给出 java.lang.reflect.UndeclaredThrowableException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27523717/

相关文章:

java - 尝试使用 Tab 在 JTextField 之间垂直跳转(使用 Custom FocusTraversalPolicy)

java - 无法从 RecyclerView 中的 URL 加载图像

android - 未找到 native 的实现,java.lang.UnsatisfiedLinkError

android - 如何在 build.gradle 中引用系统 ProGuard 二进制文件?

java - 为什么在无限循环时 while 和 do-while 循环优于 for 循环?

java - Struts 2 - 拦截使用CKEditor上传的图像文件

java - Android MapView方向箭头

android - 使用 Tomcat 服务器托管 android 和 web 应用程序打开太多连接

java - 如何使用 Proguard 或其他工具删除未使用的重写 toString() 方法?

java - proguard 解析 android.txt 文件中的异常