将Gradle从7.4.2升级到Gradle 8.0.0会在发布应用程序时出现错误-java.lang.ClassCastException。

19

我最近将Android Studio升级到Flamingo版本,并将Gradle从7.4.2升级到8.0.0。在7.4.2版本中,一切都正常。

当我使用Gradle 8.0.0生成签名APK时,它会给我一个java.lang.ClassCastException的运行时错误。

我尝试了许多解决方案,例如为Retrofit、Okio、OkHttp等添加Proguard规则,但仍然会出现这样的错误。

注意:当我将Gradle从8.0.0降级到7.4.2时,它可以正常工作。

所以,有人能帮我找出AGP 8.0.0的问题吗?

build.gradle(app)

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-kapt'
    id 'kotlin-parcelize'
    id 'com.google.dagger.hilt.android'
    id 'com.google.gms.google-services'
    id 'com.google.firebase.crashlytics'
}


android {
    
    compileSdk 33

    defaultConfig {
        
        minSdk 24
        targetSdk 33
        versionCode 22
        versionName "1.0.16"
        multiDexEnabled true

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        /*vectorDrawables {
            useSupportLibrary true
        }*/

        def localPropertiesFile = rootProject.file("local.properties")
        def localProperties = new Properties()
        localProperties.load(new FileInputStream(localPropertiesFile))
        buildConfigField "String", "API_KEY", localProperties['API_KEY']



    }

    
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            signingConfig signingConfigs.release
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = '17'
    }
    buildFeatures {
        compose true
        viewBinding = true
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.4.2'
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
    bundle {
        language {
            enableSplit = false
        }
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.10.0'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.multidex:multidex:2.0.1'

    
    implementation 'com.google.accompanist:accompanist-permissions:0.24.11-rc'
    implementation 'com.google.accompanist:accompanist-webview:0.24.11-rc'
    implementation 'com.google.accompanist:accompanist-pager:0.24.13-rc'
    implementation "com.google.accompanist:accompanist-pager-indicators:0.24.13-rc"
    implementation "com.google.accompanist:accompanist-drawablepainter:0.25.1"
    implementation "com.google.accompanist:accompanist-flowlayout:0.31.0-alpha"


   
    implementation 'androidx.activity:activity-compose:1.7.1'
    implementation platform('androidx.compose:compose-bom:2022.10.00')
    implementation 'androidx.compose.ui:ui'
    implementation 'androidx.compose.ui:ui-graphics'
    implementation 'androidx.compose.ui:ui-tooling-preview'
    implementation 'androidx.compose.material:material'
//    implementation 'androidx.compose.material3:material3'
    implementation "androidx.navigation:navigation-compose:2.5.3"
    implementation 'com.google.firebase:protolite-well-known-types:18.0.0'
    implementation "androidx.compose.ui:ui-viewbinding"
    implementation project(path: ':pdfviewer')

  
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4"

    
    implementation "com.google.dagger:hilt-android:2.45"
    debugImplementation "androidx.compose.ui:ui-test-manifest"
    kapt "com.google.dagger:hilt-compiler:2.45"
    kapt "androidx.hilt:hilt-compiler:1.0.0"
    implementation 'androidx.hilt:hilt-navigation-compose:1.0.0'

  
    implementation "androidx.activity:activity-ktx:1.7.1"

  
    implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.6.1"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.6.1"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1"
    implementation "androidx.lifecycle:lifecycle-process:2.6.1"
    kapt "androidx.lifecycle:lifecycle-compiler:2.6.1"

    /* *****************************************************
       **** Retrofit2
       ****************************************************** */
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation "com.squareup.okhttp3:okhttp:4.9.0"
    implementation "com.squareup.okhttp3:logging-interceptor:4.9.0"
    implementation 'com.squareup.retrofit2:converter-moshi:2.9.0'

   
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'

   
    implementation "androidx.room:room-runtime:2.5.1"
    kapt "androidx.room:room-compiler:2.5.1"

    
    implementation "androidx.room:room-ktx:2.5.1"

   
    implementation 'androidx.core:core-splashscreen:1.0.1'

    
    def billing_version = "5.2.0"
    implementation "com.android.billingclient:billing:$billing_version"
    implementation "com.android.billingclient:billing-ktx:$billing_version"
    implementation 'com.google.firebase:firebase-crashlytics-buildtools:2.9.5'

   
    implementation platform('com.google.firebase:firebase-bom:31.1.0')
    implementation 'com.google.firebase:firebase-config-ktx'
    implementation 'com.google.firebase:firebase-analytics-ktx'
    implementation 'com.google.firebase:firebase-crashlytics-ktx'
    implementation 'com.google.firebase:firebase-messaging-ktx'
    implementation 'com.google.android.gms:play-services-ads:22.0.0'

   
    implementation 'com.airbnb.android:lottie-compose:4.0.0'


}

kapt {
    correctErrorTypes true
}

项目 Gradle 文件

buildscript {
    ext {
        compose_ui_version = '1.5.0-alpha02'
        kotlin_version = '1.8.10'
    }
        dependencies {
            // Add this line
            classpath 'com.google.gms:google-services:4.3.15'
            classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.5'
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
    repositories {
        mavenCentral()
    }
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '8.0.0' apply false
    id 'com.android.library' version '8.0.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.8.10' apply false
    id 'com.google.dagger.hilt.android' version '2.44' apply false
}

2
您需要提供一个 [mre] 和完整的异常堆栈跟踪。Gradle 8 中有许多重大变更,因此可能您正在使用与 Gradle 8 不兼容的插件或其他内容。 - Mark Rotteveel
2
完整的异常堆栈跟踪信息是什么? - Mark Rotteveel
它是以密码形式存在的,因为我启用了R8。虽然我会分享它。 - Chirag Thummar
将其添加到问题本身中,而不是在评论中。 - Mark Rotteveel
你应该发布一个包含你的解决方案的“答案”。不要在评论中发布。 - Mark Rotteveel
我已经回滚了你的编辑。请将答案发布为一个答案,而不是问题的一部分。 - Mark Rotteveel
4个回答

14
我认为禁用R8 fullmode是解决方案,直到所有库都更新到Gradle 8并考虑R8 fullmode规则。
在gradle.properties中添加以下行:
android.enableR8.fullMode=false

是的,你也可以做到这一点。 - Chirag Thummar
1
仍然出现错误:执行任务':app:minifyReleaseWithR8'失败。在执行com.android.build.gradle.internal.tasks.R8Task$R8Runnable时发生了故障;原因是错误:R8:缺少类com.google.errorprone.annotations.DoNotMock(引用自:autodispose2.ScopeProvider和其他1个上下文) - Dr.jacky
解决方案:使用R8 8.1.56和gradle工具8.2.0-alpha15,并在app/proguard-rules.pro中添加-dontwarn com.google.errorprone.annotations.** - Dr.jacky

3

很可能您正在使用与Gradle 8.0不兼容的插件。如果没有必要升级到Gradle 8.0,我建议您降级回7.4.2,直到这些插件变得兼容为止。另一种方法是检查那些不兼容的插件,并在它们可以添加时尝试替换它们。


1
通过添加Proguard规则解决 - Chirag Thummar
@chiragthummar,你能分享一下完整的规则文件吗?我遇到了同样的问题。 - Michael Winkler
@MichaelWinkler,请在我的原始问题中查找,我已经在那里添加了解决方案。如果您使用了parcelize插件,则会出现此问题。 - Chirag Thummar
1
https://github.com/square/retrofit/issues/3880 - avianey

1
我遇到了这个问题,是因为使用了 Parcelize 插件。我正在使用 Parcelize 插件在活动之间传递自定义对象 ArrayList。
经过多次尝试,最终找到了解决方案,即更改 proguard-rules.pro 文件。
以下是我的 proguard-rules.pro 文件内容。
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}

# 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

-dontwarn rx.**

-dontwarn okio.**

-dontwarn com.squareup.okhttp.**
-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }

