1、JDK的选择
词汇表
Java Development Kit (JDK)
JDK包含:工具(编译器、解析器、产物生成器)、Kotlin或Java调用库(但部分API在Android中不可用)、JVM(这个JVM没用应用在Android设备或模拟器上)
JetBrains Runtime (JBR)
JBR是一款加强JDK,与Android Studio一起发布。对JetBrains相关的产品做了一些优化。
Android Studio 启动脚本查找 JVM 顺序如下:
1、STUDIO_JDK environment variable
2、studio.jdk directory (in the Android Studio distribution)
3、jbr directory (JetBrains Runtime), in the Android Studio distribution. Recommended.
4、JDK_HOME environment variable
5、JAVA_HOME environment variable
6、(Windows系统)java executable in the PATH environment variable
Gradle构建时选择哪个JDK?
1、如果使用Android Studio的按钮构建,则使用的是Android Studio设置页面中的JDK(示例:Build Tools-Gradle中指定的JDK)
2、如果在Terminal中构建,无论是Android Studio内的Terminal还是Mac系统的Terminal,JAVA_HOME变量决定了Gradle脚本使用的是哪个JDK,如果JAVA_HOME没有设置,则使用Windows系统中的PATH环境变量中的java配置。
为了保持一致性,建议JAVA_HOME和Android Studio的设置页面,设置同一个JDK。
所选择的JDK用于Gradle脚本构建和解析Java API的引用。需要注意的是,compileSdk所指定的版本也会约束java符号是否可用。
Android Gradle Plugin (AGP) 的版本对JDK的最低版本有要求:
比如:the Android Gradle Plugin version 8.x requires JDK 17.
每一个Android版本仅支持指定的JDK版本。如果使用Java API在指定的compileSdk版本下可以使用,但minSdk版本下不可以用时,可以使用脱糖技术:
android {
defaultConfig {
// Required when setting minSdkVersion to 20 or lower
multiDexEnabled = true
}
compileOptions {
// Flag to enable support for the new language APIs
// For AGP 4.1+
isCoreLibraryDesugaringEnabled = true
// For AGP 4.0
// coreLibraryDesugaringEnabled = true
// Sets Java compatibility to Java 8
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
dependencies {
// For AGP 7.4+
coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:2.0.3")
// For AGP 7.3
// coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:1.2.3")
// For AGP 4.0 to 7.2
// coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:1.1.9")
}
Android版本支持的Java版本
Android | Java | API and language features supported |
---|---|---|
14 (API 34) | 17 | Core libraries |
13 (API 33) | 11 | Core libraries |
12 (API 32) | 11 | Java API |
11 and lower | Android versions |
https://developer.android.com/build/jdks
The following table lists which version of Gradle is required for each version of the Android Gradle plugin. For the best performance, you should use the latest possible version of both Gradle and the plugin.
Plugin version | Minimum required Gradle version |
---|---|
8.4 (alpha) | 8.6-rc-1 |
8.3 (beta) | 8.4 |
8.2 | 8.2 |
8.1 | 8.0 |
8.0 | 8.0 |
7.4 | 7.5 |
The following table lists which version of Android Gradle plugin(AGP) is required for each version of Android Studio.
Android Studio version | Required AGP version |
---|---|
Iguana - 2023.2.1 | 3.2-8.3 |
Hedgehog - 2023.1.1 | 3.2-8.2 |
Giraffe - 2022.3.1 | 3.2-8.1 |
Flamingo - 2022.2.1 | 3.2-8.0 |
Electric Eel - 2022.1.1 | 3.2-7.4 |
The minimum versions of Android Studio and AGP are as follows:
API level | Minimum Android Studio version | Minimum AGP version |
---|---|---|
34 | Hedgehog - 2023.1.1 | 8.1.1 |
33 | Flamingo - 2022.2.1 | 7.2 |
https://developer.android.com/build/releases/gradle-plugin
Make sure to choose a JDK version that is higher than or equal to the JDK versions used by plugins that you use in your Gradle build. To determine the minimum required JDK version for the Android Gradle Plugin (AGP), see the compatibility table in the release notes.
For example, the Android Gradle Plugin version 8.x requires JDK 17. If you try to run a Gradle build that uses it with an earlier version of the JDK, it reports a message like:
An exception occurred applying plugin request [id: ‘com.android.application’]
Failed to apply plugin ‘com.android.internal.application’.
Android Gradle plugin requires Java 17 to run. You are currently using Java 11.
Your current JDK is located in /usr/local/buildtools/java/jdk11
You can try some of the following options:
- changing the IDE settings.
- changing the JAVA_HOME environment variable.
- changingorg.gradle.java.home
ingradle.properties
.