You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级Gradle从7.4.2到8.0.0后发布APK遇ClassCastException错误求助

升级AGP 8.0.0后签名APK运行出现java.lang.ClassCastException

近期将Android Studio升级至Flamingo版本,同时把Gradle从7.4.2升级到8.0.0。在Gradle 7.4.2版本下项目运行一切正常,但使用Gradle 8.0.0生成签名APK时,运行时出现java.lang.ClassCastException错误。尝试过添加Retrofit、Okio、OkHttp等库的Proguard规则,但问题仍未解决。降级回7.4.2版本后恢复正常,求排查AGP 8.0.0相关问题。

app模块build.gradle

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
}

项目根目录build.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
}

排查方向与解决方案

1. 补全ProGuard/R8规则

AGP 8.0.0默认启用R8 full mode,代码收缩逻辑比旧版本更严格,仅添加Retrofit相关规则不够,需补充以下库的规则:

  • Hilt/Dagger:
    -keep class dagger.hilt.** { *; }
    -keep class javax.inject.** { *; }
    -keepnames class com.yourpackage.**_HiltModules { *; }
    
  • Compose:
    -keepnames class androidx.compose.ui.tooling.preview.Preview
    -keepclassmembers class androidx.compose.ui.tooling.preview.Preview {
        <init>(...);
    }
    -keepclassmembers class * {
        @androidx.compose.ui.tooling.preview.Preview <methods>;
    }
    
  • Room:
    -keep class com.yourpackage.database.** { *; }
    -keepclassmembers class com.yourpackage.database.** {
        @androidx.room.* <methods>;
    }
    

2. 统一依赖版本

AGP 8.0.0对依赖版本兼容性要求更高:

  • Hilt版本不一致:根build.gradle中Hilt插件版本是2.44,app模块是2.45,统一改为2.45:
    // 根build.gradle
    plugins {
        id 'com.google.dagger.hilt.android' version '2.45' apply false
    }
    
  • Compose版本匹配:当前使用的Compose BOM 2022.10.00过于陈旧,建议更新至2023.08.00+:
    implementation platform('androidx.compose:compose-bom:2023.08.00')
    
  • Kotlin与Compose Compiler版本匹配:Kotlin 1.8.10对应的Compose Compiler应为1.4.7,替换当前1.4.2版本:
    composeOptions {
        kotlinCompilerExtensionVersion '1.4.7'
    }
    

3. 验证MultiDex配置

AGP 8.0.0对MultiDex的处理逻辑有调整,虽然minSdk 24+默认支持ART multiDex,但仍需确认:

  • 确保multiDexEnabled true在defaultConfig中已配置(当前已设置)
  • 若有自定义Application类,需继承MultiDexApplication或在attachBaseContext中调用MultiDex.install(this)

4. 定位具体异常类

通过Crashlytics或logcat获取完整异常栈,明确是哪两个类之间的转换失败,针对性添加混淆规则会更高效。

5. 临时禁用R8 full mode测试

在release buildType中添加以下配置,禁用R8 full mode,验证是否是R8导致的问题:

android {
    buildTypes {
        release {
            // 新增该行
            android.enableR8.fullMode = false
        }
    }
}

内容的提问来源于stack exchange,提问作者Chirag Thummar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 16:04:55