build failed due to wrong jvm target

huangapple go评论56阅读模式
英文:

build failed due to wrong jvm target

问题

使用Gradle 8和Kotlin 1.8编译时,我得到了以下错误:

compileDebugJavaWithJavac' task (current target is 1.8) and 'kaptGenerateStubsDebugKotlin' task (current target is 17) 
jvm target compatibility should be set to the same Java version.

这是一个多模块项目,问题仅出现在此模块中。但是,此模块也使用相同的JVM目标和兼容性目标。但是,为什么会发生这种情况呢?

这是我为此模块的Gradle配置:

plugins {
    id 'com.android.library'
    alias libs.plugins.kotlin.android.plugin
    alias libs.plugins.kotlin.kapt.plugin
    alias libs.plugins.dagger.hilt.module.plugin
}

android {
    namespace 'com.lelestacia.network'
    compileSdk 33

    defaultConfig {
        minSdk 24

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
    }

    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'
    }
}
英文:

With Gradle 8 and Kotlin 1.8 I got an error when compiling

compileDebugJavaWithJavac' task (current target is 1.8) and 'kaptGenerateStubsDebugKotlin' task (current target is 17) 
jvm target compatibility should be set to the same Java version.

This is a multi module project, and the problem only occur to this module. But, this module also using same JVM target and compatibility target. But how does this happened?

Here is my Gradle for this module:

plugins {
    id 'com.android.library'
    alias libs.plugins.kotlin.android.plugin
    alias libs.plugins.kotlin.kapt.plugin
    alias libs.plugins.dagger.hilt.module.plugin
}

android {
    namespace 'com.lelestacia.network'
    compileSdk 33

    defaultConfig {
        minSdk 24

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
    }

    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'
    }
}

答案1

得分: 7

为了使模块编译版本与 kaptGenerateStubsDebugKotlin 相适应,您需要指定模块的编译版本兼容性。

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

但是,使用最新版本的 Gradle 并不足够,因此您必须使用 Gradle JVM 工具链功能。在模块的 Gradle 文件的 android 部分添加以下配置:

    java {
       toolchain {
           languageVersion.set(JavaLanguageVersion.of(17))
       }
    }
    kotlinOptions {
        jvmTarget = '17'
    }
英文:

You need specify module compile version's compatibility to be appropriate to kaptGenerateStubsDebugKotlin.

compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
}

But with latest versions of Gradle it is not enough, so you have to use Gradle JVM toolchain feature. Add the following config to android section of your module's gradle file

java {
   toolchain {
       languageVersion.set(JavaLanguageVersion.of(17))
   }
}
kotlinOptions {
    jvmTarget = '17'
}

答案2

得分: 3

build.gradle(:project)

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
}

build.gradle(:app)

...
compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
    jvmTarget = '17'
}
...

Android Studio - Gradle Projects

build failed due to wrong jvm target

英文:

I'm getting similar error. These processes worked for me.

build.gradle(:project)

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
}

build.gradle(:app)

...
compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = '17'
    }
...

Android Studio - Gradle Projects

build failed due to wrong jvm target

答案3

得分: 0

除了已经提到的适当的 build.gradle 设置外。

如果你从命令行运行 Gradle 任务,Gradle 会检查环境中 Java 路径的设置。你可能需要更新计算机的 Java 路径。

如果你在 Android Studio 中运行 Gradle 任务,Gradle 会检查 Android Studio 的 JDK 设置。你可能需要更新你的 IDE 的 JDK。

英文:

Besides the appropriate build.gradle settings mentioned already.

If you run a gradle task from cli, gradle checks what the environments java path is set to. You may need to update your computer's java path.

If you run a gradle task from android studio, gradle checks what Android Studio's JDK is set to. You may need to update your IDE's jdk.

huangapple
  • 本文由 发表于 2023年3月3日 21:08:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627470.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定