Solve android compilation problem by setup the latest java sdk on android studio for ubuntu.
After updating your Android Studio to the latest version, you may need to update each main tool you use to build your project.
Many times, you need to update your Gradle, but it’s not the most important tool to look out for. Rarely, your Java JDK needs to be updated and set up in your Android Studio.
It’s not straightforward, especially on Linux, to set up JDK on Android Studio. In this post, you will learn how to do it properly so that the next update will be in several months.
Table of Contents
How to setup java jdk on android studio on linux
To set up your Java JDK on Android Studio, you need to follow these steps. Depending on where you are, you may skip the first step.
To set up your new Java JDK on Android Studio, you need to:
- Download the latest Java JDK.
- Change Gradle settings.
- Apply changes and let the project build.
- Set up target and source compatibility in your Gradle.
If you follow these steps, your new project will be based on your latest Java JDK. Let’s dive into details.
Download your latest jdk
Usually, I look for the Java JDK compatible with Android Gradle and other tools. But lately, I tried to install the latest Java JDK that has not been tested with Android tools.
You know what? My Java JDK 20.0.1 worked with the source and target compatibility set to 20.
I recommend using the latest supported Java JDK version for your tools, but if you like exploring things, you might download the latest Java JDK version.
I don’t know which errors might occur later, but I am here to find them and troubleshoot them for you.
Download the latest Java JDK by following this link:
https://www.oracle.com/java/technologies/downloads/
Change gradle settings
To do so, we have to
- Go to android Gradle settings.
- Click on Gradle JDK and choose your java jdk .
In the last step, there are a lot of options like, directly download the latest java jdk for your android studio.
If you already get the jdk, you only have to find select it’s path on android studio.
Find Gradle Settings page on android studio can be tricky. Here two ways to find the parameter page :
How to go to Gradle settings – 1
- Press Ctrl+Alt+Shift+S or File -> Project Structure
- Then go to SDK Location
- Click Gradle Settings links on the sentence : JDK Location was moved to Gradle Settings
How to go to Gradle settings – 2
- Press Ctrl+Alt+S or File -> Settings
- Then go to Build,Execution,Deployment -> Build Tools
- Click on Gradle
After changing your gradle setting with your latest java jdk, quit the windows after clicking Apply button and Ok.
The next step is to setup target and source compatibility in build.gradle file
Setup your project target compatibility and source compatibility
- Go to file build.gradle( :app ) of your project
- Set source and target compatibility to your java jdk api version.
- Sync your project with gradle file
As an exemple, my latest java jdk api is ” 20 “. The content of build.gradle(:app) file look like this :
...
compileOptions {
sourceCompatibility = "20"
targetCompatibility = "20"
}
...
I choose to change it manually because inside the android studio IDE, i cannot select this java api.
It’s easy to change file content and then sync it with gradle.
If you pass this step without error congratulation. Your android studio project is using the latest jdk of java and the next time you will change it, will be perhaps, next year.
Happy coding !!