【问题标题】:okhttp3 MediaType.parse Fatal Exception: java.lang.IncompatibleClassChangeErrorokhttp3 MediaType.parse 致命异常:java.lang.IncompatibleClassChangeError
【发布时间】:2020-09-23 05:33:37
【问题描述】:

在 Java Android Studio 项目中,我正在使用 Retrofit(最新 2.9 版本)上传二进制文件(通过 PUT Web 服务)。这段代码

MediaType mediaType = MediaType.parse("application/octet-stream");

使用发布配置构建时使应用程序崩溃,产生以下异常

The method 'f.a0 f.a0$a.b(java.lang.String)' was expected to be of type virtual but instead was found to be of type static (declaration of 'd.a.a.c.h.p0' appears in /data/app/my.app.packageid.dev-1L1taSMlTPHQ0cbe1kpruQ==/base.apk)
       at okhttp3.MediaType.parse(MediaType.java:6)

发布配置使用 proguard,但在启用 proguard 的情况下构建调试配置时(将调试部分放入 buildTypes 中,minifyEnabled 为 true,并且与发布部分具有相同的 proguardFiles 定义),不会发生崩溃。

这里是我的 build.gradle(不能降级到 Java 7)

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'io.fabric'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"
    defaultConfig {
        applicationId "my.app.packageid"
        minSdkVersion 24
        targetSdkVersion 29
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    signingConfigs {
        release {
            keyAlias "NotTheRealValue"
            keyPassword "NotTheRealValue"
            storeFile file("ValidPath")
            storePassword "NotTheRealValue"
        }
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
    }
    flavorDimensions "environment"
    productFlavors {
        dev {
            dimension "environment"
            applicationIdSuffix ".dev"
        }
        prod {
            dimension "environment"
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation 'com.squareup.retrofit2:retrofit:2.8.1'
    implementation 'com.squareup.retrofit2:converter-gson:2.8.1'

    implementation 'com.google.firebase:firebase-analytics:17.4.2'
    implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
}

还有我的 proguard-rules.pro

##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*

# Gson specific classes
-dontwarn sun.misc.**
#-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { <fields>; }

# Prevent proguard from stripping interface information from TypeAdapter, TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * implements com.google.gson.TypeAdapter
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer

# Prevent R8 from leaving Data object members always null
-keepclassmembers,allowobfuscation class * {
  @com.google.gson.annotations.SerializedName <fields>;
}

##---------------End: proguard configuration for Gson  ----------

##---------------Start: proguard configuration for Crashlytics  ----------

-keepattributes SourceFile,LineNumberTable
-keep public class * extends java.lang.Exception
-keep class com.crashlytics.** { *; }
-dontwarn com.crashlytics.**

##---------------End: proguard configuration for Crashlytics  ----------

##---------------Start: Retrofit---------------

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

##---------------End: Retrofit---------------

##---------------Start: OkHttp---------------

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

# A resource is loaded with a relative path so the package of this class must be preserved.
-keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase

# Animal Sniffer compileOnly dependency to ensure APIs are compatible with older versions of Java.
-dontwarn org.codehaus.mojo.animal_sniffer.*

# OkHttp platform used only on JVM and when Conscrypt dependency is available.
-dontwarn okhttp3.internal.platform.ConscryptPlatform

##---------------End: OkHttp---------------

##---------------Start: Okio---------------

# Animal Sniffer compileOnly dependency to ensure APIs are compatible with older versions of Java.
-dontwarn org.codehaus.mojo.animal_sniffer.*

##---------------End: Okio---------------

这是与 Kotlin-Java 转换有关的问题(因为 MediaType 是用 Kotlin 编写的)还是我做错了什么?

【问题讨论】:

  • 经过一些试验和错误,它似乎是一个 gradle 插件相关的错误,因为从 'com.android.tools.build:gradle:4.0.0' 降级到 'com.android.tools。 build:gradle:3.6.3' 解决了这个问题。我在 Retrofit 的 Github 上开了一个 issue (github.com/square/retrofit/issues/3412)

标签: java android kotlin okhttp media-type


【解决方案1】:

对我来说,只需使用最新的 r8 版本即可解决此问题。在您的顶级构建文件中:

repositories {
    maven {
        url 'https://storage.googleapis.com/r8-releases/raw'
    }
}

dependencies {
    classpath 'com.android.tools:r8:e2a8efc7cba321bdb419c5260847e18e2732f0c8'          // Place BEFORE AGP classpath declaration!
    classpath 'com.android.tools.build:gradle:X.Y.Z' // AGP
}

或者将-keep class okhttp3.MediaType$Companion { *; } 添加到您的proguard 文件中。

这个issue has already been reported to the R8 team 和一个修复应该在 AGP 7.0.0-alpha11/12 捆绑的 R8 版本中登陆。

【讨论】:

    猜你喜欢
    • 2017-06-28
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-16
    • 2017-09-13
    • 2012-07-23
    相关资源
    最近更新 更多