Google表示衅码,為保證用戶數(shù)據(jù)和設(shè)備的安全,針對下一代 Android 系統(tǒng)(Android P) 的應用程序脊岳,將要求默認使用加密連接,這意味著 Android P 將禁止 App 使用所有未加密的連接垛玻,因此運行 Android P 系統(tǒng)的安卓設(shè)備無論是接收或者發(fā)送流量割捅,未來都不能明碼傳輸,需要使用下一代(Transport Layer Security)傳輸層安全協(xié)議帚桩,而 Android Nougat 和 Oreo 則不受影響亿驾。
因此在Android P 使用HttpUrlConnection進行http請求會出現(xiàn)以下異常
W/System.err: java.io.IOException: Cleartext HTTP traffic to **** not permitted
使用OKHttp請求則出現(xiàn)
java.net.UnknownServiceException: CLEARTEXT communication ** not permitted by network security policy
在Android P系統(tǒng)的設(shè)備上,如果應用使用的是非加密的明文流量的http網(wǎng)絡請求账嚎,則會導致該應用無法進行網(wǎng)絡請求莫瞬,https則不會受影響,同樣地郭蕉,如果應用嵌套了webview疼邀,webview也只能使用https請求。
有人認為 Android P 上所有的 App 都需要使用 TLS 加密會降低上網(wǎng)體驗召锈,事實上這是一種誤解旁振,至于 App 對于少數(shù)舊服務器的連接如果非要使用明碼傳輸,開發(fā)者需要更改 App 的網(wǎng)絡安全配置以允許此類連接涨岁。
有以下三種解決方案
APP改用https請求
targetSdkVersion 降到27以下
在 res 下新增一個 xml 目錄拐袜,然后創(chuàng)建一個名為:network_security_config.xml 文件(名字自定) ,內(nèi)容如下梢薪,大概意思就是允許開啟http請求
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
然后在APP的AndroidManifest.xml文件下的application標簽增加以下屬性
<application
...
android:networkSecurityConfig="@xml/network_security_config"
...
/>
相關(guān)參考鏈接
https://android-developers.googleblog.com/2018/04/protecting-users-with-tls-by-default-in.html
https://android-developers.googleblog.com/2018/04/dns-over-tls-support-in-android-p.html