Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which java version should I use in Android Studio to build an app?

Which java version should I use in android projects, I know by default android set target and compiler version to JAVA 1.8, but gradle sdk is set to java 11 by default. And In somewhere in compose documentation I see we can use java 17 for target and compile options. I just want to know how and from where I can check latest supported java version in android because I want to use latest java version. Reason for that is newer version comes with new features that' why. I

like image 239
Shubham Kumar Avatar asked Apr 06 '26 21:04

Shubham Kumar


1 Answers

Please see Java versions in Android builds. (Disclosure: I wrote it)

The main place Google keeps the info for which Java levels are supported is Which Java APIs can I use in my Java or Kotlin source code?

The Android compileSdk determines which JDK API are available for use in your source code (Kotlin or Java).

Be aware that there are multiple JDKs in play:

  • JDK that runs Android Studio - let it default to the JetBrains Runtime (JBR) that comes with Android Studio

  • JDK that runs Gradle - set it in Android Studio for Studio builds or via org.gradle.java.home in gradle.properties, which defaults to JAVA_HOME, for shell builds

  • Toolchain JDK - default for source and target levels, as well as JDK used to run unit tests. I strongly recommend using this instead of sourceCompatibility, targetCompatibility and jvmTarget. Set by adding

    kotlin {
       jvmToolchain(17)
    }
    

    at the top of your module's build.gradle.kts, and remove sourceCompatibility, targetCompatibility and jvmTarget settings.

  • sourceCompatibility - Java level for Java source (defaults to toolchain)

  • targetCompatibility - Java bytecode generation level for Java source (defaults to toolchain)

  • jvmTarget - Java bytecode generation level for Kotlin source (defaults to toolchain)

A simple resolver to use is https://plugins.gradle.org/plugin/org.gradle.toolchains.foojay-resolver-convention. This is a settings plugin; you'll apply it at the top of your settings.gradle.kts. For example:

// Copyright 2024 Google LLC.
// SPDX-License-Identifier: Apache-2.0

// at top of settings.gradle.kts
plugins {
    id("org.gradle.toolchains.foojay-resolver-convention") version "0.5.0"
}
like image 77
Scott Stanchfield Avatar answered Apr 09 '26 09:04

Scott Stanchfield



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!