Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
    }
}
like image 389
Scott Kruse Avatar asked Dec 17 '20 00:12

Scott Kruse


People also ask

How do I specify Java 11 in Gradle?

Right click on the deploy or any other task and select "Open Gradle Run Configuration..." Then navigate to "Java Home" and paste your desired java path.

What version of Java does Gradle use?

Gradle can only run on Java version 8 or higher. Gradle still supports compiling, testing, generating Javadoc and executing applications for Java 6 and Java 7.

What is Gradle toolchain?

Gradle 6.7 introduces “Java toolchain support”. In a nutshell, it allows you to run Gradle with whatever version of Java you have installed, while Gradle builds the project with the version of Java declared in a single place.

How to build a Java project using Gradle build file?

This chapter explains how to build a java project using Gradle build file. First of all, we have to add java plugin to the build script, because, it provides the tasks to compile Java source code, to run the unit tests, to create a Javadoc and to create a JAR file. Use the following line in build.gradle file.

What version of Java do I need to run Android Gradle plugin?

Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8 I'm trying to use a physical Android device to test/debug my React Native project. I connected the device to my Windows 11 machine via USB and turned on Developer mode on the device but when I tried to run my project, I got an error stating the following:

Can I use a Java toolchain instead of Gradle?

Additionally, you may want to build a project using a Java version that is not supported for running Gradle. A Java Toolchain (from now on referred to simply as toolchain) is a set of tools, usually taken from a local JRE/JDK installation that are used to configure different aspects of a build.

Why Joda Time is not declared in Gradle build?

If you ran gradle build to build the project now, the build would fail because you have not declared Joda Time as a compile dependency in the build. For starters, you need to add a source for 3rd party libraries.


2 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

like image 117
Byron Katz Avatar answered Oct 13 '22 15:10

Byron Katz


Just solved this by adding jitpack.yml file in project root folder with content:

jdk:
  - openjdk11
  • Helpful reading: https://ayoubbenkhemis.medium.com/publish-android-library-with-jitpack-be00a29a8174
like image 27
Sam Chen Avatar answered Oct 13 '22 13:10

Sam Chen