React Native 构建无法评估“android”

问题描述 投票:0回答:1

我正在尝试

build
在 Android Studio (Koala [v 2024]) 上使用 React Native (
$ npx react-native init AnAppName
) 创建的应用程序。

我遇到了一些问题,因为我的 Java 和 Gradle 需要更新和添加路径。更新Java和Gradle后,我刚刚删除了以前的项目并开始了一个新的项目。这个新应用程序也没有修改任何代码。我所做的就是执行

$ npx react-native init AnAppName
,在Android Studio(Koala)中打开它,然后执行
$ npx react-native run-android
。我已正确连接设备进行测试并创建了 Android 模拟器。

向终端(Android Studio Powershell 终端)抛出的错误代码是:

    ERROR: autolinkLibrariesFromCommand: process cmd /c npx @react-native-community/cli config exited with error code: 1
    
    FAILURE: Build failed with an exception.
    
    * Where:
    Settings file 'C:\Users\USER NAME\reactNativeProjects\aaaandanotherproject\android\settings.gradle' line: 3
    
    * What went wrong:
    A problem occurred evaluating settings 'android'.
    > ERROR: autolinkLibrariesFromCommand: process cmd /c npx @react-native-community/cli config exited with error code: 1

提到的 settings.gradle 文件(自初始化应用程序以来未经编辑)是:

路径:项目名称/android/settings.gradle 文件开始:settings.gradle

    pluginManagement { includeBuild("../node_modules/@react-native/gradle-plugin") }
    plugins { id("com.facebook.react.settings") }
    extensions.configure(com.facebook.react.ReactSettingsExtension){ ex ->     ex.autolinkLibrariesFromCommand() }
    rootProject.name = 'aaaandanotherproject'
    include ':app'
    includeBuild('../node_modules/@react-native/gradle-plugin')

文件结束:settings.gradle

我尝试从

settings.gradle
行中删除第三行:

    extensions.configure(com.facebook.react.ReactSettingsExtension){ ex ->     ex.autolinkLibrariesFromCommand() }

但这只是创建了一个错误,表示

':app'
未初始化。

包含 autolinkLibrariesFromCommand 函数的

ReactSettingsExtension.kt
文件(错误中列出的是):

路径:项目名称/node_modules/@react-native/gradle-plugin/settings-plugin/src/main/kotlin/com/facebook/react/ReactSettingsExtension.kt

文件开始:ReactSettingsExtension.kt


package com.facebook.react

import com.facebook.react.utils.JsonUtils
import com.facebook.react.utils.windowsAwareCommandLine
import java.io.File
import java.math.BigInteger
import java.security.MessageDigest
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import org.gradle.api.GradleException
import org.gradle.api.file.FileCollection
import org.gradle.api.initialization.Settings
import org.gradle.api.logging.Logging

abstract class ReactSettingsExtension @Inject constructor(val settings: Settings) {

  private val outputFile =
      settings.layout.rootDirectory.file("build/generated/autolinking/autolinking.json").asFile
  private val outputFolder =
      settings.layout.rootDirectory.file("build/generated/autolinking/").asFile

  private val defaultConfigCommand: List<String> =
      windowsAwareCommandLine(listOf("npx", "@react-native-community/cli", "config")).map {
        it.toString()
      }

  @JvmOverloads
  public fun autolinkLibrariesFromCommand(
      command: List<String> = defaultConfigCommand,
      workingDirectory: File? = settings.layout.rootDirectory.dir("../").asFile,
      lockFiles: FileCollection =
          settings.layout.rootDirectory
              .dir("../")
              .files("yarn.lock", "package-lock.json", "package.json", "react-native.config.js")
  ) {
    outputFile.parentFile.mkdirs()
    val lockFilesChanged = checkAndUpdateLockfiles(lockFiles, outputFolder)
    if (lockFilesChanged || outputFile.exists().not() || outputFile.length() != 0L) {
      val process =
          ProcessBuilder(command)
              .directory(workingDirectory)
              .redirectOutput(ProcessBuilder.Redirect.to(outputFile))
              .redirectError(ProcessBuilder.Redirect.INHERIT)
              .start()
      val finished = process.waitFor(5, TimeUnit.MINUTES)
      if (!finished || (process.exitValue() != 0)) {
        val prefixCommand =
            "ERROR: autolinkLibrariesFromCommand: process ${command.joinToString(" ")}"
        val message =
            if (!finished) "${prefixCommand} timed out"
            else "${prefixCommand} exited with error code: ${process.exitValue()}"
        val logger = Logging.getLogger("ReactSettingsExtension")
        logger.error(message)
        if (outputFile.length() != 0L) {
          logger.error(outputFile.readText().substring(0, 1024))
        }
        outputFile.delete()
        throw GradleException(message)
      }
    }
    linkLibraries(getLibrariesToAutolink(outputFile))
  }

