適配Bugly不支持Android P

Bugly官方文檔上最高是適配的8.x,剛開始的時候我拿Android P進行測試也是無效果.後來發現log中有一句Cleartext HTTP traffic to android.bugly.qq.com not permitted. 拿起就是一陣Google,發現原來是Android P須要進行適配(限制了明文流量的網絡請求,非加密的流量請求都會被系統禁止掉). 當我發現這個的時候,彷彿看到了一絲光明,抱着試一試的心態,搞了一下,果真可行.(測試機型爲Pixel 2,Android 9.0)java

1. Android 9.0 適配

Android 9.0上會報如下錯誤,聯網會失敗:android

2018-10-10 16:39:21.312 31611-31646/com.xfhy.tinkerfirmdemo W/CrashReport: java.io.IOException: Cleartext HTTP traffic to android.bugly.qq.com not permitted
        at com.android.okhttp.HttpHandler$CleartextURLFilter.checkURLPermitted(HttpHandler.java:115)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:458)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:127)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:258)
        at com.tencent.bugly.proguard.ai.a(BUGLY:265)
        at com.tencent.bugly.proguard.ai.a(BUGLY:114)
        at com.tencent.bugly.proguard.al.run(BUGLY:355)
        at com.tencent.bugly.proguard.ak$1.run(BUGLY:723)
        at java.lang.Thread.run(Thread.java:764)
2018-10-10 16:39:21.312 31611-31646/com.xfhy.tinkerfirmdemo E/CrashReport: Failed to upload, please check your network.
2018-10-10 16:39:21.312 31611-31646/com.xfhy.tinkerfirmdemo D/CrashReport: Failed to execute post.
2018-10-10 16:39:21.312 31611-31646/com.xfhy.tinkerfirmdemo E/CrashReport: [Upload] Failed to upload(1): Failed to upload for no response!
2018-10-10 16:39:21.313 31611-31646/com.xfhy.tinkerfirmdemo E/CrashReport: [Upload] Failed to upload(1) userinfo: failed after many attempts
複製代碼

解決辦法bash

  • 具體緣由:Android P - CLEARTEXT communication not permitted by network security policy 詳細介紹

在資源文件新建xml目錄,新建文件 network_security_config.xml網絡

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">android.bugly.qq.com</domain>
    </domain-config>
</network-security-config>
複製代碼

而後在清單文件中application下加入android:networkSecurityConfig="@xml/network_security_config"便可app

相關文章
相關標籤/搜索