Android Studio项目升级报错:
Inconsistent JVM-target compatibility detected for tasks ‘compileDebugJavaWithJavac’ (1.8) and ‘compileDebugKotlin’ (17).
Android Studio版本是:
根目录中的gradle\wrapper\gradle-wrapper.properties:
#Wed Dec 23 11:45:01 CST 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
根目录中的build.gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '2.0.0'
repositories {
google()
jcenter()
}
dependencies {
// AGP版本
classpath 'com.android.tools.build:gradle:8.4.2'
// KGP版本
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()
}
// 添加这行解决报错
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
kotlinOptions.jvmTarget = "17"
}
// 添加这行解决报错
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KaptGenerateStubs).configureEach {
kotlinOptions.jvmTarget = "17"
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
根目录\app\中的build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
// 已弃用kotlin-android-extensions
//apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 34
defaultConfig {
applicationId "com.hencoder.text"
minSdkVersion 21
targetSdkVersion 34
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_17
targetCompatibility JavaVersion.VERSION_17
}
// 添加这行解决报错
kotlinOptions {
jvmTarget =17 }
/*
项目升级时没有添加这个namespace也会报错。
这里的包名路径应该与AndroidManifest.xml中的<manifest/>中的package属性值一致
*/
namespace "com.hencoder.text"
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.5.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
总结,解决报错Inconsistent JVM-target compatibility detected for tasks ‘compileDebugJavaWithJavac’ (1.8) and ‘compileDebugKotlin’ (17).的方法是:
步骤一:根目录\app\中的build.gradle中添加
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget =17 }
}
步骤二:根目录中的build.gradle中添加
allprojects {
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
kotlinOptions.jvmTarget = "17"
}
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KaptGenerateStubs).configureEach {
kotlinOptions.jvmTarget = "17"
}
}