Search code examples
androiddryandroid-network-security-config

How to use a resource value in the network security configuration file?


I have a Network Security Configuration file that looks like this:

<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">123.123.123.123</domain>
    </domain-config>
</network-security-config>

In code a have a variable that contains also this ip. So if in the future i have to change the ip, I must remember to change it in two different places, so this doesn't respect the DRY principle.

How can I reference a value in the Network Security Configuration file?

I tried with <domain includeSubdomains="true">@string/my_ip</domain> but it didn't work.


Solution

  • How can I reference a value in the Network Security Configuration file?

    That is not supported by the build tools directly. You could create your own script or Gradle task to generate the network security configuration file from some template, where you perform some substitution of a placeholder value with the actual IP address/domain name.