如何将Android项目升级到Java11
我正在使用最新的 Android Studio Arctic Fox 2020.03.01 Canary 8 和 AGP 7,并且我想将我的项目转换为使用 Java 11。显然只是执行以下操作不起作用,如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"
}
}
我在构建时收到以下错误:
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.
为简单起见,我一直在此处的 Jetpack Compose 示例项目上尝试此操作:https : //github.com/android/compose-samples/tree/main/JetNews
有什么想法吗?谢谢你们
回答
Android Studio 北极狐 2020.3.1
Preferences (Setting) -> Build, Excution, Deloyment -> Build Tools -> Gralde -> Gradle JDK -> 选择JDK 11或下载JDK
前
我假设您安装了 Java 11 或更高版本。以下步骤:
文件 -> 项目结构 -> SDK 位置 -> 将 JDK 位置更改为 Java 11 jdk 文件夹
如果您使用的是 Mac OS,请尝试在以下位置搜索该文件夹:
/Library/Java/JavaVirtualMachines/jdk-11.0.9.jdk/Contents/Home
-
@nymeria In the Arctic Fox Canary 14, this setting has been moved to
`Settings | Build, Execution, Deployment | Build Tools | Gradle | Gradle JDK`
https://issuetracker.google.com/issues/186135735 - The strangest thing is happening for me, JDK location does not appear as an option in the SDK location section...I don't know if it's something broken in the latest version of Arctic Fox.
- ah thank you @MatthieuEsnault! also leaving this as a comment if anyone else has upgraded to the latest recently and is getting an error about Java 11 even though everything seems to be in order, run `./gradlew --stop` and rebuild and it will work.