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

我得到:

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

我使用的命令:

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


当前回答

Ubuntu

第一次,我用react-native init project-name创建了新的应用程序。 我得到了同样的错误。 所以在我的例子中,我执行以下步骤来解决这个问题。

首先执行sudo chown user-name-of-pc /dev/kvm。 从Android手机调试时,选择“使用USB传输照片(PTP)”。 在project-name/android/app/src/main中创建文件夹资产 确保index.js在你的项目根目录下可用,然后从控制台在CD项目名称目录后运行下面的命令。

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

或 对于index.android.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

在“android-studio/bin”目录下执行。/studio.sh命令。它将开放Android Studio。 执行命令react-native Run -android。

其他回答

IOS:

在终端中:

cd ios

使用:rm -r build删除build文件夹

再次运行:react-native Run -ios

或者,你可以打开Finder,导航到YOUR_PROJECT/ios并删除构建文件夹。

然后再次运行:react-native run-ios

为ANDROID:

在终端中:

cd android/app

使用:rm -r build删除build文件夹

再次运行:react-native Run -android

或者,你可以打开Finder,导航到YOUR_PROJECT/android/app并删除构建文件夹。

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

1进入你的项目目录,检查这个文件夹是否存在android/app/src/main/assets

如果它存在,那么删除两个文件即index.android.bundle和index.android.bundle.meta 如果文件夹资产不存在,则在那里创建资产目录。

2.从您的根项目目录做

cd android && ./gradlew clean

3.最后,导航回根目录,检查是否有一个名为ledindex.js的条目文件

If there is only one file i.e. index.js then run following command 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 If there are two files i.e index.android.js and index.ios.js then run this react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res Now run react-native run-android

对我来说,gradle清洁修复了它,当我在Android上有问题

cd android

./gradlew clean

在MAC或Windows上,我们可以在1秒内解决这个问题

在你的项目中搜索project.ext.react这个术语

并在bundleInDebug: true中做一个更改,添加这一行。并再次运行您的项目。

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

我在真正的android设备上也遇到过同样的问题。

解决方案:基于Niltoid的回答

查找本地IP 打开应用程序,摇晃你的安卓设备 去开发设置和>调试服务器… 粘贴你的IP和端口;X.X.X.X:8088"(其中X是您的本地IP)

Mac用户:打开你的网络首选项,在这里你会得到你的本地IP。