改造不适用于应用程序的发布版本

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

我在我的应用程序中使用 Retrofit。一切工作正常,但当我创建应用程序的发布版本时,某些调用无法正常工作。

问题可能是什么?我已经在我的 gradle 文件中禁用了

minifyEnabled

编辑: 发现真正的问题: 我通过特定的 API 调用获取用户数据。我将其映射到以下课程:

String ID;
String user_login;
String user_nicename;
String user_email;
String display_name;

由于某种原因,除了 ID 之外的所有字段都已填写。当我不使用发布但调试时,ID 字段会被填充。

android android-gradle-plugin retrofit retrofit2
5个回答
9
投票

试试这个:

-dontnote okhttp3.**, okio.**, retrofit2.**
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }

如果上面的代码不起作用,则将

@Keep
注释添加到您的模型类中,如下所示。

import androidx.annotation.Keep;

@Keep
public class Blog {

    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

7
投票

对我来说,我试图使用改造来获取数据。我的模型如下:

data class ChecksumResponseEntity(
    @SerializedName("code")
    val code: Double,
    @SerializedName("message")
    val message: String,
    @SerializedName("checksum")
    val checksum: String
)

问题是所有值都被初始化为 null,而不是响应中的值。

在 proguard 文件中添加了以下内容,但它没有帮助我:

-dontnote okhttp3.**, okio.**, retrofit2.**
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }

我尝试将 @Keep 注释添加到我的模型文件中,如下所示:

@Keep
data class ChecksumResponseEntity(
    @SerializedName("code")
    val code: Double,
    @SerializedName("message")
    val message: String,
    @SerializedName("checksum")
    val checksum: String
)

不知道为什么会发生,但添加@Keep为我修复它


3
投票

确保提供了具体的改造规则。 如果您已启用

minifyEnabled=true

在您的

proguard-rules.pro
文件中添加以下改造规则

-dontnote okhttp3.**, okio.**, retrofit2.**
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }

1
投票

保持包装原样,DTO 所在位置:

-keep class com.test.apppackagename.retrofit.dto** {
    *;
}

0
投票

三种可能的解决方案

解决方案 1:使用 Proguard 规则保留模型类

# Keep the model classes and their fields
-keep class com.example.myapp.model.** { *; }
-keepclassmembers class com.example.myapp.model.** { *; }

在本例中,

com.example.myapp
是包名称。

解决方案2:使用@Keep注解

使用模型类的@Keep注释。像这样:

package com.example.myapp.model;

import androidx.annotation.Keep;

@Keep
public class MyClass {

    String ID;
    String user_login;
    String user_nicename;
    String user_email;
    String display_name;
    
    // Rest of the code...
}

解决方案3:

在应用程序级别

build.gradle
使用

minifyEnabled false

要测试您的应用程序,请在应用程序级别的 build.gradle 中使用以下代码行进行发布和调试配置:

buildTypes {
    /*release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }*/
    debug {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.