75

I am using the latest Android Studio Arctic Fox 2020.03.01 Canary 8 and AGP 7, and I want to convert my project to use Java 11. Apparently just doing the following does not work as mentioned on https://developer.android.com/studio/preview/features#use-java-11:

android {
    compileSdkVersion 30

    compileOptions {
      sourceCompatibility JavaVersion.VERSION_11
      targetCompatibility JavaVersion.VERSION_11
    }

    // For Kotlin projects
    kotlinOptions {
      jvmTarget = "11"
    }
}

I get the following error when I build:

Execution failed for task ':app:compileDebugJavaWithJavac'.
> Could not resolve all files for configuration ':app:androidJdkImage'.
   > Failed to transform core-for-system-modules.jar to match attributes {artifactType=_internal_android_jdk_image, org.gradle.libraryelements=jar, org.gradle.usage=java-runtime}.
      > Execution failed for JdkImageTransform: /Users/azfarsiddiqui/Library/Android/sdk/platforms/android-30/core-for-system-modules.jar.
         > jlink executable /Applications/Android Studio Preview.app/Contents/jre/jdk/Contents/Home/bin/jlink does not exist.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

To keep it simple, I've been trying this on a Jetpack Compose sample project here: https://github.com/android/compose-samples/tree/main/JetNews

Any thoughts? Thanks guys

azfar
  • 751
  • 1
  • 4
  • 3

9 Answers9

105

From Android Studio Artic Fox 2020.3.1

Preferences (Settings) -> Build, Execution, Deployment -> Build Tools -> Gradle -> Gradle JDK -> Select JDK 11 or download JDK

enter image description here

Before Artic Fox 2020.3.1 Version

I assume you have Java 11 or later installed. Following steps:

File -> Project Structure -> SDK Location -> Change JDK Location to the Java 11 jdk folder

If you are using Mac OS then try to search for that folder in:

/Library/Java/JavaVirtualMachines/jdk-11.0.9.jdk/Contents/Home

enter image description here

Wilson Tran
  • 4,050
  • 3
  • 22
  • 31
  • 5
    The strangest thing is happening for me, JDK location does not appear as an option in the SDK location section...I don't know if it's something broken in the latest version of Arctic Fox. – nymeria Apr 30 '21 at 18:07
  • 21
    @nymeria In the Arctic Fox Canary 14, this setting has been moved to `Settings | Build, Execution, Deployment | Build Tools | Gradle | Gradle JDK` https://issuetracker.google.com/issues/186135735 – Matthieu Esnault May 01 '21 at 08:44
  • 3
    ah thank you @MatthieuEsnault! also leaving this as a comment if anyone else has upgraded to the latest recently and is getting an error about Java 11 even though everything seems to be in order, run `./gradlew --stop` and rebuild and it will work. – nymeria May 03 '21 at 20:08
  • Make sure you have you have at least Android Studio Artic Fox 2020.3.1 *PATCH 2* – Gianluca Veschi Oct 11 '21 at 10:03
20

Install OpenJDK 11. With brew it looks like this:

brew tap AdoptOpenJDK/openjdk
brew install adoptopenjdk11

In Android Studio: File -> Project Structure -> SDK Location, set the JDK location

You can find the JDK location with the command

/usr/libexec/java_home -v 11

It is /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home for me.

jeff
  • 6,299
  • 1
  • 15
  • 18
13

I had openjdk11 installed but jlink did actually not exist.

Reinstalling openjdk11 fixed this.

  • (if installed) sudo apt autoremove openjdk-11-jdk-headless
  • sudo apt install openjdk-11-jdk-headless
hb0
  • 3,350
  • 3
  • 30
  • 48
  • Sometimes (for me at least on Ubuntu 18.04) there is no openjdk-11-jdk-headless installed. So 1. step is redundant – papandreus May 10 '22 at 22:58
6

If you want remove the "Target JRE version does not match project SDK" warning, you need to change the ProjectRootManager settings in $PROJECT_DIR$/.idea/misc.xml.

enter image description here

From

<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">

to

<component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="11" project-jdk-type="JavaSDK" />

james sa
  • 631
  • 5
  • 14
2

Make sure you are using Android Gradle Plugin 7.0.0-alpha08 & Gradle Version 6.8.3 In project Structure Java 11 working fine for me

Update the IDE first

enter image description here

Ryan
  • 356
  • 1
  • 6
  • 26
Morpheus
  • 550
  • 1
  • 9
  • 23
2

File -> Project Structure -> SDK Location -> Gradle Settings -> Gradle JDKProject Structure

Gradle

Htue Ko
  • 119
  • 2
  • 7
2

Just download Android Studio Bumblebee (2021.1.1) Canary 5 https://developer.android.google.cn/studio/archive?hl=id

Seroj Grigoryan
  • 159
  • 1
  • 2
0

I also encountered this error trying to use a different JDK17 (graalvm 17.0.3) than the one bundled with Android Studio Flamingo Canary. Switching to the bundled version fixed it for me.

kenyee
  • 2,309
  • 1
  • 24
  • 32
0

I wanted to use a switch with lambdas. Such a construction, if I'm not mistaken, appears in java 14. To change the version, you can press "Ctrl"+"Alt"+"Shift"+"S" or go to File -> Project Structure. Then - SDK Location -> "JDK Location was moved to Gradle Settings" -> Gradle JDK. Select or download the desired jdk version (for example, 15). "Ok".

enter image description here

Then go to the Modules tab and change "Source Compatibility" and "Target Compatibility" to the desired version. "Ok".

enter image description here

Denis
  • 29
  • 3