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


当前回答

一个更新

现在在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"
    ...

一个更新

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

好吧,我想我知道问题出在哪里了。这和我当时扮演的守望者有关。

在一个新的shell中,运行brew update 然后:酿解守夜人 然后:酿造安装守望者

现在,您可以从项目文件夹中运行react-native start

我让这个shell打开,创建一个新的shell窗口并从我的项目文件夹中运行:react-native run-android。世界一切都好。

ps.我最初玩的是3.2版的守望者。这将我升级到3.7。

pp。我是新手,所以这可能不是最快的解决方法,但对我来说很有效。

*在设备上运行/调试的更多信息

你可能会发现,如果你将应用部署到Android设备上,而不是模拟器上,你会看到一个红色的死机屏幕,错误提示无法加载JS Bundle。您需要为您的设备设置调试服务器为您的计算机运行react…名称或IP地址。

按设备菜单按钮 选择Dev Settings 为设备选择调试服务器主机或更改包位置 输入你的机器的IP和重载JS加上反应端口,例如192.168.1.10:8081

更多信息:http://facebook.github.io/react-native/docs/running-on-device-android.html

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

    //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

当你的服务器运行(react-native start)时,将JS文件捆绑到你的apk中下载到你的应用程序的assets目录:

curl "http://localhost:8081/index.android.bundle?platform=android" -o "android/app/src/main/assets/index.android.bundle"

在下一个版本(0.12)中,我们将修复react-native bundle命令,以与预期的android项目一起工作。