gpt4 book ai didi

java - okhttp3 MediaType.parse 致命异常 : java. lang.IncompatibleClassChangeError

转载 作者:行者123 更新时间:2023-12-04 13:35:36 25 4
gpt4 key购买 nike

在 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 编写的)还是我做错了什么?

最佳答案

对我来说,只需使用最新的 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 中 bundle 的 R8 版本中进行修复。

关于java - okhttp3 MediaType.parse 致命异常 : java. lang.IncompatibleClassChangeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62191139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com