(gradle4.1)ReactNative assemblRelease,运行提示Unable to load script from assets ‘index.android.bundle‘.

公司RN项目添加Flavors风味打包后,打release包,报错Unable to load script from assets ‘index.android.bundle’.

首先解压apk发现asset下确实没有 index.android.bundle,但是构建文件夹output-generated文件夹里又生成了这个文件。

然后google了一下,找到了相关问题答案。
https://github.com/facebook/react-native/pull/30177
https://github.com/facebook/react-native/issues/29398

简单来说,就是因为gradle 6.5+ 和gradle plugin 4.1+后,引入了打包的一个问题。mergeResources任务运行在了currentAssetsCopyTask任务之前。导致打包index.android.bundle没有打包到apk里。

解决方法很简单,assembleRelease执行2次,第二次生成的包就能包含index.android.bundle文件了。

另外一个可能的解决办法,就是参考答案的,

//Fix for bundle not getting included in APK or AAB with android gradle plugin 4+
//Seems like there's a task ordering issue and the assets get computed before the bundle is copied
//This forces the copy task to run before merge resources
project.afterEvaluate {
        tasks.findAll { task ->
            task.name.startsWith('merge') && task.name.endsWith('Resources')
        }.each { t -> t.dependsOn "copyReleaseBundledJs" }
}

添加到android/app/build.gradle中。在mergexxxxResources命令后,再执行copyReleaseBundleJs任务。

这个方法没有验证。大家可以试一下。

最简单的办法还是assembleRelease两次。然后等后面的RN版本修复这个bug吧。

posted @ 2022-06-17 14:19  forlllearning  阅读(35)  评论(0)    收藏  举报  来源