【问题标题】:Custom Error Handling with Retrofit when obfuscated using proguard gives java.lang.reflect.UndeclaredThrowableException使用 proguard 混淆时使用 Retrofit 进行自定义错误处理会给出 java.lang.reflect.UndeclaredThrowableException
【发布时间】:2014-12-17 10:43:55
【问题描述】:

为改造编写了自定义错误处理。代码在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;
}

Proguard

-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 和改造还很陌生。我无法理解,如何处理异常。

【问题讨论】:

    标签: java android proguard obfuscation retrofit


    【解决方案1】:

    Proguard 默认移除 Exception 属性。

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

    【讨论】:

      【解决方案2】:

      看起来UnauthorizedException 是一个检查异常(从Exception 扩展,而不是RuntimeException),这意味着您需要在所有服务接口上声明throws 子句。

      interface MyService {
        @GET("/")
        Something doSomething() throws UnauthorizedException;
      }
      

      【讨论】:

      • 我已经做到了,杰克。当我混淆我的代码时它停止工作。在没有 proguard 的情况下,它可以完美运行。
      猜你喜欢
      • 1970-01-01
      • 2012-01-14
      • 2019-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-11
      • 1970-01-01
      相关资源
      最近更新 更多