我试图在我的Nexus5 (android 5.1.1)上运行AwesomeProject。

我能够构建项目并将其安装到设备上。但当我运行它时,我看到一个红色屏幕,上面写着

无法下载JS包。您是否忘记启动开发服务器或连接设备?

在react原生iOS中,我可以选择离线加载jsbundle。我如何在Android上做同样的事情?(或者至少,我可以在哪里配置服务器地址?)

更新

要使用本地服务器运行,请在react-native项目根目录下运行以下命令

React-native start > /dev/null 2>&1 & Adb reverse tcp:8081 tcp:8081

请看disssitka的回答,了解更多细节。

要在没有服务器的情况下运行,可以运行以下命令将jsfile捆绑到apk中:

在android/app/src/main下创建一个assets文件夹 旋度“http://localhost: 8081 / index.android.bundle吗?平台=android" -o "android/app/src/main/assets/index.android.bundle"

详情请看kzzzf的回答。


当前回答

从项目目录中运行

react-native start

输出如下:

$ react-native start
 ┌────────────────────────────────────────────────────────────────────────────┐ 
 │  Running packager on port 8081.                                            │ 
 │                                                                            │ 
 │  Keep this packager running while developing on any JS projects. Feel      │ 
 │  free to close this tab and run your own packager instance if you          │ 
 │  prefer.                                                                   │ 
 │                                                                            │ 
 │  https://github.com/facebook/react-native                                  │ 
 │                                                                            │ 
 └────────────────────────────────────────────────────────────────────────────┘ 
Looking for JS files in
   /home/user/AwesomeProject 


React packager ready.

[11:30:10 PM] <START> Building Dependency Graph
[11:30:10 PM] <START> Crawling File System
[11:30:16 PM] <END>   Crawling File System (5869ms)
[11:30:16 PM] <START> Building in-memory fs for JavaScript
[11:30:17 PM] <END>   Building in-memory fs for JavaScript (852ms)
[11:30:17 PM] <START> Building in-memory fs for Assets
[11:30:17 PM] <END>   Building in-memory fs for Assets (838ms)
[11:30:17 PM] <START> Building Haste Map
[11:30:18 PM] <START> Building (deprecated) Asset Map
[11:30:18 PM] <END>   Building (deprecated) Asset Map (220ms)
[11:30:18 PM] <END>   Building Haste Map (759ms)
[11:30:18 PM] <END>   Building Dependency Graph (8319ms)

其他回答

一个更新

现在在windows上不需要运行react-native start。打包程序将自动运行。

这是我的解决方案

以上的方法都不能解决我的问题,只能按照下面的步骤来做。 添加network-security-config.xml文件

    <domain-config cleartextTrafficPermitted="true">
          <domain includeSubdomains="true">10.0.2.2</domain>
    </domain-config>
    ...

然后更新AndroidManifest.xml文件

<application
+   android:networkSecurityConfig="@xml/network_security_config"
    ...

嘿,我正面临这个问题与我的反应原生裸露项目,IOS模拟器工作良好,但android一直给我这个错误。这里有一个可能的解决方案,对我来说很有效。

步骤1,通过在您的终端上运行:adb devices检查adb设备是否获得授权

如果未经授权,则运行以下命令

步骤2,sudo adb kill-server 步骤3,sudo adb start-server . sh

然后检查adb devices命令是否显示 所附设备清单 模拟器- 5554设备

而不是未经授权,如果它是设备而不是未经授权

步骤4,执行NPX react-native run-android

这在我的例子中是有效的。

在停止react-native run-android进程后,我在Linux上得到了这个。似乎节点服务器仍在运行,所以下次你运行它时,它不会正常运行,你的应用程序无法连接。

这里的修复是杀死运行在Xterm中的节点进程,你可以通过ctrl- ctrl该窗口(更容易)杀死它,或者你可以使用lsof -n -i4TCP:8081然后杀死-9 PID找到它。

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

在新的React Native(当然是0.59)中,调试配置在android/app/src/debug/res/xml/react_native_config.xml中

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