启用 Proguard 时,改装 2 不起作用



我有一个应用程序,它使用改造从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 调用仍然不起作用。我在堆栈溢出上尝试了许多帖子,但对我没有任何用处。有人可以通过提出解决方案或替代方案来帮助我吗?

谢谢

您是否尝试过将@SerializedName放入对象字段?

public class YourJsonClass{
@SerializedName("username") 
String username;
}
  1. 使用官方改造站点中的程序规则 https://github.com/square/retrofit/blob/master/retrofit/src/main/resources/META-INF/proguard/retrofit2.pro

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

-keep class com.sample.myapp.model.request.{; }
-keep class com.sample.myapp.model.model.response.
* {; }*

最新更新