:[已解决]:将 gradle 升级到 4.0.0 后,改造不再收到响应

Posted

技术标签:

【中文标题】:[已解决]:将 gradle 升级到 4.0.0 后,改造不再收到响应【英文标题】::[SOLVED]: After upgrading gradle to 4.0.0, retrofit do not receive response anymore 【发布时间】:2020-09-24 08:41:12 【问题描述】:

gradle 3.6.3 升级到 4.0.0 后,retrofit 只能发送请求,不能接收响应。

从日志中我只能看到:

I/okhttp.OkHttpClient: --> GET https://stg.xxxxx.com/api/users/profile
I/okhttp.OkHttpClient: --> END GET

// this is missing
I/okhttp.OkHttpClient: <-- GET https://stg.xxxxx.com/api/users/profile

一旦我删除了minifyEnabled, shrinkResources and proguardFiles,改造就可以照常调用api了。

android Studio 版本:4.0 摇篮版本:6.1.1 插件版本:4.0.0

build.gradle:

buildTypes 
        debug 
            signingConfig signingConfigs.debug
        
        release 
            signingConfig signingConfigs.release
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        
    

用于改造的proguard设置:

# from https://github.com/square/retrofit/blob/master/retrofit/src/main/resources/META-INF/proguard/retrofit2.pro

# 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>

-dontwarn retrofit2.**
-dontwarn org.codehaus.mojo.**
-keep class retrofit2.**  *; 
-keepattributes Signature
-keepattributes Exceptions
-keepattributes *Annotation*

-keepattributes RuntimeVisibleAnnotations
-keepattributes RuntimeInvisibleAnnotations
-keepattributes RuntimeVisibleParameterAnnotations
-keepattributes RuntimeInvisibleParameterAnnotations

-keepattributes EnclosingMethod

-keepclasseswithmembers class * 
    @retrofit2.* <methods>;


-keepclasseswithmembers interface * 
    @retrofit2.* <methods>;


-keepclasseswithmembers class * 
    @retrofit2.http.* <methods>;


-keepclasseswithmembers interface * 
    @retrofit2.http.* <methods>;


# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on RoboVM on ios. Will not be used at runtime.
-dontnote retrofit2.Platform$IOS$MainThreadExecutor
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions

有人有解决办法吗?

更新:通过添加classpath 'com.android.tools:r8:1.6.84'解决

【问题讨论】:

【参考方案1】:

使用@Keep关键字保留改造的所有模型类

例子

在 Kotlin 中:

@Keep
data class MarkerData(var planeId : String , var planeData : NewMainFlights)

在 Java 中:

@Keep
class MarkerDataClass 

    String planeId;
    String planeData;

    public MarkerDataClass(String planeId, String planeData) 
        this.planeId = planeId;
        this.planeData = planeData;
    


【讨论】:

我的所有模型类中都有@Keep

以上是关于:[已解决]:将 gradle 升级到 4.0.0 后,改造不再收到响应的主要内容,如果未能解决你的问题,请参考以下文章

升级 android build:gradle:4.0.0 后在 proguard 中显示错误

Flutter AppCenter Gradle Plugin 4.0.0+ 构建问题

Rails 4.0.0到4.1.0升级会导致错误的参数错误

将 android gradle 插件从 4.2.1 升级到 7.0.1 后出现错误

为啥 eslint 考虑 JSX 或一些反应 @types undefined,因为将 typescript-eslint/parser 升级到版本 4.0.0

生成APK(签名)升级gradle版本时出现问题