6

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

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>

应用程序/build.gradle

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

gradle.properties

localIpAddress="IP_ADDRESS_HERE"

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

4

0 回答 0