:[已解决]。在将gradle升级到4.0.0后,retrofit不再收到答复。

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

从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.0Gradle版本:6.1.1Plugin版本。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'
        }
    }

retrofit的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'

android kotlin gradle retrofit proguard
1个回答
0
投票

使用@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;
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.