问题
I am writing an application with Kotlin and Retrofit 2. As I use proguard, I follow the rules here:
https://github.com/krschultz/android-proguard-snippets/blob/master/libraries/proguard-square-retrofit2.pro
Besides I also need to proguard my models too, as stated in https://stackoverflow.com/a/41136007/3286489
It works fine if I have my models in a package, and I have -keep class com.elyeproj.wikisearchcount.model.** { *; }
package com.elyeproj.wikisearchcount.model
object Model {
data class Result(val query: Query)
data class Query(val searchinfo: SearchInfo)
data class SearchInfo(val totalhits: Int)
}
However, if I keep my Models in the base package as the code below, but I don't want to keep the entire package i.e. -keep class com.elyeproj.wikisearchcount.** { *; }
, since this defeat the purpose of proguard
package com.elyeproj.wikisearchcount
object Model {
data class Result(val query: Query)
data class Query(val searchinfo: SearchInfo)
data class SearchInfo(val totalhits: Int)
}
How could I keep my model classes?
I tried -keep class com.elyeproj.wikisearchcount.Model.** { *; }
, but it doesn't work.
回答1:
Why don't you use the annotation @SerializedName
and then you don't have to worry about the obfuscation?
You could use the following code:
object Model {
data class Result(@SerializedName("query") val query: Query)
data class Query(@SerializedName("searchInfo") val searchinfo: SearchInfo)
data class SearchInfo(@SerializedName("totalhits") val totalhits: Int)
}
回答2:
After exploring further, I found the answer
-keep class com.elyeproj.wikisearchcount.Model** { *; }
来源:https://stackoverflow.com/questions/44473673/how-to-skip-proguard-models-used-by-retrofit2-that-is-on-the-base-package