我试图在我的设备上第一次运行我的第一个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

其他回答

如果你在你的主AndroidManifest.xml中定义了android:networkSecurityConfig,这个问题也会发生。android:usesCleartextTraffic="true"在你的调试AndroidManifest.xml将被忽略。

要解决这个问题,你必须配置应用程序允许http流量到localhost。

创建一个文件android/src/debug/res/xml/network_security_config.xml:

<?xml version="1.0" encoding="utf-8" ?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">localhost</domain>
    </domain-config>
</network-security-config>

并在android/src/debug/AndroidManifest.xml中引用它:

<?xml version="1.0" encoding="utf-8" ?>
<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

    <application 
        android:networkSecurityConfig="@xml/network_security_config" 
        tools:targetApi="28" 
        tools:ignore="GoogleAppIndexingWarning" />
</manifest>

更多信息请访问:https://medium.com/astrocoders/i-upgraded-to-android-p-and-my-react-native-wont-connect-to-my-computer-to-download-index-delta-42580377e1d3

操作系统: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

这对我很管用。

对于所有从创建-反应-原生应用程序开发出来的人,在开发过程中遇到这个问题, 这对我来说很管用 这是公认答案中的一个小细节

(在项目目录)mkdir android/app/src/main/assets

下面是需要改变的部分,因为你是在开发中,去掉——dev: false部分:

React-native bundle——platform android——entry-file index.js——bundle-output android / app / src / main /资产/ index.android。包——assets-dest android / app / src / main / res

在关闭所有终端后,从android/app/删除构建文件夹,以确保你开始干净地构建你的应用程序,打开一个新的终端,然后

(在项目目录)NPM运行android

是否会提示打包器控制台(在另一个终端)并成功构建

我也面临着这个问题,因为当我在模拟器上运行项目时,它的工作很好,但在真实的设备上,它给出了这个错误。所以我用下面的方法来解决这个问题

第一步:首先打开cmd,进入SDK管理器Platform-tools文件夹

cd C:Development\Android\Sdk\Platform-tools

第二步:现在运行这个命令:

adb devices

执行此命令后,检查命令提示符中列出的设备

第三步:现在运行这个

adb reverse tcp:8081 tcp:8081

现在您的设置完成了

第四步:进入您的项目目录并运行此命令

react-native run-android

首先检查你的assets文件夹是否存在于这个位置\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

然后在android设备上使用

react-native run-android