我试图在我的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目录运行npm start对我来说很有效。

其他回答

删除你手机上的App !我尝试了几个步骤,但最终还是成功了。

If you tried to run your app before but failed, delete it from your android device. Run $ react-native run-android Open the React Rage Shake Menu from within your app on your android device, go to Dev Settings and then to Debug server host & port for device. There enter your server IP (IP of your computer) and host 8081, e.g. 192.168.50.35:8081. On a mac you can find the IP of your computer at System Preferences -> Network -> Advanced... -> TCP/IP -> IPv4 Address. Open the Rage Shake Menu again and click Reload JS.

我没有足够的声誉来评论,但这是指dsissitka的答案。 它也可以在Windows 10上运行。

重申一下,这些命令是:

cd (App Dir)
react-native start > /dev/null 2>&1 &
adb reverse tcp:8081 tcp:8081

以下是在android上运行应用的简单步骤:

1. 转到应用程序根目录。

2. 执行该命令。

React-native bundle——平台android——dev false——入口文件 index.android.js——bundle-output android / app / src / main /资产/ index.android。包——assets-dest android / app / src / main / res

所有文件都已复制。

3.创建签名APK。

打开android studio。

生成签名APK >[填写所需细节,在此步骤之前必须生成击键]

生成的APK应该没有任何错误。安装在您的设备上,这应该工作没有任何问题。

你也可以连接你的设备,直接点击android studio上的Run图标进行测试。


生成按键:

去C:\Program Files\Java\jdkx.x.x_x\bin 运行

Keytool -genkey -v -keystore d:\my_private_key。密钥别名 RSA -keysize 2048 -validity 10000

它可能会要求你填写一些细节。这样做,你就有了你的击键文件(my_private_key.keystore),可以用来签署你的apk。

我只使用Ubuntu和Android,我无法让它运行。我发现了一个非常简单的解决方案,那就是更新你的包。Json文件,更改这行:

"scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start"
  },

to

"scripts": {
    "start": "adb reverse tcp:8081 tcp:8081 && node node_modules/react-native/local-cli/cli.js start"
  },

然后通过输入命令运行服务器

npm run start

这确保android设备在您的计算机上查找节点服务器,而不是在本地手机上。

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

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