【问题标题】:React Native: 'compileJava' task (current target is 1.8) and 'compileKotlin' task (current target is 11) jvm target compatReact Native:\'compileJava\' 任务(当前目标为 1.8)和 \'compileKotlin\' 任务(当前目标为 11)jvm 目标兼容
【发布时间】:2023-02-17 01:55:13
【问题描述】:

大家好,我正在尝试创建和运行 React Native 应用程序。我跑 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

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

有人可以帮我吗? 很感谢!

【问题讨论】:

    标签: java react-native gradle


    【解决方案1】:

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

    choco install -y openjdk11
    

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

    如果继续收到错误,可能需要调整项目的 gradle 版本以兼容新的 JDK 版本。
    您可以通过编辑文件来实现此目的YOUR_PROJECTndroidgradlewrappergradle-wrapper.properties
    更新版本分布网址至 7.4.2

    希望对您有所帮助

    【讨论】:

    • 更新 distributionUrl 为我修复了它
    • 之后,需要cd ./android/ && ./gradlew clean清理
    【解决方案2】:

    消息终于消失了。

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

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

    【讨论】:

    • 这对我不起作用,但如果人们想要复制/粘贴整个内容:<?xml version="1.0" encoding="UTF-8"?> <project version="4"> <component name= "CompilerConfiguration"> <bytecodeTargetLevel target="18" > <module name="react-native-gradle-plugin" target="11" /> <module name="react-native-gradle-plugin.main" target=" 11" /> <module name="react-native-gradle-plugin.test" target="11" /> </bytecodeTargetLevel> </component> </project>
    【解决方案3】:

    我刚刚安装了 jdk11 然后运行 ​​cd ./android/ &amp;&amp; ./gradlew clean 这对我有用

    【讨论】:

      【解决方案4】:

      Android Gradle 插件需要 Java 11 才能运行。您当前使用的是 Java 1.8。

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

      【讨论】:

      • 我安装了但我有同样的错误。当我配置 React Native 时,我记得我运行“choco install -y nodejs.install openjdk8”来安装 Java SE Development Kit (JDK)。现在我看到 (link) 命令是“choco install -y nodejs-lts openjdk11”。如果我运行它可能会解决问题?
      【解决方案5】:

      我在这里遇到了同样的问题。我尝试了 MouTio 的解决方案并在 Project-Namendroidgradlewrappergradle-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' 执行失败。

      无法查询任务“:react-native-gradle-plugin:compileKotlin”属性“compilerRun”的值 Kotlin 在 Java 安装中找不到所需的 JDK 工具。确保 Kotlin compilat

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

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

      【讨论】:

        【解决方案6】:
        openjdk11 v11.0.16.20220913 already installed.
        
        node js version: v18.14.0
        
        java version "19.0.2" 2023-01-17
        Java(TM) SE Runtime Environment (build 19.0.2+7-44)
        Java HotSpot(TM) 64-Bit Server VM (build 19.0.2+7-44, mixed mode, sharing)
        

        仍然有同样的问题。

        【讨论】:

          【解决方案7】:

          任务:react-native-gradle-plugin:compileKotlin “compileJava”任务(当前目标为 1.8)和“compileKotlin”任务(当前目标为 11)jvm 目标兼容性应设置为相同的 Java 版本。

          解决方案

          执行以下命令

          • 运行cmd(管理员)过去 choco install -y openjdk11

          【讨论】:

            猜你喜欢
            • 2022-10-17
            • 2021-11-03
            • 1970-01-01
            • 2011-06-21
            • 1970-01-01
            • 1970-01-01
            • 2018-01-10
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多