您好,我尝试在 Android Studio(版本 4.1)上运行 Kotlin 文件,但出现错误: Kotlin 库 {0} 是使用较新的 Kotlin 编译器编译的,无法读取。请更新 Kotlin 插件。
即使我遇到这个错误,我也可以运行 kotlin 文件,但当我使用 kotlin 函数和方法时,Ide 没有给我提示,并用红色标记。 gradle中的Kotlin版本是1.6.0,如果我返回到旧版本,我可以看到有关方法名称的提示,并且不会显示红色。 Gradle 构建(应用程序):
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 31
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.kotlin_udemy4"
minSdkVersion 26
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.7.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
Gradle 构建:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.6.0"
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.0"
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
}
有人知道如何解决这个问题吗?
最佳答案
我也有类似的问题。为了解决这个问题,我做了以下事情:
我尝试重建项目,然后出现以下错误日志: a中指定的minCompileSdk(32) 依赖项的 AAR 元数据 (META-INF/com/android/build/gradle/aar-metadata.properties) 大于该模块的compileSdkVersion (android-31)。 依赖项:androidx.appcompat:appcompat-资源:1.5.0。 AAR 元数据文件:C:\Users.........
将 androidx.appcompat:appcompat 版本降低到 1.4.0,然后同步 gradle。就是这样。
也许你可以试试这个方法。
关于android - Kotlin 库是用较新的 Kotlin 编译器编译的,无法读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72468897/