Reputation: 743
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.
Upvotes: 5
Views: 3379
Reputation: 3321
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
Upvotes: 0
Reputation: 625
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
Upvotes: 1
Reputation: 396
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>;
}
Upvotes: 7