RN编译问题:Execution failed for task ':app:bundleStageReleaseJsAndAssets'.爬坑

问题log:
Successfully started process ‘command ‘node’’
internal/modules/cjs/loader.js:605
throw err;
^

Error: Cannot find module ‘/Users/xxxx/xxxx/xxxx/xxxx/node_modules/react-native/local-cli/cli.js’
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:603:15)
at Function.Module._load (internal/modules/cjs/loader.js:529:25)
at Function.Module.runMain (internal/modules/cjs/loader.js:775:12)
at startup (internal/bootstrap/node.js:300:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:826:3)
:app:bundleStageReleaseJsAndAssets FAILED
:app:bundleStageReleaseJsAndAssets (Thread[Task worker for ‘:’ Thread 3,5,main]) completed. Took 5.584 secs.

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ‘:app:bundleStageReleaseJsAndAssets’.
    Process ‘command ‘node’’ finished with non-zero exit value 1

主要原因是找不到node_modules/react-native/local-cli/cli.js,因为我的项目把JS代码,android原生,IOS原生代码分开了,所以默认的编译脚本就找不到node_modules了。

解决方法:
查看/node_modules/react-native/react.gradle文件,开头有段代码:


def config = project.hasProperty("react") ? project.react : [];

def cliPath = config.cliPath ?: "node_modules/react-native/local-cli/cli.js"
def bundleAssetName = config.bundleAssetName ?: "index.android.bundle"
def entryFile = config.entryFile ?: "index.android.js"
def bundleCommand = config.bundleCommand ?: "bundle"

其中cliPath就是问题的关键,用到他的代码在下面:

def currentBundleTask = tasks.create(
            name: "bundle${targetName}JsAndAssets",
            type: Exec) {
            group = "react"
            description = "bundle JS and assets for ${targetName}."
 ···省略···

            if (Os.isFamily(Os.FAMILY_WINDOWS)) {
                commandLine("cmd", "/c", *nodeExecutableAndArgs, cliPath, bundleCommand, "--platform", "android", "--dev", "${devEnabled}",
                    "--reset-cache", "--entry-file", entryFile, "--bundle-output", jsBundleFile, "--assets-dest", resourcesDir, *extraArgs)
            } else {
                commandLine(*nodeExecutableAndArgs, cliPath, bundleCommand, "--platform", "android", "--dev", "${devEnabled}",
                    "--reset-cache", "--entry-file", entryFile, "--bundle-output", jsBundleFile, "--assets-dest", resourcesDir, *extraArgs)
            }
···省略···
            }

可以看到,errorlog里报错的gradle任bundleStageReleaseJsAndAssets,就是源码中的任务bundle${targetName}JsAndAssets,所以设置正确的cliPath就可以解决了。

设置方法:
RN项目中的app的build.gradle中有一段
project.ext.react = [
entryFile: “index.js”,
]
在这里添加正确的cliPath就可以了,例如
project.ext.react = [
entryFile: “index.js”,
cliPath: “…/xxxx/node_modules/react-native/local-cli/cli.js”
]
谢谢阅读

这个错误通常是由于dex方法数超出限制引起的。你可以尝试以下方法解决: 1.app的build.gradle文件中添加下面的代码: ```groovy android { defaultConfig { ... multiDexEnabled true } ... } dependencies { implementation 'com.android.support:multidex:1.0.3' } ``` 然后在你的Application类中添加下面的代码: ```java public class MyApplication extends Application { @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } ... } ``` 2. 如果你使用了很多第三方库,可以尝试使用ProGuard来优化代码。在app的build.gradle文件中添加下面的代码: ```groovy android { ... buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } ``` 然后在app的根目录下创建一个proguard-rules.pro文件,添加下面的代码: ``` -dontwarn okio.** -dontwarn javax.annotation.** -dontwarn com.squareup.okhttp.** -dontwarn retrofit2.Platform$Java8 -dontwarn retrofit2.Platform$Java8$.* -dontwarn retrofit2.Platform$Java9 -dontwarn retrofit2.Platform$Java9$.* -keep class com.google.gson.** { *; } -keep class org.apache.commons.io.** { *; } -keep class retrofit2.** { *; } -keepattributes Signature -keepattributes Exceptions -keepattributes InnerClasses ``` 注意:ProGuard可能会导致某些库出现问题,因此需要进行适当的测试和调整。 3. 如果以上方法都无法解决问题,可以尝试升级Gradle版本或者在项目中使用AndroidX。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值