0

我有一个应用程序,它使用改造从 API 中获取徽标。

当我不混淆和缩小我的代码时,一切正常。但是当我启用它时,API 调用停止工作。我没有收到任何崩溃或错误消息,只是没有从 API 中获得任何值。

毕业典礼

buildTypes {
    debug{
        // Enables code shrinking, obfuscation, and optimization for only
        // your project's release build type.
        minifyEnabled true

        // Enables resource shrinking, which is performed by the
        // Android Gradle plugin.
        shrinkResources true

        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        signingConfig signingConfigs.debug
    }
    release {
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        signingConfig signingConfigs.release
    }
}

我在调试中使用它只是为了测试目的。如果我在发布中使用它,它具有相同的输出。

我检查了改造页面,他们建议使用以下文件

proguard-rules.pro

# Uncomment this to preserve the line number information for
# debugging stack traces.
-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
-renamesourcefileattribute SourceFile
-printusage usage.txt

-keep class com.th3pl4gu3.locky_offline.core.main.** {*;}
-keep class com.th3pl4gu3.locky_offline.repository.network.** {*;}
-keep class com.th3pl4gu3.locky_offline.repository.database.** {*;}

# Retrofit does reflection on generic parameters. InnerClasses is required to use Signature and
# EnclosingMethod is required to use InnerClasses.
-keepattributes Signature, InnerClasses, EnclosingMethod

# Retrofit does reflection on method and parameter annotations.
-keepattributes RuntimeVisibleAnnotations, RuntimeVisibleParameterAnnotations

# Retain service method parameters when optimizing.
-keepclassmembers,allowshrinking,allowobfuscation interface * {
    @retrofit2.http.* <methods>;
}

# Ignore annotation used for build tooling.
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement

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

# Guarded by a NoClassDefFoundError try/catch and only used when on the classpath.
-dontwarn kotlin.Unit

# Top-level functions that can only be used by Kotlin.
-dontwarn retrofit2.KotlinExtensions
-dontwarn retrofit2.KotlinExtensions$*

# With R8 full mode, it sees no subtypes of Retrofit interfaces since they are created with a Proxy
# and replaces all potential values with null. Explicitly keeping the interfaces prevents this.
-if interface * { @retrofit2.http.* <methods>; }
-keep,allowobfuscation interface <1>

API 调用仍然不起作用。我在stackoverflow上尝试了很多帖子,但对我没有任何帮助。有人可以通过提出解决方案或替代方案来帮助我吗?

谢谢

4

2 回答 2

1

您是否尝试将 @SerializedName 放入 Object 字段?

public class YourJsonClass{
    @SerializedName("username") 
    String username;
}
于 2020-06-03T15:00:22.337 回答
0
  1. 使用官方改造网站 https://github.com/square/retrofit/blob/master/retrofit/src/main/resources/META-INF/proguard/retrofit2.pro的 progaurd 规则

  2. 有时问题似乎来自改造,但事实并非如此。这是 GSON 和 ProGuard 的问题。要修复它,您只需在存储 Pojo 类的包的 proguard 规则中添加-keep 类配置 例如,我的 API 响应和请求类存储如下(包名称可能因您的应用程序而异):

-保留类 com.sample.myapp.model.request。{ ; -keep
类 com.sample.myapp.model.model.response。
* { ; } *

于 2021-02-22T14:43:26.660 回答