6

To make http-API calls on localhost in Android I needed to add a network-security-config to define my local IP address. Of course, the local IP address is different for each developer, so I put this value in gradle.properties so everyone can override this themselves. However, I haven't found out how to include that value in the network-security-config.xml file. I tried it by creating a buildConfigField or resValue in build.gradle, but both don't seem to work. What can I do?

network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <!-- We are allowed to call localhost on http instead of https:
        https://stackoverflow.com/questions/45940861/android-8-cleartext-http-traffic-not-permitted -->
        <domain includeSubdomains="true">LOCALHOST</domain>
    </domain-config>
</network-security-config>

app/build.gradle

buildTypes {
    debug {
        buildConfigField "String", "LOCALHOST", localIpAddress
        resValue "string", "LOCALHOST", localIpAddress
    }
}

gradle.properties

localIpAddress="IP_ADDRESS_HERE"

Any other solutions how to gracefully include my local IP address in network_security_config.xml are welcome too!

P Kuijpers
  • 1,593
  • 15
  • 27

0 Answers0