升级Gradle和Kotlin后执行assembleRelease遇Firebase相关D8警告求助
近期升级了项目的Gradle和Kotlin版本,但执行assembleRelease任务时,仅针对com.google.firebase.analytics组件出现以下警告:
WARNING: D8: An error occurred when parsing kotlin metadata. This normally happens when using a newer version of kotlin than the kotlin version released when this version of R8 was created. To find compatible kotlin versions, please see: https://developer.android.com/studio/build/kotlin-d8-r8-versions
WARNING: D8: Unexpected error during rewriting of Kotlin metadata for class 'com.google.firebase.analytics.ktx.ktxtesting.TestingKt':
com.android.tools.r8.internal.A10: Should never be called
当前项目配置
项目级build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { dependencies { classpath 'com.google.android.gms:oss-licenses-plugin:0.10.6' } } plugins { id 'com.android.application' version '8.1.4' apply false id 'com.android.library' version '8.1.4' apply false id 'org.jetbrains.kotlin.android' version '1.9.10' apply false id 'com.google.gms.google-services' version '4.4.2' apply false id 'com.google.firebase.crashlytics' version '3.0.2' apply false }
gradle-wrapper.properties
distributionBase=GRADLE_USER_HOME distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip distributionPath=wrapper/dists zipStorePath=wrapper/dists zipStoreBase=GRADLE_USER_HOME
已查阅官方兼容文档,显示当前版本组合不存在兼容性问题,寻求解决该警告的方案。
可行解决办法
1. 升级Android Gradle Plugin(AGP)版本
当前AGP版本8.1.4对Kotlin 1.9.10的适配存在细节问题,升级到8.2.0及以上版本可修复R8与Kotlin元数据解析的兼容性问题。修改项目级build.gradle中的AGP版本:
plugins { id 'com.android.application' version '8.2.0' apply false id 'com.android.library' version '8.2.0' apply false // 其他插件版本保持不变 }
2. 排除Firebase Analytics的测试模块
警告指向ktxtesting.TestingKt,属于测试相关的Kotlin扩展模块。若项目未使用Firebase Analytics的测试功能,可在app模块的build.gradle中排除该模块:
dependencies { implementation platform('com.google.firebase:firebase-bom:32.7.0') // 使用最新稳定BOM版本 implementation 'com.google.firebase:firebase-analytics-ktx' // 排除测试模块 implementation('com.google.firebase:firebase-analytics-ktx') { exclude group: 'com.google.firebase', module: 'firebase-analytics-ktx-testing' } }
同时建议升级Firebase BOM到最新版本,确保所有Firebase组件版本统一且适配当前AGP与Kotlin版本。
3. 升级Gradle版本
当前Gradle 8.0可搭配AGP 8.1.4,但升级到Gradle 8.2能更好适配AGP 8.2.x与Kotlin 1.9.10。修改gradle-wrapper.properties中的distributionUrl:
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
4. 临时禁用R8的Kotlin元数据重写(不推荐长期使用)
若上述方法无效,可在app模块的build.gradle中添加配置临时禁用R8对Kotlin元数据的处理,但这会影响代码优化效果,仅作为应急方案:
android { buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' // 添加以下配置 kotlinOptions { freeCompilerArgs += ['-Xno-kotlin-metadata'] } } } }
内容的提问来源于stack exchange,提问作者SVG

