简体   繁体   English

如何在 Android 手机上运行 React Native 应用程序

[英]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.它看起来像这样。

华为 P8 lite 崩溃

If you are connected via cable, do the following:如果通过电缆连接,请执行以下操作:

1. Goto > Settings > About Device 1.转到>设置>关于设备

2. Then Software Info 2. 然后是软件信息

3. Then > Build Number 3.然后>内部版本号

4. Now Tap (Click) multiple times on Build Number to Enable Developer Options 4. 现在在版本上多次点击(单击)以启用开发人员选项

5. Here you go not the Developer Options will be visible in your Settings 5. 在这里,您的设置中不会显示开发人员选项

6. Now Go inside the Developer Options and Enable USB Debugging Mode. 6.现在进入开发人员选项并启用USB调试模式。

7. Open your terminal 7. 打开你的终端

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.这将显示您计算机上的设备和模拟器/虚拟设备。

8. Forward requests from your device 8. 从您的设备转发请求

Type:类型:

$ adb reverse tcp:8081 tcp:8081

9. Run it 9.运行它

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 代码:-

  1. Please make sure you are on the same wifi network(Mobile and Laptop).请确保您在同一个 wifi 网络(手机和笔记本电脑)上。
  2. Run your code and install it on the mobile through the Android Studio.运行您的代码并通过 Android Studio 将其安装在移动设备上。
  3. The app will install and asking you to change your "dev settings".该应用程序将安装并要求您更改“开发设置”。
  4. Shake your phone and go to "dev settings" and type your machine's IP address(192.16.XX.XXX:8081).摇晃你的手机,进入“开发设置”,输入你机器的IP地址(192.16.XX.XXX:8081)。
  5. Close the app once and open again, in the terminal you will see like this关闭应用程序并再次打开,在终端中你会看到这样在此处输入图片说明

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 连接到本地开发服务器

  • Make sure your laptop and your phone are on the same Wi-Fi network.确保您的笔记本电脑和手机在同一个 Wi-Fi 网络中。
  • Open your React Native app on your device.在您的设备上打开您的 React Native 应用程序。 You can do this the same你也可以这样做
  • way you'd open any other app.你打开任何其他应用程序的方式。 You'll see a red screen with an error.您会看到一个带有错误的红色屏幕。
  • This is OK.还行吧。 The following steps will fix that.以下步骤将解决该问题。 Open the Developer打开开发者
  • Go to Dev Settings.转到开发设置。 Go to Debug server host for device.转到设备的调试服务器主机。 Type in your machine's IP address and the port of the local dev server (eg 10.0.1.1:8081).输入您机器的 IP 地址和本地开发服务器的端口(例如 10.0.1.1:8081)。
  • On Mac, you can find the IP address in System Preferences / Network.在 Mac 上,您可以在系统偏好设置/网络中找到 IP 地址。 On Windows, open the command prompt and type ipconfig to find your machine's IP address (more info).在 Windows 上,打开命令提示符并键入 ipconfig 以查找您机器的 IP 地址(更多信息)。
  • Go back to the Developer menu and select Reload JS.返回开发人员菜单并选择重新加载 JS。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM