我试图在我的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的回答。


当前回答

在android上的应用程序中,我打开菜单(Genymotion中的命令+ M) -> Dev Settings ->设备的调试服务器主机和端口

设置为:localhost:8081

这对我很管用。

其他回答

在新的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>

我发现这很奇怪,但我有一个解决办法。 我注意到每隔一段时间我的项目文件夹变成只读,我不能从VS.中保存它,所以我读了一个建议,将NPM从本地用户PATH转移到系统范围的PATH全局变量,它就像一个咒语。

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

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

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

在尝试了这些答案的组合后,这是对我有用的。

我使用Genymotion作为模拟器。

1启动Genymotion模拟器。

2从终端

cd AwesomeProject
react-native run-android # it loads in genymotion and fails with bundle error
react-native start > /dev/null 2>&1 & # from dsissitka
adb reverse tcp:8081 tcp:8081 # from dsissitka

从模拟器重新加载。

它加载!

我现在可以开始显影了。

此错误可以通过以下步骤轻松解决:

    //run the command at /android sdk / platforms-tool folder
        adb reverse tcp:8081 tcp:8081

    //now come to root folder of your app
    1. npm start
    2. create 'assets' folder inside rootApp/android/app/src/main/
    3. curl "http://localhost:8081/index.android.bundle?platform=android" -o "android/app/src/main/assets/index.android.bundle"
    4. make sure anyone physical or virtual device attached is running.
    5. react-native run-android