最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - React Native ERROR after editing "AndroidManifest.xml" - Stack Overflow

programmeradmin0浏览0评论

straight to the point. I created a new react native app in windows using:

npx @react-native-community/cli init exampleApp

I then run:

npx react-native run-android

Everything loads up OK. Metro runs fine, Gradlew builds the app OK and the emulator loads fine allowing me to edit the app in real time. Everything works fine.

THE PROBLEM:

The react native app connects OK to a LAN server using HTTP or WebSockets (ws). I need the app to connect using those protocols over TLS, i.e. https or wss.

I've read that Android by default blocks self-signed SSL certificates and non-secure connections like http:// and ws://.

So, I read a bit more online and found something on Android docs. Also, this Stack Overflow question looks relevant.

The thing is, when I do what those two resources mention I need to do so that my app connects to the server, I'm getting the following error:

Unable to load script. Make sure youre either running Metro (run "npx react-native start")
   or that your bundle "index.android.bundle" is packaged correctly for release.

MY CONFIGURATION:

I created a new folder at the following location and copied/pasted the root CA certificate that I created using openssl. This certificate signed the certificate that the LAN server is using: exampleApp/android/app/src/main/res/raw/test_rootca_certificate.crt

I created a file called "network_security_config.xml" at the following location: exampleApp/android/app/src/main/res/xml/network_security_config.xml

and this is the content:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="false">
        <trust-anchors>
            <certificates src="@raw/test_rootca_certificate"/>
            <certificates src="system"/>
        </trust-anchors>
    </base-config>
 </network-security-config>

Finally I added the following line inside the application element tag in the "AndroidManifest.xml" file: android:networkSecurityConfig="@xml/network_security_config"

CONCLUSIONS:

I already tried running Metro bundler first in a separate terminal and then when Metro loads, I proceed to build the app using npx react-native run-android, but I get the same error.

If I remove the following line from AndroidManifest.xml, the error is gone:

android:networkSecurityConfig="@xml/network_security_config"

I already tried doing the following but the same error appears:

adb reverse tcp:8081 tcp:80181

I tried clearing the gradlew cache, and restarting adb server but the same error appears.

What could be the issue here? Is there some Android-specific configuration I'm missing? The app works fine in HTTP, but since I need HTTPS I'm following these network configurations.

Any help will do, thank you!

发布评论

评论列表(0)

  1. 暂无评论