Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upgrade an Android project to Java 11

Tags:

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

like image 901
azfar Avatar asked Mar 03 '21 00:03

azfar


People also ask

Can I use Java 11 in Android Studio?

As a requirement to run, PSPDFKit for React Native needs the latest stable version of Android Studio. Starting with version 4.2 , Android Studio is now bundled with JDK 11.

Which Java version should I use for Android development?

Set the JDK version A copy of the latest OpenJDK comes bundled with Android Studio 2.2 and higher, and this is the JDK version we recommend you use for your Android projects.

How to install Android 11 SDK in Android Studio?

After you install and open Android Studio, install the Android 11 SDK as follows: 1 Click Tools > SDK Manager. 2 In the SDK Platforms tab, select Android 11. 3 In the SDK Tools tab, select Android SDK Build-Tools 30 (or higher). 4 Click OK to begin install.

Why should you upgrade to Java 11 from Java 8?

As a result, most of the companies decided to upgrade to Java 11 to get the latest updates or else they have to pay to receive Oracle JDK 8 support from Oracle or stay with Oracle JDK 8 without receiving any updates, which is not recommended.

How do I get Started with Android 11?

Below are the steps to fully support Android 11. To get started with full Android 11 support, first download the Android 11 SDK (and any other tools needed) into Android Studio. Next change the app's targetSdkVersion and compileSdkVersion to "30" and re-compile the app.

How do I update my Android app's build configuration to Android 11?

Changing your app's build configuration to target Android 11 gives your app access to the Android 11 APIs and lets you fully test your app's compatibility as you prepare to add full support for Android 11. To do this, open your module-level build.gradle file and update the compileSdkVersion and targetSdkVersion: ...


2 Answers

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

like image 197
Wilson Tran Avatar answered Sep 28 '22 15:09

Wilson Tran


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.

like image 23
ham Avatar answered Sep 28 '22 15:09

ham