React Native: 'compileJava' task (current target is 1.8) and 'compileKotlin' task (current target is 11) jvm target compat

React Native: 'compileJava' task (current target is 1.8) and 'compileKotlin' task (current target is 11) jvm target compat

大家好,我正在尝试创建 运行 React 本机应用程序。我 运行 npx react-native init rn4 但是当我 运行 npm run android 我有这个错误:

> Task :react-native-gradle-plugin:compileKotlin
'compileJava' task (current target is 1.8) and 'compileKotlin' task (current target is 11) jvm target compatibility should be set to the same Java version.
w: C:\Users\emanu\App\rn4\node_modules\react-native-gradle-plugin\src\main\kotlin\com\facebook\react\ReactExtension.kt: (10, 37): 'BaseVariant' is deprecated. Deprecated in Java
w: C:\Users\emanu\App\rn4\node_modules\react-native-gradle-plugin\src\main\kotlin\com\facebook\react\ReactExtension.kt: (119, 30): 'BaseVariant' is deprecated. Deprecated in Java
w: C:\Users\emanu\App\rn4\node_modules\react-native-gradle-plugin\src\main\kotlin\com\facebook\react\ReactExtension.kt: (135, 26): 'BaseVariant' is deprecated. Deprecated in Java
w: C:\Users\emanu\App\rn4\node_modules\react-native-gradle-plugin\src\main\kotlin\com\facebook\react\ReactExtension.kt: (155, 32): 'BaseVariant' is deprecated. Deprecated in Java
w: C:\Users\emanu\App\rn4\node_modules\react-native-gradle-plugin\src\main\kotlin\com\facebook\react\ReactExtension.kt: (161, 31): 'BaseVariant' is deprecated. Deprecated in Java
w: C:\Users\emanu\App\rn4\node_modules\react-native-gradle-plugin\src\main\kotlin\com\facebook\react\ReactExtension.kt: (169, 36): 'BaseVariant' is deprecated. Deprecated in Java
w: C:\Users\emanu\App\rn4\node_modules\react-native-gradle-plugin\src\main\kotlin\com\facebook\react\ReactPlugin.kt: (99, 48): 'reactRoot: DirectoryProperty' is deprecated. reactRoot was confusing and has been replace with rootto point to your root project and reactNativeDir to point to the folder of the react-native NPM package
w: C:\Users\emanu\App\rn4\node_modules\react-native-gradle-plugin\src\main\kotlin\com\facebook\react\TaskConfiguration.kt: (10, 37): 'ApplicationVariant' is deprecated. Deprecated in Java
w: C:\Users\emanu\App\rn4\node_modules\react-native-gradle-plugin\src\main\kotlin\com\facebook\react\TaskConfiguration.kt: (11, 37): 'BaseVariant' is deprecated. Deprecated in Java
w: C:\Users\emanu\App\rn4\node_modules\react-native-gradle-plugin\src\main\kotlin\com\facebook\react\TaskConfiguration.kt: (12, 37): 'LibraryVariant' is deprecated. Deprecated in Java
w: C:\Users\emanu\App\rn4\node_modules\react-native-gradle-plugin\src\main\kotlin\com\facebook\react\TaskConfiguration.kt: (28, 51): 'BaseVariant' is deprecated. Deprecated in Java
w: C:\Users\emanu\App\rn4\node_modules\react-native-gradle-plugin\src\main\kotlin\com\facebook\react\TaskConfiguration.kt: (131, 12): 'ApplicationVariant' is deprecated. Deprecated in Java
w: C:\Users\emanu\App\rn4\node_modules\react-native-gradle-plugin\src\main\kotlin\com\facebook\react\TaskConfiguration.kt: (132, 12): 'LibraryVariant' is deprecated. Deprecated in Java
w: C:\Users\emanu\App\rn4\node_modules\react-native-gradle-plugin\src\main\kotlin\com\facebook\react\TaskConfiguration.kt: (250, 14): 'BaseVariant' is deprecated. Deprecated in Java

> Task :react-native-gradle-plugin:compileJava
6 actionable tasks: 6 executed
Note: C:\Users\emanu\App\rn4\node_modules\react-native-gradle-plugin\src\main\java\com\facebook\react\codegen\generator\SchemaJsonParser.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Users\emanu\App\rn4\android\app\build.gradle' line: 1

* What went wrong:
A problem occurred evaluating project ':app'.
> Failed to apply plugin 'com.android.internal.application'.
   > Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.
     You can try some of the following options:
       - changing the IDE settings.
       - changing the JAVA_HOME environment variable.
       - changing `org.gradle.java.home` in `gradle.properties`.

* 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.

* Get more help at https://help.gradle.org

BUILD FAILED in 59s

