Ankit Kumar
Ankit Kumar

Reputation: 101

Getting `java.lang.AssertionError: java.lang.NoSuchFieldException: HTTP_1_0`

In my project, I am performing API call using RxJava. Without proguard, it is running fine. But when I apply proguard It gives java.lang.AssertionError: java.lang.NoSuchFieldException: HTTP_1_0 in onError(e: Throwable) of a subscriber.

I applied -keepclassmembers enum * { *; } in my proguard to prevent obfuscation.

Api Call

 fun latestPosts(): Subscription {
        return service.latestPosts
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(APICallSubscriber(presenterContract, ApiIndex.POSTS))
    }

Subscriber


class APICallSubscriber<T>(private val callback: BasePresenterContract,
                           private val apiIndex: String) : Subscriber<Response<T>>() {

    override fun onCompleted() {

    }

    override fun onError(e: Throwable) {
        Log.d("HomeTest", "${e}")
    }

    override fun onNext(response: Response<T>) {
        val jsonObject = App.gson().toJsonTree(response).asJsonObject
        val responseCode = jsonObject
                .get(PayloadKeys.RAW_RESPONSE).asJsonObject
                .get(PayloadKeys.CODE).asInt
        Log.d("HomeTest", "$jsonObject")
        val body: JsonElement? = jsonObject.get(PayloadKeys.BODY)
        if (body != null) {
            val responseBody = body.asJsonObject
            callback.onNetworkRequestCompletedWith(responseBody, responseCode, apiIndex)
        } else {
            val errorBody: JsonElement? = jsonObject.get(PayloadKeys.ERROR_BODY)
            callback.onNetwordRequestError(errorBody!!.asJsonObject, apiIndex)
        }
    }
}

I have tried different proguard rules but no result. Please help.

Upvotes: 10

Views: 4376

Answers (3)

user14279758
user14279758

Reputation: 141

I was facing the same issue, I solved by adding all these in proguard-rules.pro file:

-keepclassmembers enum * { *; }
-keep class com.google.code.gson.* { *; }
-keepattributes *Annotation*, Signature, Exception
-keepclassmembers,allowobfuscation class * {
  @com.google.gson.annotations.SerializedName <fields>;
}

Upvotes: 14

Sam
Sam

Reputation: 1029

In case this helps anyone, I had an existing project with a library module I had made. To prevent my app from crashing after being minified by ProGuard, I needed to add

-keepclassmembers enum * { *; }

to not only the library's build.gradle, but also to the main module's build.gradle.

Upvotes: 12

Shailesh Chandra
Shailesh Chandra

Reputation: 2340

below configuration always worked for me

-keepclassmembers enum  * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

Upvotes: 0

Related Questions