Whenever I run my RN-App via npx react-native run-android
the fetch requests are working perfectly fine. However, once I create a release build via
npx react-native run-android --variant=release
,
my App returns the error [TypeError: Network request failed]
whenever I try to fetch anything.
My fetch method looks like this:
try{
const res = await fetch('http://192.168.1.10:4000/api/');
const json = await res.json();
}catch(err){
console.log(err)
}
As others mentioned in similar questions, I have already tried adding
<application ... android:usesCleartextTraffic="true">...</application>
to AndroidManifest.xml
in android/app/src/main/
I also figured it might have something to do with Android apps not being allowed to run any requests without ssl in release mode, so I added a self signed SSL cert to my nodejs backend and tried again using https, without any success.
Any help is greatly appreciated since it's my first time doing this!
CodePudding user response:
Try with network_security_config.xml file
create a file network_security_config.xml
in res/xml
folder and add this code in the file.
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">192.168.1.10</domain>
</domain-config>
</network-security-config>
then add this line in your AndroidManifest.xml
file
<application
...
android:usesCleartextTraffic="true"
android:networkSecurityConfig="@xml/network_security_config"
... >
Hope this works for you!
CodePudding user response:
As it turns out, the error lied in me not running the bundler when I changed something. Turns out you have to run it every time you make changes and want them to occur in the release build so I just ran the bundler with
npx react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res
and then created the release build via
npx react-native run-android --variant=release
and now it runs perfectly fine.