将 proguard 与 retrofit2 和 rxjava 一起使用时出错

Posted

技术标签:

【中文标题】将 proguard 与 retrofit2 和 rxjava 一起使用时出错【英文标题】:Error when used proguard with retrofit2 and rxjava 【发布时间】:2017-11-13 17:12:26 【问题描述】:

当我禁用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)

我进行了测试,使用 compile 'com.artemzin.rxjava:proguard-rules:1.3.0.0' 但它仍然无法正常工作。

【问题讨论】:

【参考方案1】:

尝试将此添加到您的 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

【讨论】:

它仍然错误:(我不知道为什么我得到错误:retrofit2.adapter.rxjava.HttpException: HTTP 400 Bad Request 当我使用 proguard 时 您是否从 proguard 中排除了您的模型和请求? 天哪!我忘了 。非常感谢你 。当我花了 4 个小时时,你是我的救援,但我不知道为什么会出现这个错误 :)

以上是关于将 proguard 与 retrofit2 和 rxjava 一起使用时出错的主要内容,如果未能解决你的问题,请参考以下文章

ProGuard for Android 和 Retrofit2 Converter Gson?

Retrofit2 - 缺少 @GET URL 或 @Url 参数 - Proguard

Proguard - PersistenceException:构造函数与类不匹配

Proguard & Retrofit 错误

启用 ProGuard 时改造 2 不发送数据

ProtocolException:太多后续请求:Android 中使用 Proguard 和 OkHttp 3.0 的 21 个