Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Error Handling with Retrofit when obfuscated using proguard gives java.lang.reflect.UndeclaredThrowableException

Have written custom error handling for retrofit. The code works perfectly when minifyEnabled false. When I enable proguard, I get the following exception

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)

Error Handler

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

I am fairly new to using proguard and retrofit. I am unable to understand, how to handle the exception.

like image 755
iZBasit Avatar asked Jan 10 '23 06:01

iZBasit


2 Answers

Proguard removes Exception attributes by default.

-keepattributes Exceptions will make sure that your Throwable remains in your code after obfuscation.

like image 65
Alim Parkar Avatar answered Jan 23 '23 18:01

Alim Parkar


Looks like UnauthorizedException is a checked exception (extends from Exception, not RuntimeException) which means you need to declare a throws clause on all of your service interfaces.

interface MyService {
  @GET("/")
  Something doSomething() throws UnauthorizedException;
}
like image 42
Jake Wharton Avatar answered Jan 23 '23 17:01

Jake Wharton