-dontwarn retrofit.**
-keep class retrofit.** { *; }
-keepclasseswithmembers class * {
    @retrofit.http.* <methods>;
}

-keepattributes Signature
-keepattributes *Annotation*


-dontwarn javax.annotation.**

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

# 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 and other security providers are available.
-dontwarn okhttp3.internal.platform.**
-dontwarn org.conscrypt.**
-dontwarn org.bouncycastle.**
-dontwarn org.openjsse.**

-repackageclasses
-ignorewarnings

# 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

# Keep annotation default values (e.g., retrofit2.http.Field.encoded).
-keepattributes AnnotationDefault

# Retain service method parameters when optimizing.
-keepclassmembers,allowshrinking,allowobfuscation interface * {
    @retrofit2.http.* <methods>;
}

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

# Keep inherited services.
-if interface * { @retrofit2.http.* <methods>; }
-keep,allowobfuscation interface * extends <1>

# Keep generic signature of Call, Response (R8 full mode strips signatures from non-kept items).
-keep,allowobfuscation,allowshrinking interface retrofit2.Call
-keep,allowobfuscation,allowshrinking class retrofit2.Response

# With R8 full mode generic signatures are stripped for classes that are not
# kept. Suspend functions are wrapped in continuations where the type argument
# is used.
-keep,allowobfuscation,allowshrinking class kotlin.coroutines.Continuation


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

