Cannot parse json objects with GSON when setting minifyEnabled is true
Asked Answered
R

3

5

When I use

buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

everything works perfectly.

But if change to

buildTypes {
        release {
            minifyEnabled true
            useProguard true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

I get a parsing error.

My classes for parse:

data class SearchItem(
    @SerializedName("position") val position: Long,
    @SerializedName("user") val user: String?
) {
    fun getId(): Long {
        return 0L
    }
}

data class SearchResponse(
    @SerializedName("list") val list: List<SearchItem>,
    @SerializedName("has_more") val has_more: Boolean,
    @SerializedName("rank_token") val rank_token: String,
    @SerializedName("status") val status: String
)

Here I use gson:

val searchResponse = Gson().fromJson(jsonObject.toString(), SearchResponse::class.java)

In proguard-rules.pro I have one non-comment line -keepattributes *Annotation*

I would be grateful for any help!

EDIT Error log:

E/AndroidRuntime: FATAL EXCEPTION: k.c0 Dispatcher
    Process: PID: 31392
    java.lang.NullPointerException: throw with null exception
        at h.a.a.j.a.a.a(:2)
        at k.l0.g.e$a.run(:6)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:764)

This happens only with .apk installation. If I run the app from Android Studio all is fine.

Rhodian answered 21/7, 2020 at 13:8 Comment(3)
what is the error?Registry
Please share the crash logMontcalm
@Registry java.lang.NullPointerException: throw with null exception I don't have any error tracks because this happens after .apk installation. If I run the app from Studio all is fineRhodian
A
7

I believe that you need to edit your proguard-rules.pro file to include those data classes with this line. You can read more about it here.

-keepclassmembers,allowobfuscation class * {
    @com.google.gson.annotations.SerializedName <fields>;
}
Anthill answered 21/7, 2020 at 13:24 Comment(0)
D
0

for any one with same problem when not. using @SerializedName attribute use this instead

-keepclassmembers class MyDataClass {
 !transient <fields>;
} 

insteady of accepted answer above check link https://r8.googlesource.com/r8/+/refs/heads/master/compatibility-faq.md

Deutoplasm answered 6/8, 2023 at 12:8 Comment(0)
M
0

Because I'm using GSON, I had to add the following additional lines to the proguard-rules.pro file. See https://r8.googlesource.com/r8/+/refs/heads/main/compatibility-faq.md#troubleshooting-gson-gson

# GSON
-keepattributes Signature
-keep class com.google.gson.reflect.TypeToken { *; }
-keep class * extends com.google.gson.reflect.TypeToken

# Coroutines
-keepattributes Signature
-keep class kotlin.coroutines.Continuation
Madore answered 15/2 at 18:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.