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

我得到:

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

我使用的命令:

CD(项目目录) react-native开始 react-native运行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

其他回答

我在学习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。

我面临着这个问题,很多搜索,我解决了这个问题。

c:\ProjectFolder\ProjectName> 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:

c: \ ProjectFolder \ ProjectName > react-native运行android

我花了半天时间来解决这个问题。

如果你使用的是28.0.0以上的API目标版本,那么你可能会遇到这个问题。

只要加上这一行

android:usesCleartextTraffic="true"

在你的Manifest Application块中。

清单应用程序块代码。

 <application
        ....
        android:usesCleartextTraffic="true"
.../>

我花了几个小时试图解决这个问题。我的问题不是Windows特有的,而是Android特有的。

在本地和模拟器的浏览器中访问开发服务器。唯一不能工作的是在应用程序中访问开发服务器。

从Android 9.0 (API级别28)开始,默认情况下禁用明文支持。

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        ...
        android:usesCleartextTraffic="true"
        ...>
        ...
    </application>
</manifest>

查看更多信息: https://stackoverflow.com/a/50834600/1713216

确保你已经在path变量中添加了/path/to/sdk/platform-tools。

当你运行react-native run-android时,它会运行adb reverse tcp:< device-port > tcp:< local-port >命令将请求从你的设备转发到你计算机上本地运行的服务器。 如果没有找到adb,您将看到类似这样的内容。

/bin/sh: 1: adb: not found
Starting the app (.../platform-tools/adb shell am start -n 
com.first_app/com.first_app.MainActivity...
Starting: Intent { cmp=com.first_app/.MainActivity }