Gradle building android project with Java 1.8 even though java gradle toolchain is set to jdk 11

Viewed 5209
FAILURE: Build failed with an exception.
* Where:
Build file '/bitrise/src/app/build.gradle' line: 1
* What went wrong:
A problem occurred evaluating project ':app'.
> Failed to apply plugin 'com.android.internal.application'.
   > Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.
     You can try some of the following options:
       - changing the IDE settings.
       - changing the JAVA_HOME environment variable.
       - changing `org.gradle.java.home` in `gradle.properties`.

Gradle detects locally installed JVMs Gradle chooses a JRE/JDK matching the requirements of the build (in this case a JVM supporting Java 14) If no matching JVM is found, it will automatically download a matching JDK from AdoptOpenJDK

Does Android Gradle Plugin support the new gradle toolchains? https://docs.gradle.org/current/userguide/toolchains.html

Notable libraries / plugins / versions

  • Running AGP 7.0.0-alpha03
  • Kotlin 1.4.21
  • Android Studio Arctic Fox Canary 3
  • jvmTarget 1.8
  • gradle wrapper 6.8-rc-1

App module gradle config below (some details removed)

android {

    java {
        toolchain {
            languageVersion = JavaLanguageVersion.of(11)
        }
    }

    kotlinOptions {
        jvmTarget = Versions.jdkNumber
        useIR = true
    }

    buildToolsVersion Config.buildToolsVersion
    compileSdkVersion Config.compileSdk

    buildFeatures {
        compose true
        dataBinding true
    }

    composeOptions {
        kotlinCompilerVersion Versions.kotlin_version
        kotlinCompilerExtensionVersion Versions.compose
    }

    androidExtensions {
        experimental = true
    }
}
3 Answers

You might check this out. I had a similar problem and it fixed it for me. This is Gradle's own "snippet" of code for the problem you're facing.

import org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompile

plugins {
    id "org.jetbrains.kotlin.jvm" version "1.4.20"
}

repositories {
    jcenter()
}

// tag::compiler-kotlin[]
def compiler = javaToolchains.compilerFor {
    languageVersion = JavaLanguageVersion.of(11)
}

tasks.withType(KotlinJvmCompile).configureEach {
    kotlinOptions.jdkHome = compiler.get().metadata.installationPath.asFile.absolutePath
}
// end::compiler-kotlin[]

This was found at https://github.com/gradle/gradle/blob/master/subprojects/docs/src/snippets/java/toolchain-kotlin/groovy/build.gradle

I incorporated it in my project at https://github.com/7ep/r3z/blob/51cc69bb6396c34f0507b67763487c9bc171a03e/build.gradle#L36

Good luck!

Byron

I had the same issue and found out that Bitrise Ubuntu Stack is using Java 8. In order to switch to java 11, a script step is required. Follow the below on how to do it.

Add a Script Step to the Workflow before any Step that uses Java in any way. The simplest way to do it is to place it as the first Step of the Workflow. Add the following commands to the Script content input of the Step:

sudo update-alternatives --set javac /usr/lib/jvm/java-11-openjdk-amd64/bin/javac
sudo update-alternatives --set java /usr/lib/jvm/java-11-openjdk-amd64/bin/java

export JAVA_HOME='/usr/lib/jvm/java-11-openjdk-amd64'
envman add --key JAVA_HOME --value '/usr/lib/jvm/java-11-openjdk-amd64'

The above info comes from Bitrise documentation. If you want to read more then follow link below. https://devcenter.bitrise.io/infrastructure/virtual-machines/#switching-to-java-11

Related