如何在 network_security_config.xml 中包含 BuildConfig 或资源

P K*_*ers 6 android localhost android-build-type android-network-security-config

为了在 Android 中的本地主机上进行 http-API 调用,我需要添加一个网络安全配置来定义我的本地 IP 地址。当然,每个开发者的本地IP地址都是不同的,所以我把这个值放进去,gradle.properties这样每个人都可以自己覆盖它。但是,我还没有找到如何将该值包含在文件中network-security-config.xml。我尝试创建 abuildConfigFieldresValuein build.gradle,但两者似乎都不起作用。我能做些什么?

网络安全配置.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:
        /sf/ask/3215860301/ -->
        <domain includeSubdomains="true">LOCALHOST</domain>
    </domain-config>
</network-security-config>
Run Code Online (Sandbox Code Playgroud)

应用程序/build.gradle

buildTypes {
    debug {
        buildConfigField "String", "LOCALHOST", localIpAddress
        resValue "string", "LOCALHOST", localIpAddress
    }
}
Run Code Online (Sandbox Code Playgroud)

gradle.properties

localIpAddress="IP_ADDRESS_HERE"
Run Code Online (Sandbox Code Playgroud)

network_security_config.xml也欢迎任何其他如何优雅地包含我的本地 IP 地址的解决方案!