  public fun autolinkLibrariesFromConfigFile(
      autolinkConfigFile: File,
  ) {
    // We copy the file to the build directory so that the various Gradle tasks can access it.
    autolinkConfigFile.copyTo(outputFile, overwrite = true)
    linkLibraries(getLibrariesToAutolink(autolinkConfigFile))
  }

  private fun linkLibraries(input: Map<String, File>) {
    input.forEach { (path, projectDir) ->
      settings.include(path)
      settings.project(path).projectDir = projectDir
    }
  }

  companion object {
    private val md = MessageDigest.getInstance("SHA-256")
    internal fun checkAndUpdateLockfiles(lockFiles: FileCollection, outputFolder: File): Boolean {
      var changed = false
      lockFiles.forEach { lockFile ->
        if (lockFile.exists()) {
          val sha = computeSha256(lockFile)
          val shaFile = File(outputFolder, "${lockFile.name}.sha")
          if (shaFile.exists().not() || shaFile.readText() != sha) {
            shaFile.writeText(sha)
            changed = true
          }
        }
      }
      return changed
    }

    internal fun getLibrariesToAutolink(buildFile: File): Map<String, File> {
      val model = JsonUtils.fromAutolinkingConfigJson(buildFile)
      return model
          ?.dependencies
          ?.values
          // We handle scenarios where there are deps that are
          // iOS-only or missing the Android configs.
          ?.filter { it.platforms?.android?.sourceDir != null }
          // We want to skip dependencies that are pure C++ as they won't contain a .gradle file.
          ?.filterNot { it.platforms?.android?.isPureCxxDependency == true }
          ?.associate { deps ->
            ":${deps.nameCleansed}" to File(deps.platforms?.android?.sourceDir)
          } ?: emptyMap()
    }

    internal fun computeSha256(lockFile: File) =
        String.format("%032x", BigInteger(1, md.digest(lockFile.readBytes())))
  }
}

文件结束:ReactSettingsExtension.kt

自动创建且尚未被我修改的package.json文件是:

路径:项目名称/package.json

{
  "name": "aaaandanotherproject",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "lint": "eslint .",
    "start": "react-native start",
    "test": "jest"
  },
  "dependencies": {
    "react": "18.3.1",
    "react-native": "0.75.2"
  },
  "devDependencies": {
    "@babel/core": "^7.20.0",
    "@babel/preset-env": "^7.20.0",
    "@babel/runtime": "^7.20.0",
    "@react-native/babel-preset": "0.75.2",
    "@react-native/eslint-config": "0.75.2",
    "@react-native/metro-config": "0.75.2",
    "@react-native/typescript-config": "0.75.2",
    "@types/react": "^18.2.6",
    "@types/react-test-renderer": "^18.0.0",
    "babel-jest": "^29.6.3",
    "eslint": "^8.19.0",
    "jest": "^29.6.3",
    "prettier": "2.8.8",
    "react-test-renderer": "18.3.1",
    "typescript": "5.0.4"
  },
  "engines": {
    "node": ">=18"
  }
}

文件结束:package.json

我该怎么办?

react-native
1个回答
0
投票

从未遇到过此类问题。一个可能的解决方案是检查 package.json 文件并检查 @react-native-community/cli 和react-native 包之间的兼容性

"dependencies": {
    ...

    "@react-native-community/cli": "^5.0.1", --> you have to check this version 
    "react-native": "0.63.3"

    ...
}
© www.soinside.com 2019 - 2024. All rights reserved.