How to upgrade an Android project to Java 11

29,141

Solution 1

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

Solution 2

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.

Solution 3

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" />

Solution 4

I had openjdk11 installed but jlink did actually not exist.

Reinstalling openjdk11 fixed this.

  • sudo apt autoremove openjdk-11-jdk-headless
  • sudo apt install openjdk-11-jdk-headless

Solution 5

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

Share:
29,141
Admin
Author by

Admin

Updated on January 17, 2022

Comments

  • Admin
    Admin over 2 years

    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