前前后后忙了两个月,终于发版了,终于有时间再写东西了
今天推送的是项目中兼容android 9.0 时碰见的一个问题,希望对你们有帮助。
一、问题现象:
项目中使用 https://github.com/danikula/AndroidVideoCache 作为视频缓存组件,但是在9.0手机上无法正常缓存,并且报错:
1、详细错误截图
2、详细错误文本描述
二、问题原因:
根本原因是从Android9.0开始,出于完全因素考虑,默认不再支持http网络请求,需要使用 https。
但是,在AndroidVideoCache中使用了一个缓存代理地址:http://127.0.0.1,所以,就会报错。
三、解决方案:
解决的基本思路是:对指定的网址进行过滤,强制允许指定网址继续使用http请求
参考地址1:https://stackoverflow.com/questions/45940861/android-8-cleartext-http-traffic-not-permitted
参考地址2:https://developer.android.com/training/articles/security-config#CleartextTrafficPermitted
参考地址3: https://koz.io/android-m-and-the-war-on-cleartext-traffic/
下列解决步骤基于参考地址1整理:
1、步骤1
A:创建 res/xml/networksecurityconfig.xml
注意:上述代码的意思是解除对指定网址的限制。
在解除上述视频缓存库报错信息时,节点的具体取值如下:
B:修改 AndroidManifest.xml
配置 android:networkSecurityConfig
2、步骤2
A:修改AndroidManifest.xml
启用 android:usesCleartextTraffic
3、步骤3
在 @david.s'的回答中指出 也可能会导致该问题 -
在 Manifest Docs https://developer.android.com/guide/topics/manifest/manifest-element#targetSandboxVersion中对于 有如下描述
android:targetSandboxVersion
The target sandbox for this app to use. The higher the sandbox version number, the higher the level of security. Its default value is 1; you can also set it to 2. Setting this attribute to 2 switches the app to a different SELinux sandbox. The following restrictions apply to a level 2 sandbox:
The default value of usesCleartextTraffic in the Network Security Config is false.Uid sharing is not permitted.
所以,如果你在 AndroidManifest.xml 的节点中配置了 ,需要将它的值置为1.
A:修改AndroidManifest.xml
降低 android:targetSandboxVersion 的版本
领取专属 10元无门槛券
私享最新 技术干货