在Android p中,默认情况下会禁用明文通信。相反,有两种选择:
- 需要明确声明允许在清单文件中使用
- 或者需要声明允许通过网络安全配置进行明文通信的允许域
我的问题与第二种方法有关。我可以在network_security_config.xml 中列出这样的特定ip地址
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">192.168.1.1</domain>
</domain-config>
</network-security-config>
然而,我想将所有私人ip地址列入白名单。我有一些试错案例,但我没能成功。
本质上,是否提供了在网络安全配置中定义范围ip地址的选项?
本以为您正在寻找一种允许特定范围的IP地址的方法,但这似乎是不可能的。现在,您可以允许所有IP地址,并通过以下配置消除错误消息:
config.xml
<platform name="android">
...
<edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android">
<application android:networkSecurityConfig="@xml/network_security_config" />
</edit-config>
<resource-file src="resources/android/xml/network_security_config.xml" target="app/src/main/res/xml/network_security_config.xml" />
...
</platform>
resources/android/xml/network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
不,对不起。
事实上,我怀疑支持<domain includeSubdomains="true">192.168.1.1</domain>
是偶然的,随着时间的推移可能不会被证明是可靠的,如果他们开始认为<domain>
指的是实际的域名,而不是任意的主机值,例如IP地址。
为了获得机器的IP,有一个解决方案
这是我在res/xml:上的网络配置文件
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">
<!-- YOUR LOCAL IP -->
</domain>
</domain-config>
</network-security-config>
这是我年级的代码:
static def getLocalIP() {
def ip4s = []
NetworkInterface.getNetworkInterfaces()
.findAll { it.isUp() && !it.isLoopback() && !it.isVirtual() }
.each {
if (it.name.startsWith("wlan")) {
it.getInetAddresses()
.findAll { !it.isLoopbackAddress() && it instanceof Inet4Address }
.each { ip4s << it }
}
}
return ip4s.first().toString().substring(1)
}
task ipNetwork(type: Copy) {
from ('src/main/res/xml/network_security_config.xml')
into ('src/debug/res/xml')
filter {
String line -> line.replaceAll("<!-- YOUR LOCAL IP -->", getLocalIP())
}
}
这只会更改调试文件,因此将调试应用程序指向本地计算机非常有用。该脚本可以适用于为IP的每个变体生成一组域标签。这是我的第一个想法,为每192.168..添加一个域,但这会导致一个文件有65536个域,这似乎有点糟糕