error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup.
Error: Command failed: gradlew.bat app:installDebug -PreactNativeDevServerPort=8081
Note: C:\Users\emanu\App\rn4\node_modules\react-native-gradle-plugin\src\main\java\com\facebook\react\codegen\generator\SchemaJsonParser.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Users\emanu\App\rn4\android\app\build.gradle' line: 1

* What went wrong:
A problem occurred evaluating project ':app'.
> Failed to apply plugin 'com.android.internal.application'.
   > Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.
     You can try some of the following options:
       - changing the IDE settings.
       - changing the JAVA_HOME environment variable.
       - changing `org.gradle.java.home` in `gradle.properties`.

* 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.

* Get more help at https://help.gradle.org

BUILD FAILED in 59s

    at makeError (C:\Users\emanu\App\rn4\node_modules\execa\index.js:174:9)
    at C:\Users\emanu\App\rn4\node_modules\execa\index.js:278:16
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async runOnAllDevices (C:\Users\emanu\App\rn4\node_modules\@react-native-community\cli-platform-android\build\commands\runAndroid\runOnAllDevices.js:109:5)
    at async Command.handleAction (C:\Users\emanu\App\rn4\node_modules\@react-native-community\cli\build\index.js:192:9)
info Run CLI with --verbose flag for more details.

我也运行 cd android && ./gradlew clean 输出是:

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Users\emanu\App\rn4\android\app\build.gradle' line: 1

* What went wrong:
A problem occurred evaluating project ':app'.
> Failed to apply plugin 'com.android.internal.application'.
   > Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.
     You can try some of the following options:
       - changing the IDE settings.
       - changing the JAVA_HOME environment variable.
       - changing `org.gradle.java.home` in `gradle.properties`.

这是android/gradle/wrapper/gradle-wrapper.properties

中的文件
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

这是我 运行 ./gradlew --version

时的输出
------------------------------------------------------------
Gradle 7.3.3
------------------------------------------------------------

Build time:   2021-12-22 12:37:54 UTC
Revision:     6f556c80f945dc54b50e0be633da6c62dbe8dc71

Kotlin:       1.5.31
Groovy:       3.0.9
Ant:          Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM:          1.8.0_302 (Oracle Corporation 25.302-b08)
OS:           Windows 10 10.0 amd64

我阅读了类似的帖子,但我无法修复它。使用我几天前创建的文件夹,当我 运行 应用程序时没有问题。

有人可以帮助我吗? 非常感谢!

Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.

最终你需要更新版本的 Java 到 运行。有许多在线资源可以帮助您解决这个问题。因为它推荐 Java 11 我会坚持下去。获得 Java 11 for Windows 的一个地方在 Oracle downloads site 上(因为您当前拥有 Oracle 构建),但还有其他与 OpenJDK 相关的地方。

如果您使用的是 React Native Chocolatey,您必须将 JDK 版本更新到 11。
为了进行更新,请在 PowerShell 中执行此操作(具有管理员权限):

choco install -y openjdk11

在那之后,你第一次(只是第一次)运行npm android,你会收到很多警告,但是项目的构建是可行的。

如果您继续收到错误,您可能需要调整项目的 gradle 版本以与新的 JDK 版本兼容。
您可以通过编辑文件 YOUR_PROJECT\android\gradle\wrapper\gradle-wrapper.properties
来实现此目的 将 distributionUrl 的版本更新为 7.4.2

希望对您有所帮助

我在这里遇到了同样的问题。我尝试了 MouTio 的解决方案并在 Project-Name\android\gradle\wrapper\gradle-wrapper.properties 将 distributionUrl 更新为 7.4.2 但这仍然给我这个错误:

> Task :react-native-gradle-plugin:compileKotlin FAILED

'compileJava' 任务(当前目标为 1.8)和 'compileKotlin' 任务(当前目标为 11)jvm targeta 版本。 1 个可操作任务:1 个已执行

失败:构建失败,出现异常。

  • 出了什么问题: 任务 ':react-native-gradle-plugin:compileKotlin' 执行失败。

Failed to query the value of task ':react-native-gradle-plugin:compileKotlin' property 'compilerRun Kotlin could not find the required JDK tools in the Java installation. Make sure Kotlin compilat

但是,在使用管理权限通过命令行 运行 执行此 choco install -y nodejs-lts openjdk11 后错误已得到修复。

并且不要忘记 运行 npm 运行 android 使用刷新的终端。

消息终于消失了

在此之前执行以下操作[我正在使用 Mac]

  1. 已安装 JDK11 并设置 HOME
  2. 如果您有任何旧版本或已安装最新的 18,而不是错误中提到的。然后按照此 删除安装并确保路径设置正确 [参考得分最高的]
  3. 现在按照这个设置路径 URL。由 Arv
  4. 撰写
  5. 现在在 android 文件夹中查找此文件 android/.idea/compiler.xml 并将目标设置为“ 1.8”到“11
  6. 运行 ./gradlew clean [运行 两次..否则相同的消息将可见]