使用Hermes构建应用时出错:“ ..”未被识别为内部或外部命令 更改后,现在的构建输出如下:

问题描述

  • 在Windows 10上构建本机应用程序。

  • 针对第165行的错误打印 node_modules\react-native\react.gradle

'..'不被识别为内部或外部命令,可操作程序或批处理文件

第165行是下面的第5行,从exec开始:

if (enableHermes) {
                doLast {
                    def hermesFlags;
                    def hbcTempFile = file("${jsBundleFile}.hbc")
                    exec {
                        if (targetName.toLowerCase().contains("release")) {
                            // Can't use ?: since that will also substitute valid empty lists
                            hermesFlags = config.hermesFlagsRelease
                            if (hermesFlags == null) hermesFlags = ["-O","-output-source-map"]
                        } else {
                            hermesFlags = config.hermesFlagsDebug
                            if (hermesFlags == null) hermesFlags = []
                        }

                        if (Os.isFamily(Os.FAMILY_WINDOWS)) {
                            commandLine("cmd","/c",getHermesCommand(),"-emit-binary","-out",hbcTempFile,jsBundleFile,*hermesFlags)
                        } else {
                            commandLine(getHermesCommand(),*hermesFlags)
                        }
                    }
                    ant.move(
                        file: hbcTempFile,toFile: jsBundleFile
                    );
                    if (hermesFlags.contains("-output-source-map")) {
                        ant.move(
                            // Hermes will generate a source map with this exact name
                            file: "${jsBundleFile}.hbc.map",tofile: jsCompilerSourceMapFile
                        );
                        exec {
                            // Todo: set task dependencies for caching

                            // Set up the call to the compose-source-maps script
                            workingDir(reactRoot)
                            if (Os.isFamily(Os.FAMILY_WINDOWS)) {
                                commandLine("cmd",*nodeExecutableAndArgs,composeSourceMapsPath,jsPackagerSourceMapFile,jsCompilerSourceMapFile,"-o",jsOutputSourceMapFile)
                            } else {
                                commandLine(*nodeExecutableAndArgs,jsOutputSourceMapFile)
                            }
                        }
                    }
                }
            }

以下是android / app / build.gradle中hermescommand的定义:

project.ext.react = [
    enableHermes: true,// clean and rebuild if changing
    // next added by Yossi
    hermesCommand: "../../node_modules/hermesengine/win64-bin/hermes",extraPackagerArgs: ["--sourcemap-output","$buildDir/intermediates/assets/release/index.android.bundle.map"]
]

据我所知,指向以下文件

node_modules\hermes-engine\win64-bin\hermes.exe

有什么主意吗?

更新了android / app / build.gradle中的定义(遵循以下答案):

project.ext.react = [
    enableHermes: true,// clean and rebuild if changing
    // -either- hermesCommand: "..\\..\\node_modules\\hermesengine\\win64-bin\\hermes",// -or- hermesCommand: "../../node_modules/hermesengine/%OS-BIN%/hermes",hermesCommand: "../../node_modules/hermesengine/%OS-BIN%/hermes","$buildDir/intermediates/assets/release/index.android.bundle.map"]
]

更改后,现在的构建输出如下:

> Task :app:bundleReleaseJsAndAssets
warning: the transform cache was reset.
                 Welcome to React Native!
                Learn once,write anywhere


info Writing bundle output to:,C:\esites-grocery\test1\plumpclient\android\app\build\generated\assets\react\release\index.android.bundle
info Writing sourcemap output to:,C:\esites-grocery\test1\plumpclient\android\app\build/intermediates/assets/release/index.android.bundle.map
info Done writing bundle output
info Done writing sourcemap output
info copying 141 asset files
info Done copying assets
The system cannot find the path specified.

> Task :app:bundleReleaseJsAndAssets Failed

> Task :app:bundleReleaseJsAndAssets_SentryUpload_3001
Processing react-native sourcemaps for Sentry upload.
> Analyzing 2 sources
> rewriting sources
> Adding source map references
Uploading sourcemaps for release com.plumpplum@RN62 Hermes+3001 distribution 3001
> Bundled 2 files for upload
> Uploaded release files to Sentry
> File upload complete

Source Map Upload Report
  Minified Scripts
    ~/index.android.bundle (sourcemap at index.android.bundle.map)
  Source Maps
    ~/index.android.bundle.map

> Task :app:bundleReleaseJsAndAssets_SentryUpload_1001
Processing react-native sourcemaps for Sentry upload.
> Analyzing 2 sources
> rewriting sources
> Adding source map references
Uploading sourcemaps for release com.plumpplum@RN62 Hermes+1001 distribution 1001
> Bundled 2 files for upload
> Uploaded release files to Sentry
> File upload complete

Source Map Upload Report
  Minified Scripts
    ~/index.android.bundle (sourcemap at index.android.bundle.map)
  Source Maps
    ~/index.android.bundle.map

> Task :app:bundleReleaseJsAndAssets_SentryUpload_2001
Processing react-native sourcemaps for Sentry upload.
> Analyzing 2 sources
> rewriting sources
> Adding source map references
Uploading sourcemaps for release com.plumpplum@RN62 Hermes+2001 distribution 2001
> Bundled 2 files for upload
> Uploaded release files to Sentry
> File upload complete

Source Map Upload Report
  Minified Scripts
    ~/index.android.bundle (sourcemap at index.android.bundle.map)
  Source Maps
    ~/index.android.bundle.map

> Task :app:bundleReleaseJsAndAssets_SentryUpload_4001
Processing react-native sourcemaps for Sentry upload.
> Analyzing 2 sources
> rewriting sources
> Adding source map references
Uploading sourcemaps for release com.plumpplum@RN62 Hermes+4001 distribution 4001
> Bundled 2 files for upload
> Uploaded release files to Sentry
> File upload complete

Source Map Upload Report
  Minified Scripts
    ~/index.android.bundle (sourcemap at index.android.bundle.map)
  Source Maps
    ~/index.android.bundle.map

FAILURE: Build Failed with an exception.

* Where:
Script 'C:\esites-grocery\test1\plumpclient\node_modules\react-native\react.gradle' line: 165

* What went wrong:
Execution Failed for task ':app:bundleReleaseJsAndAssets'.
> Process 'command 'cmd'' finished with non-zero exit value 1

解决方法

react.gradle的更多内容:

def getHermesCommand = {
    // If the project specifies a Hermes command,don't second guess it.
    if (!hermesCommand.contains("%OS-BIN%")) {
        return hermesCommand
    }

    // Execution on Windows fails with / as separator
    return hermesCommand
            .replaceAll("%OS-BIN%",getHermesOSBin())
            .replace('/' as char,File.separatorChar);
}

您要指定../../node_modules/hermesengine/win64-bin/hermes作为路径,该路径是特定于OS的(由没有OS占位符确定)。

根据注释,react.gradle因此不会再猜测您的路径,而只是将其直接传递给OS命令解释器。

此操作失败,因为您的命令解释器不允许/作为目录分隔符。

要么指定对您的系统有效的路径:

hermesCommand: "..\\..\\node_modules\\hermesengine\\win64-bin\\hermes",

或指定与操作系统无关的路径,以让react.gradle为每个平台对其进行转换:

hermesCommand: "../../node_modules/hermesengine/%OS-BIN%/hermes",