简体   繁体   English

React Native:发布未连接到本地服务器的应用程序

[英]React Native: Release app not connecting to local server

I'm trying to build a release version of my app on android.我正在尝试在 android 上构建我的应用程序的发布版本。

Followed the necessary steps from creating a signed apk throughreact native docs .遵循从创建签名 apk 到react native docs的必要步骤。 Once I install the apk on my device, the initial page loads but I get no response when I try to do an action calling the api.在我的设备上安装 apk 后,初始页面会加载,但当我尝试执行调用 api 的操作时没有任何响应。

I've ran my local server and have connected my app to the same IP with the server I'm running on.我已经运行了我的本地服务器并将我的应用程序连接到与我正在运行的服务器相同的 IP。

The debug version of my app is doing well and I'm confused as to why this is happening.我的应用程序的调试版本运行良好,我很困惑为什么会发生这种情况。 I'm using axios for the calls and have set my url to something like this: http://192.168.12.24:8000/api/ .我正在使用 axios 进行调用,并将我的 url 设置为这样的: http://192.168.12.24:8000/api/ : http://192.168.12.24:8000/api/ :8000/api/。

Do you have ideas on what could have gone wrong?您对可能出现的问题有什么想法吗? Does react native not allow using local server in a release app? react native 是否不允许在发布应用中使用本地服务器?

I managed to fix the problem.我设法解决了这个问题。 It turns out I should run the bundle every time I generate a new apk.事实证明,每次我生成一个新的 apk 时,我都应该运行这个包。

This is what I did:这就是我所做的:

react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest andro id/app/src/main/res

cd android

./gradlew assembleRelease

Referenced from this answer .从此答案中引用。

Adding android:usesCleartextTraffic="true" in <application> tag in android\\app\\src\\main\\AndroidManifest.xml worked for me.android\\app\\src\\main\\AndroidManifest.xml中的<application>标记中添加android:usesCleartextTraffic="true"对我android:usesCleartextTraffic="true"

The reason is by default release build expects https url connections and it is possible that your server is having http based API like http://192.168.12.24:8000/api/ , so to avoid this filter, usesCleartextTraffic="true" makes http based api to be accessible from release build.原因是默认发布版本需要https url 连接,并且您的服务器可能具有基于http的 API,例如http://192.168.12.24:8000/api/ ,因此为了避免此过滤器,使用 CleartextTraffic="true" 使 http可从发布版本访问基于 api。

you can read more about here.你可以在这里阅读更多信息。 https://github.com/facebook/react-native/issues/24361 https://github.com/facebook/react-native/issues/24361

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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