Search code examples
androidkotlinretrofit2moshiandroid-r8

Moshi/retrofit error with one single Int field after enabling R8 minify


Having trouble with parsing one single field from JSON response after enabling minify, with minify disabled all works correctly:

retrofit API call:

@FormUrlEncoded
@POST("/api/test")
fun test(@Field <some String fields>
): Observable<Response<TestListing>>

wrapped in repo

   override fun test(<some String fields>): Observable<Response<TestListing>> {
        return api.test(<some String fields>)
            .subscribeOn(schedulers.io())
    }

model:

data class TestListing (
    @Json(name = "success") val success:Int,
    @Json(name = "user") val user: TestUser?
    )

TestUser class

data class TestUser(
    @Json(name = "id") val id: Int,
    @Json(name = "email")  val email: String,
    @Json(name = "name")  val name: String,
    @Json(name = "key") val remix_userkey: String,
    @Json(name = "downloads_limit") val downloads_limit: Int?,
    <some other fields>
    )

and finally calling it in a viewModel

fun test(<some String fields>){
        compositeDisposable.add(testRepo.test(<some String variables>)
            .subscribeOn(schedulers.io())
            .observeOn(schedulers.main())
            .subscribe ({ testList ->
                testListDebug.postValue(testList)
                if (testList.isSuccessful) user.postValue(userList.body()?.user)
                else {<some error posting>}
            })
            { throwable -> <some actions>})
    }

So without minifyEnabled it parses this JSON

{"success":1,"user":{"id":"123456","email":"[email protected]","name":"Test","remix_userkey":"abcd123abcd","downloads_limit":15}}

correctly, after I enable minify - id field is always 0.

Same JSON, but somehow it wraps in retrofit Response already with id=0 in the body(all other fields are parsed correctly)

example of testListDebug value from debugger after API call

Tried adding all library rules in proguard-rules.pro file, but with no effect; also tried adding @Keep annotation to TestUser class and renaming id field

Where I can dig from here? Is it something regarding Moshi or Retrofit/Okhttp?


Solution

  • Figured it out - needed to keep a custom moshi annotation class, which was used for parsing some field(which sometimes Int and sometimes Boolean) in other API calls and which was not used here. After adding keep annotation to it id is parsed fine

    Very strange behavior since this annotation was not used here