今天公司之前的項(xiàng)目適配Android 9.0(P) ,進(jìn)入到登錄界面,輸入賬號(hào)密碼,報(bào)錯(cuò),內(nèi)容為:
CLEARTEXT communication to host(host主機(jī)地址) not permitted by network.
翻譯:網(wǎng)絡(luò)安全策略不允許與host(host主機(jī)地址)進(jìn)行明文通信。
查看報(bào)錯(cuò)異常來源

發(fā)現(xiàn)是OkHttp3做網(wǎng)絡(luò)請(qǐng)求框架時(shí),如果是http請(qǐng)求而非https請(qǐng)求,會(huì)導(dǎo)致請(qǐng)求失敗。
此項(xiàng)目使用的就是OkHttp3做網(wǎng)絡(luò)請(qǐng)求框架,并且是Http請(qǐng)求?,F(xiàn)在App應(yīng)該都是使用的是OkHttp3網(wǎng)絡(luò)請(qǐng)求框架吧,哎呀,咱也不知道,咱也不敢問呀。
查看OkHttp3的源碼可以看到,它做了網(wǎng)絡(luò)請(qǐng)求的檢查。
if (!Platform.get().isCleartextTrafficPermitted(host)) {
throw new RouteException(new UnknownServiceException(
"CLEARTEXT communication to " + host + " not permitted by network security policy"));
}
如果請(qǐng)求是明文流量,默認(rèn)情況下,在Android 9.0(P)版本Okhttp3就會(huì)拋出異常:
throw new RouteException(new UnknownServiceException(
"CLEARTEXT communication to " + host + " not permitted by network security policy"));
因?yàn)槭沁m配Android 9.0(P),(并且之前App正常運(yùn)行)所以直接去官網(wǎng)查看關(guān)于Android 9.0(P)的新特性,發(fā)現(xiàn)Android 9.0(P)系統(tǒng)默認(rèn)禁止http協(xié)議,即禁止明文傳輸,必須使用https來通訊。找到關(guān)于Android 9.0(P)的網(wǎng)絡(luò)安全配置文章。
閱讀完畢
現(xiàn)在開始重新解決問題,
問題如下:
發(fā)起網(wǎng)絡(luò)請(qǐng)求時(shí)App打印錯(cuò)誤信息:CLEARTEXT communication to host(host主機(jī)地址) not permitted by network
解決方案:(因?yàn)轫?xiàng)目著急上線)現(xiàn)在必須得禁用掉明文流量請(qǐng)求的檢查。
在res下新建xml文件夾,然后創(chuàng)建一個(gè)name.xml的文件(name可自由命名),文件內(nèi)容如下:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
接著在清單文件中的application節(jié)點(diǎn)下添加代碼:android:networkSecurityConfig="@xml/name.xml"(上述新建文件的名字)。此處名字為network_config_sdjy_banker,結(jié)構(gòu)如下:
<application
...
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_config_sdjy_banker"
...>
重新編譯運(yùn)行,報(bào)錯(cuò)。
MMP又報(bào)錯(cuò),自己明明那么那么查看官網(wǎng)文檔的。
算了,還是先看一下報(bào)錯(cuò)信息(此時(shí)如果項(xiàng)目本沒有引入關(guān)于修改網(wǎng)絡(luò)安全配置的庫時(shí),加入上述代碼,就已經(jīng)可以禁用掉明文流量請(qǐng)求的檢查了,使用Http協(xié)議的App就可以正常使用了。)
...l4_app\src\main\AndroidManifest.xml:47:9-64 Error:
Attribute application@networkSecurityConfig value=(@xml/network_config_sdjy_banker) from AndroidManifest.xml:47:9-64
is also present at [com.baijiayun.live:liveplayer-sdk-core:2.0.0-rc01] AndroidManifest.xml:18:18-79 value=(@xml/network_config_baijiayun).
Suggestion: add 'tools:replace="android:networkSecurityConfig"' to <application> element at AndroidManifest.xml:42:5-441:19 to override.
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':l4_app:processDebugManifest'.
> Manifest merger failed : Attribute application@networkSecurityConfig value=(@xml/network_config_sdjy_banker) from AndroidManifest.xml:47:9-64
is also present at [com.baijiayun.live:liveplayer-sdk-core:2.0.0-rc01] AndroidManifest.xml:18:18-79 value=(@xml/network_config_baijiayun).
Suggestion: add 'tools:replace="android:networkSecurityConfig"' to <application> element at AndroidManifest.xml:42:5-441:19 to override.
報(bào)錯(cuò)信息理解就是此App項(xiàng)目中引入的關(guān)于百家云的庫文件,清單文件配置項(xiàng)與剛才在主module(l4_app)的配置項(xiàng)有沖突。
依據(jù)報(bào)錯(cuò)信息和AS建議
Suggestion: add 'tools:replace="android:networkSecurityConfig"' to <application> element at AndroidManifest.xml:42:5-441:19 to override.
修改AndroidManifest.xml文件,覆蓋掉百家云清單文件配置項(xiàng)。
<application
...
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_config_sdjy_banker"
...
tools:replace="android:networkSecurityConfig">
重新運(yùn)行App,完美運(yùn)行。
這樣的操作是使得App不使用Https請(qǐng)求,使用Http明文的網(wǎng)絡(luò)請(qǐng)求。
但是呢,推薦建議后臺(tái)服務(wù)器和App都改用Https,畢竟Android 9.0(P)系統(tǒng)默認(rèn)禁止http協(xié)議,咱們也要與時(shí)俱進(jìn)呢。