Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: network security config only in debug version

I'm working on an Android app. For development purpose, I need to specify a network security config as specified in the official documentation. Obviously, it is referenced in the AndroidManifest.xml.

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">aaa.bb.cc.dd</domain>
    </domain-config>
</network-security-config>

Now, I need to remove this file (and its reference in the manifest too) in the release version of my app. How can I achieve this? Thank you in advance.

like image 865
xcesco Avatar asked Dec 18 '22 14:12

xcesco


2 Answers

Even better than duplicating the AndroidManifest.xml, is to create an extra xml file in the same on the app/src/debug path and Android will automatically recognise that one during a debug build.

So, if you currently have a network policy xml file located on:

app/src/main/res/xml/network_policy.xml

You would simply create another xml resource directory under app/src/debug and then another network_policy.xml file within that directory:

app/src/debug/res/xml/network_policy.xml

And then you just write the network policy for each build variant accordingly.

like image 86
luizParreira Avatar answered Dec 21 '22 11:12

luizParreira


You simply create another AndroidManifest.xml and put in in the debug folder structure, something like app/src/debug, so this manifest property will be used in debug build, but the real manifest will be used in prod build.

like image 42
Samir Spahic Avatar answered Dec 21 '22 09:12

Samir Spahic