【问题标题】:Retrofit2 not working when enabling Proguard启用 Proguard 时 Retrofit2 不起作用
【发布时间】:2020-06-03 14:35:29
【问题描述】:

我有一个应用程序使用改造从 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上尝试了很多帖子,但对我没有任何帮助。有人可以通过提出解决方案或替代方案来帮助我吗?

谢谢

【问题讨论】:

  • 你用 Gson 吗?
  • 我使用moshi库
  • 我不敢相信这解决了我的问题!非常感谢兄弟。这真的很有帮助!
  • @IR42 嗨,如果使用 Gson 呢?我现在遇到这个问题.. com.android.tools.build:gradle:4.0.0 和 android studio 4.0。从日志中我可以看到请求但没有收到响应

标签: android kotlin retrofit2 proguard android-obfuscation


【解决方案1】:

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

public class YourJsonClass{
    @SerializedName("username") 
    String username;
}

【讨论】:

  • 是的,但在发布 apk 中仍然不能进行改造
【解决方案2】:
  1. 使用官方改造网站的 progaurd 规则 https://github.com/square/retrofit/blob/master/retrofit/src/main/resources/META-INF/proguard/retrofit2.pro

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-04
    • 2013-07-23
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多