尝试在Android 4.4.2上创建一个反应原生项目,我得到了这个错误屏幕

却找不到解决的办法。我尝试重新启动packager,重新连接设备,甚至重新安装react native并开始新的项目。在6.0.0和更高版本上,它工作得很好。


当前回答

在设置中,转到隐私>本地网络,查看请求访问的每个应用程序的列表。 我通过允许我的应用程序访问本地网络来解决这个问题。 (试图从我的mac m1上运行在同一wifi的实体iPhone设备上)。

其他回答

对我来说,这是因为adb不在PATH中。它位于/Users/man/Library/Android/sdk/platform-tools,对我来说可能在其他地方,但无论如何,找到它并将其添加到你的路径,看看是否有帮助。

因为你使用的是Android < 5.0,所以你不能使用默认的adb反向方法,但是Facebook已经添加了官方文档来通过Wi-Fi连接到开发服务器,这将支持你的版本。引用MacOS的说明,但也有Linux和Windows的说明:

Method 2: Connect via Wi-Fi You can also connect to the development server over Wi-Fi. You'll first need to install the app on your device using a USB cable, but once that has been done you can debug wirelessly by following these instructions. You'll need your development machine's current IP address before proceeding. You can find the IP address in System Preferences → Network. Make sure your laptop and your phone are on the same Wi-Fi network. Open your React Native app on your device. You'll see a red screen with an error. This is OK. The following steps will fix that. Open the in-app Developer menu. Go to Dev Settings → Debug server host for device. Type in your machine's IP address and the port of the local dev server (e.g. 10.0.1.1:8081). Go back to the Developer menu and select Reload JS.

我最终不得不打开我正在使用的端口(默认为8081)。在Linux上,您可以执行以下操作

sudo iptables -A INPUT -p tcp --dport 8081 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
sudo iptables -A OUTPUT -p tcp --sport 8081 -m conntrack --ctstate ESTABLISHED -j ACCEPT

您可以进行测试,看看是否真的需要这样做。只需在Android设备上的Chrome浏览器中导航到开发服务器。如果它没有响应,那么这可能就是您所需要的。如果它有响应,那么这对你没有帮助。

对此,一个可能的解决方案是,您很可能不首先捆绑应用程序,而是执行以下步骤,然后将app-debug.apk部署到您的设备上

$ cd myproject  
$ react-native start > /dev/null 2>&1 &  
$ curl "http://localhost:8081/index.android.bundle?platform=android" -o "android/app/src/main/assets/index.android.bundle"

(如果文件夹资产不存在,创建它)

然后从项目根运行

$> (cd android/ && ./gradlew assembleDebug)

将创建的apk安装到您的设备,地址:android/app/build/outputs/apk/app-debug.apk

如果这解决了你的问题,请告诉我

编辑:

你可以把它放在你的包裹里。我相信它会在即将发布的react-native版本中被修复,并将在组装最终的APK之前执行,因此这将不需要(我希望也是如此)

把:

"scripts": {
    "build": "(cd android/ && ./gradlew assembleDebug)",
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "bundle-android": "react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/app/src/main/assets/index.android.bundle --sourcemap-output android/app/src/main/assets/index.android.map --assets-dest android/app/src/main/res/"
  },

或者如前所述的旋度等效,但对于大多数情况,上面的更健壮

我发现我还需要添加一个

react-native升级

使应用程序正确运行。