与retrofit2和rxjava

时间:2017-06-12 09:22:52

标签: android proguard retrofit2

当我禁用proguard时,它正常工作,但是当启用proguard时会抛出异常:

java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread.

这是我的代码:

  private fun getData(email : String) {
    mSubscriptions!!.add(NetworkUtil.retrofit.getProfile(email)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe({ this.handleResponse(it) }, { this.handleError(it) }))
}
private fun handleResponse(user: List<User>) {
    System.out.println(user.get(0).email)
}
private fun handleError(error: Throwable) {
    if (error is HttpException) {
        val gson = GsonBuilder().create()
        try {
            val errorBody = error.response().errorBody()!!.string()
            val response = gson.fromJson<Response>(errorBody, Response::class.java)
            showSnackBarMessage(response.message)
        } catch (e: IOException) {
            e.printStackTrace()
        }
    } else {
        showSnackBarMessage("Network Error !")
    }
}

这是例外:

FATAL EXCEPTION: main

Process: sega.com.myapplication, PID: 7784
java.lang.IllegalStateException: Fatal Exception thrown on         Scheduler.Worker thread.
at rx.android.schedulers.LooperScheduler$ScheduledAction.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: rx.a.e: Error occurred when trying to propagate error to     Observer.onError
at rx.d.a.a(Unknown Source)
at rx.d.a.onError(Unknown Source)
at rx.c.a.g$a.onError(Unknown Source)
at rx.c.a.f$a.a(Unknown Source)
at rx.c.a.f$a.a(Unknown Source)
at rx.android.schedulers.LooperScheduler$ScheduledAction.run(Unknown Source) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
Caused by: rx.a.a: 2 exceptions occurred. 
at rx.d.a.a(Unknown Source) 
at rx.d.a.onError(Unknown Source) 
at rx.c.a.g$a.onError(Unknown Source) 
at rx.c.a.f$a.a(Unknown Source) 
at rx.c.a.f$a.a(Unknown Source) 
at rx.android.schedulers.LooperScheduler$ScheduledAction.run(Unknown Source) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
Caused by: rx.a.a$a: Chain of Causes for CompositeException In Order Received =>
at android.util.Log.printlns(Log.java:415)
at com.android.internal.os.RuntimeInit.Clog_e(RuntimeInit.java:61)
at com.android.internal.os.RuntimeInit.-wrap0(RuntimeInit.java)
at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:86)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1068)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1063)
at rx.android.schedulers.LooperScheduler$ScheduledAction.run(Unknown Source) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
Caused by: retrofit2.adapter.rxjava.HttpException: HTTP 400 Bad Request
at retrofit2.adapter.rxjava.BodyOnSubscribe$BodySubscriber.onNext(Unknown Source)
at retrofit2.adapter.rxjava.BodyOnSubscribe$BodySubscriber.onNext(Unknown Source)
at retrofit2.adapter.rxjava.CallArbiter.deliverResponse(Unknown Source)
at retrofit2.adapter.rxjava.CallArbiter.emitResponse(Unknown Source)
at retrofit2.adapter.rxjava.CallExecuteOnSubscribe.call(Unknown Source)
at retrofit2.adapter.rxjava.CallExecuteOnSubscribe.call(Unknown Source)
at retrofit2.adapter.rxjava.BodyOnSubscribe.call(Unknown Source)
at retrofit2.adapter.rxjava.BodyOnSubscribe.call(Unknown Source)
at rx.d.a(Unknown Source)
at rx.c.a.g$a.a(Unknown Source)
at rx.c.c.a$b$1.a(Unknown Source)
at rx.c.c.f.run(Unknown Source)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Caused by: b.a
at sega.com.myapplication.activity.LoginActivity.b(Unknown Source)
at sega.com.myapplication.activity.LoginActivity.a(Unknown Source)
at sega.com.myapplication.activity.LoginActivity.a(Unknown Source)                                                                                          at sega.com.myapplication.activity.LoginActivity$e.a(Unknown Source)                                                                                      at sega.com.myapplication.activity.LoginActivity$e.call(Unknown Source)                                                                                  at rx.c.e.a.onError(Unknown Source)
at rx.d.a.a(Unknown Source)
at rx.d.a.onError(Unknown Source)
at rx.c.a.g$a.onError(Unknown Source)
at rx.c.a.f$a.a(Unknown Source)
at rx.c.a.f$a.a(Unknown Source)
at rx.android.schedulers.LooperScheduler$ScheduledAction.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

我有一个测试,使用编译'com.artemzin.rxjava:proguard-rules:1.3.0.0'但它仍然无效。

1 个答案:

答案 0 :(得分:0)

尝试将此添加到您的proguard-rules.pro文件

## Retrofit
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions

## RxJava
-keepclassmembers class rx.internal.util.unsafe.*ArrayQueue*Field* {
    long producerIndex;
    long consumerIndex;
}
-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueProducerNodeRef {
    rx.internal.util.atomic.LinkedQueueNode producerNode;
}
-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueConsumerNodeRef {
    rx.internal.util.atomic.LinkedQueueNode consumerNode;
}
-dontwarn sun.misc.Unsafe