使用 moshi-kotlin-codegen 时启用 R8

问题描述 投票:0回答:1

我正在尝试为我的项目启用 R8 :https://github.com/alirezaeiii/TMDb-Compose-Playground

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

我正在使用 moshi 来映射我的 json 响应,并且我添加了 mishi-kotlin-codegen。这是我的 Response 类的示例:

@JsonClass(generateAdapter = true)
data class NetworkMovie(
    @Json(name = ID)
    override val id: Int,
    @Json(name = OVERVIEW)
    override val overview: String,
    @Json(name = RELEASE_DATE)
    override val releaseDate: String?,
    @Json(name = POSTER_PATH)
    override val posterPath: String?,
    @Json(name = BACKDROP_PATH)
    override val backdropPath: String?,
    @Json(name = TITLE)
    override val name: String,
    @Json(name = VOTE_AVERAGE)
    override val voteAverage: Double,
    @Json(name = VOTE_COUNT)
    override val voteCount: Int
) : NetworkTMDbItem

如你所见,我正在使用

@JsonClass(generateAdapter = true)
并且我从 https://github.com/square/moshi/blob/master/moshi/src/main/resources/META-INF/proguard/moshi.pro 获得了 proguard 配置

# JSR 305 annotations are for embedding nullability information.
-dontwarn javax.annotation.**

-keepclasseswithmembers class * {
    @com.squareup.moshi.* <methods>;
}

-keep @com.squareup.moshi.JsonQualifier @interface *

# Enum field names are used by the integrated EnumJsonAdapter.
# values() is synthesized by the Kotlin compiler and is used by EnumJsonAdapter indirectly
# Annotate enums with @JsonClass(generateAdapter = false) to use them with Moshi.
-keepclassmembers @com.squareup.moshi.JsonClass class * extends java.lang.Enum {
    <fields>;
    **[] values();
}

# Keep helper method to avoid R8 optimisation that would keep all Kotlin Metadata when unwanted
-keepclassmembers class com.squareup.moshi.internal.Util {
    private static java.lang.String getKotlinMetadataClassName();
}

# Keep ToJson/FromJson-annotated methods
-keepclassmembers class * {
  @com.squareup.moshi.FromJson <methods>;
  @com.squareup.moshi.ToJson <methods>;
}

当我构建发布变体时,它成功构建,但是当我在设备上运行 apk 文件时,它在网络调用中失败。

我有什么遗漏的吗?

android proguard moshi android-r8
1个回答
0
投票

R8 完整模式从非保留项中删除通用签名,因此您需要在 proguard 文件中添加这些规则:

-keep,allowobfuscation,allowshrinking interface retrofit2.Call
-keep,allowobfuscation,allowshrinking class retrofit2.Response
-keep,allowobfuscation,allowshrinking class kotlin.coroutines.Continuation

关于您的代码,您在项目中没有使用

Call
Response
(根据我在存储库中看到的内容),因此您可以忽略前两个
-keep
并仅使用第三个,因为使用协程。

© www.soinside.com 2019 - 2024. All rights reserved.