[英]How to run React Native app on Android Phone
I am building React Native app.我正在构建 React Native 应用程序。
It is working well when I launch the app in terminal on Mac using "react-native run-android".当我使用“react-native run-android”在 Mac 上的终端中启动应用程序时,它运行良好。
But when I got the apk file and installed it on another android device manually, it does not work.但是当我拿到 apk 文件并手动安装到另一个安卓设备上时,它不起作用。
It looks like this.它看起来像这样。
If you are connected via cable, do the following:如果通过电缆连接,请执行以下操作:
On Windows open Android SDK Manger > Platform Tools在 Windows 上打开Android SDK Manger > Platform Tools
Type:类型:
$ adb devices
This will show you the devices and simulator/virtual-devices that you have on you computer.这将显示您计算机上的设备和模拟器/虚拟设备。
Type:类型:
$ adb reverse tcp:8081 tcp:8081
Type:类型:
$ npm run android
The app should appear on your device该应用程序应出现在您的设备上
Looks like the source code in your APK is looking for the package server.看起来您的 APK 中的源代码正在寻找包服务器。
Read this on how to build APKs for react-native: React-Native - Generating Signed APK阅读有关如何为 react-native 构建 APK 的文章: React-Native - Generating Signed APK
If your devices is connected via cable:如果您的设备通过电缆连接:
- If you're on a physical device connected to the same machine, run 'adb reverse tcp:8081 tcp:8081' to forward requests from your device如果您在连接到同一台机器的物理设备上,请运行“adb reverse tcp:8081 tcp:8081”以转发来自您设备的请求
Otherwise, you can still do this via Wifi by following the last point in the error:否则,您仍然可以按照错误中的最后一点通过 Wifi 执行此操作:
- If your device is on the same Wi-Fi network, set 'Debug server host & port for device' in 'Dev settings' to your machine's IP address and the port of the local dev server -eg 10.0.1.1:8081如果您的设备在同一个 Wi-Fi 网络上,请在“开发设置”中将“设备调试服务器主机和端口”设置为您机器的 IP 地址和本地开发服务器的端口 - 例如 10.0.1.1:8081
Please find the below steps to run react-native code on a physical mobile device:-请找到以下步骤以在物理移动设备上运行 react-native 代码:-
6. wait for a minute app will install and reflect the changes. 6. 稍等片刻,应用程序将安装并反映更改。
Get a list of all the devices:获取所有设备的列表:
adb devices
Then set the which device to run on:然后设置要在哪个设备上运行:
adb -s <device name> reverse tcp:8081 tcp:8081
Then deploy the app:然后部署应用程序:
react-native run-android
If you've upgraded your version of react-native since generating your android project files, you might want to regenerate those now.如果您在生成 android 项目文件后升级了 react-native 版本,您现在可能想要重新生成这些文件。 I think you can use react-native upgrade
.我认为您可以使用react-native upgrade
。
The following are required in order to run a React Native app on Android:在 Android 上运行 React Native 应用程序需要以下内容:
Android Studio Android SDK JDK 8 (installation instructions for macOS here and Windows here) Android Studio Android SDK JDK 8(此处为 macOS 和此处为 Windows 的安装说明)
Answer Source: Make An App答案来源: 制作应用
We can run the React Native app on Android platform by running the following code in the terminal.我们可以通过在终端中运行以下代码在 Android 平台上运行 React Native 应用程序。
react-native run-android Before you can run your app on Android device, you need to enable USB Debugging inside the Developer Options. react-native run-android 在您可以在 Android 设备上运行您的应用程序之前,您需要在开发人员选项中启用 USB 调试。
If you get a “bridge configuration isn't available” error.如果您收到“网桥配置不可用”错误。 Then use below command to solve adb -s tcp:8081 tcp:8081然后使用下面的命令来解决 adb -s tcp:8081 tcp:8081
Now re-run the app using现在使用重新运行应用程序
react-native run-android react-native run-android
The command for correcting networking on the physical device should instead be: adb -s reverse tcp:8081 tcp:8081在物理设备上更正网络的命令应该是: adb -s reverse tcp:8081 tcp:8081
When USB Debugging is enabled, you can plug in your device and run the code snippet given above.启用 USB 调试后,您可以插入设备并运行上面给出的代码片段。
Configure your app to connect to the local dev server via Wi-Fi配置您的应用程序以通过 Wi-Fi 连接到本地开发服务器
The Native Android emulator is slow.本机 Android 模拟器很慢。 We recommend downloading Genymotion for testing your app.我们建议下载 Genymotion 来测试您的应用程序。
The developer menu can be accessed by pressing command + M.可以通过按 command + M 访问开发人员菜单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.