我试图在我的设备上第一次运行我的第一个React Native项目(Android 4.2.2)。

我得到:

无法从assets index.android.bundle中加载脚本

我使用的命令:

CD(项目目录) react-native开始 react-native运行android


当前回答

实际上,在我的情况下(React-native版本0.61.5,并试图在设备上安装调试APK)没有一个答案帮助我,我的解决方案是添加bundleInDebug: true到android/app/build。这样Gradle:

project.ext.react = [
    entryFile: "index.js",
    enableHermes: true,  // clean and rebuild if changing
    bundleInDebug: true
]

其他回答

我在学习React Native教程时也遇到过同样的问题(在Linux上开发,目标是Android)。

这个问题帮助我通过以下步骤解决了问题。

(在项目目录)mkdir android/app/src/main/assets React-native bundle——platform android——dev false——entry-file index.js——bundle-output android/app/src/main/assets/index.android。Bundle——assets-dest android/app/src/main/res react-native运行android

您可以通过将上述步骤放在包的脚本部分来自动化它们。Json是这样的:

"android-linux": "react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res && react-native run-android"

然后你可以每次都从你的命令行执行npm run android-linux。

这可能是由React Native项目代码库中未链接的资产引起的,例如,当您重命名项目应用程序/包id或添加外部包时没有正确链接。

在你的项目根目录下试试:

react-native link

react-native run-android

照着做,对我有用。

https://github.com/react-community/lottie-react-native/issues/269

进入你的项目目录,检查这个文件夹是否存在android/app/src/main/assets i)如果它存在,那么删除两个文件即index.android.bundle和index.android.bundle.meta ii)如果assets文件夹不存在,那么在那里创建assets目录。

从您的根项目目录做 CD android && ./gradlew clean

最后,导航回根目录,检查是否有一个名为index.js的条目文件 i)如果只有一个文件,即index.js,则执行以下命令 React-native bundle——platform android——dev false——entry-file index.js——bundle-output android/app/src/main/assets/index.android。Bundle——assets-dest android/app/src/main/res

ii)如果有两个文件,即index.android.js和index.ios.js,那么运行这个 React-native bundle——platform android——dev false——entry-file index.android.js——bundle-output android/app/src/main/assets/index.android. jsBundle——assets-dest android/app/src/main/res

现在运行react-native run-android

操作系统:Windows

在尝试以上两种方法后,另一种方法可以解决这个问题 (因为我安装的SDK不在默认位置,如C:\users\"user_name"\appdata\local\ SDK)

如果您注意到命令行显示:“‘adb’未被识别为内部或外部命令……”

这就是我克服的方法: 示例:我在D:\Android\ SDK中安装了SDK 所以我将在系统变量中插入path 2行:

D:\Android\sdk\tools  
D:\Android\sdk\platform-tools  

(如果你不知道如何编辑路径系统变量,这里是主题:https://android.stackexchange.com/questions/38321/what-do-i-type-in-path-variable-for-adb-server-to-start-from-cmd/38324)

然后再次运行cmd: react-native run-android

这对我很管用。

您可以根据官方页面的提示进行修复。这个问题发生在真实设备上,因为JS包位于您的开发系统上,而真实设备中的应用程序不知道它的位置。