Android新建项目报错:Kotlin库由高版本编译器编译需更新Kotlin插件
问题描述
新建Android项目后触发如下报错:
Some Kotlin libraries attached to this project were read by a newer Kotlin compiler and can't be read. Please update Kotlin Plugin
相关参考截图:
- 报错界面:

- IDE Kotlin插件状态:

- 已尝试修改kotlin_version配置操作:

当前项目配置
根目录build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext.kotlin_version = "1.4.32" repositories { google() jcenter() } dependencies { classpath "com.android.tools.build:gradle:4.1.1" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }
app模块build.gradle
plugins { id 'com.android.application' id 'kotlin-android' } android { compileSdkVersion 31 buildToolsVersion '30.0.1' defaultConfig { applicationId "com.yousufjamil.myj" minSdkVersion 16 targetSdkVersion 31 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation 'androidx.core:core-ktx:1.8.0' implementation 'androidx.appcompat:appcompat:1.4.2' implementation 'com.google.android.material:material:1.6.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' }
问题根因
报错由版本兼容性问题导致:
- 当前项目配置的Kotlin版本为1.4.32,配套的Android Gradle插件(AGP)版本为4.1.1,该版本AGP最高仅支持Kotlin 1.4.x分支
- 项目引入的
androidx.core:core-ktx:1.8.0、androidx.appcompat:appcompat:1.4.2等AndroidX依赖,均使用Kotlin 1.6+版本编译,低版本Kotlin编译器无法读取这类库的元数据,因此抛出错误 - 单独修改
ext.kotlin_version参数无法解决问题,是因为AGP 4.1.1本身不兼容1.5以上版本的Kotlin,强行升级Kotlin版本会触发新的兼容报错
可行解决方案
二选一即可:
方案1:升级AGP与Kotlin版本(推荐)
该方案可以保留高版本AndroidX依赖的新特性,步骤如下:
- 修改根目录build.gradle的buildscript依赖版本:
- 将
classpath "com.android.tools.build:gradle:4.1.1"替换为classpath "com.android.tools.build:gradle:7.0.4" - 将
ext.kotlin_version = "1.4.32"替换为ext.kotlin_version = "1.5.31"(AGP 7.0.4官方兼容的Kotlin版本为1.5.31)
- 将
- 打开项目根目录下
gradle/wrapper/gradle-wrapper.properties文件,将distributionUrl配置项修改为对应Gradle版本:distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip - 点击IDE的
Sync Project with Gradle Files按钮同步项目即可。
如果同步时提示Kotlin插件版本不匹配,直接在IDE插件市场将Kotlin插件更新到和项目配置的1.5.31版本一致,重启IDE即可。
方案2:降低AndroidX依赖版本适配当前环境
如果不想升级AGP和Gradle,直接将app模块build.gradle中的高版本AndroidX依赖替换为兼容Kotlin 1.4的版本即可,替换dependencies块内的依赖为如下版本:
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation 'androidx.core:core-ktx:1.3.2' implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'com.google.android.material:material:1.3.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
替换完成后同步项目即可解决报错。
补充提示:如果同步过程中出现jcenter依赖下载失败的问题,可以将两处repositories块中的jcenter()替换为mavenCentral()。
内容的提问来源于stack exchange,提问作者MYJ World
相关产品推荐
相关产品推荐