#-keep public class com.itextpdf.**
-keep class com.itextpdf.** { *; }
-dontwarn com.itextpdf.*

-renamesourcefileattribute SourceFile

-dontwarn retrofit2.**
-keep class retrofit2.** {*;}

##---------------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 * extends 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>;
}


-keep class com.google.gson.reflect.TypeToken
-keep class * extends com.google.gson.reflect.TypeToken
-keep public class * implements java.lang.reflect.Type


# Retain generic signatures of TypeToken and its subclasses with R8 version 3.0 and higher.
-keep,allowobfuscation,allowshrinking class com.google.gson.reflect.TypeToken
-keep,allowobfuscation,allowshrinking class * extends com.google.gson.reflect.TypeToken

##---------------End: proguard configuration for Gson  ----------
-dontwarn org.slf4j.**

-keepdirectories src/main/res/font/*



-dontusemixedcaseclassnames
-verbose
-keepattributes *Annotation*

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
    native <methods>;
}

# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
-keepclassmembers public class * extends android.view.View {
   void set*(***);
   *** get*();
}

# We want to keep methods in Activity that could be used in the XML attribute onClick
-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

-keepclassmembers class **.R$* {
    public static <fields>;
}

# Firebase
-keep class com.google.android.gms.** { *; }
-keep class com.google.firebase.** { *; }

# in order to provide the most meaningful crash reports, add the following line:
-keepattributes SourceFile,LineNumberTable
# If you are using custom exceptions, add this line so that custom exception types are skipped during obfuscation:
-keep public class * extends java.lang.Exception

-keep class com.crashlytics.** { *; }
-dontwarn com.crashlytics.**

# Jackson
-keep @com.fasterxml.jackson.annotation.JsonIgnoreProperties class * { *; }
-keep class com.fasterxml.** { *; }
-keep class org.codehaus.** { *; }
-keepnames class com.fasterxml.jackson.** { *; }
-keepclassmembers public final enum com.fasterxml.jackson.annotation.JsonAutoDetect$Visibility {
    public static final com.fasterxml.jackson.annotation.JsonAutoDetect$Visibility *;
}

# General
-keepattributes SourceFile,LineNumberTable,*Annotation*,EnclosingMethod,Signature,Exceptions,InnerClasses

这段代码是用于parcelize插件的

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
    -keepclassmembers enum * {
        public static **[] values();
        public static ** valueOf(java.lang.String);
    }
    
    -keep class * implements android.os.Parcelable {
      public static final android.os.Parcelable$Creator *;
    }
    
    -keepclassmembers class **.R$* {
        public static <fields>;
    }

1
在编程中,添加“dontshrink dontobfuscate”不是一个好主意。 - HiddenCoder7
所有的Proguard都被以下选项忽略了: -dontshrink -dontobfuscate - alien123X
我认为 "***.R$*" 不是用于 parcelize 插件的,它是用于 Android R 文件的。我说得对吗? - David

1

在升级到AGP 8.0.1后,已发布的解决方案对我无效。 我仍然会因Retrofit创建并由Dagger模块提供的接口而得到ClassCastException。

我能够通过使用@Keep注释来修复java.lang.ClassCastException。 这将告诉编译器在生成的字节码中保留接口,即使它在代码中没有其他用途。

例如:

@Keep
interface MyApi {
  ...
}

有时候 @Keep 会出现问题,所以我添加了自己的脚本来跳过 Proguards 中的必需类。 - Chirag Thummar
通过proguard-rules.pro文件,保留接口也适用于我。-keep interface company.package.** { *; } - mochadwi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接