How to include a BuildConfig or resource in network_security_config.xml
Asked Answered
M

0

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://mcmap.net/q/35675/-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!

Magnesia answered 6/9, 2019 at 12:51 Comment(2)
Did you figure this one out? Im in the same boat.Knighthood
Nope, I'm not even on that project anymore, so can't tell..Magnesia

© 2022 - 2024 — McMap. All rights reserved.