How to skip proguard models used by retrofit2 that

2019-06-05 08:51发布

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.

2条回答
混吃等死
2楼-- · 2019-06-05 09:12

After exploring further, I found the answer

-keep class com.elyeproj.wikisearchcount.Model** { *; }
查看更多
Ridiculous、
3楼-- · 2019-06-05 09:15

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)
}
查看更多
登录 后发表回答