我收到Android 8用户的报告,我的应用程序(使用后端提要)不显示内容。经过调查,我发现Android 8上发生了以下异常:
08-29 12:03:11.246 11285-11285/ E/: [12:03:11.245, main]: Exception: IOException java.io.IOException: Cleartext HTTP traffic to * 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.deiw.android.generic.tasks.AbstractHttpAsyncTask.doConnection(AbstractHttpAsyncTask.java:207)
at com.deiw.android.generic.tasks.AbstractHttpAsyncTask.extendedDoInBackground(AbstractHttpAsyncTask.java:102)
at com.deiw.android.generic.tasks.AbstractAsyncTask.doInBackground(AbstractAsyncTask.java:88)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
(我删除了包名、URL和其他可能的标识符)
在Android 7和更低版本上,一切都正常,我没有在Manifest中设置Android:usesCleartextTraffic(并且将其设置为true没有帮助,这是默认值),也没有使用网络安全信息。如果我调用NetworkSecurityPolicy.getInstance().isCleartextTrafficPermitted(),它将使用相同的apk文件,为Android 8返回false,为旧版本返回true。我试图在谷歌关于Android O的信息中找到一些关于这一点的信息,但没有成功。
我建议同时添加开发和生产网络配置:
添加res/xml/network_security_config_dev.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">10.0.2.2</domain>
</domain-config>
</network-security-config>
地址/xml/network_security_config_prod.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="false">
<domain includeSubdomains="true">yourdomain.com</domain>
</domain-config>
</network-security-config>
在Gradle Scripts(在android studio中)下,找到build.Gradle(android.app)并查找buildTypes:release和debug(如果不存在则创建):
buildTypes {
release {
minifyEnabled false
manifestPlaceholders.securityConfig = "@xml/network_security_config_prod"
}
debug {
manifestPlaceholders.securityConfig = "@xml/network_security_config_dev"
}
}
在AndroidManifest.xml中,使用securityConfig占位符如下(在build.gradle中定义):
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:networkSecurityConfig="${securityConfig}" <------- here
videoView无法打开此视频在线视频
创建文件res/xml/network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>
AndroidManifest.xml文件中的新应用程序:
android:networkSecurityConfig="@xml/network_security_config"
https://techprogrammingideas.blogspot.com/2021/02/android-code-for-displaying-video-with.html
https://youtu.be/90hWWAqfdUU
您可能只想在调试时允许明文,但要保留在生产中拒绝明文的安全优势。这对我很有用,因为我在不支持https的开发服务器上测试我的应用程序。以下是如何在生产中强制https,但在调试模式下允许明文:
在build.gradle中:
// Put this in your buildtypes debug section:
manifestPlaceholders = [usesCleartextTraffic:"true"]
// Put this in your buildtypes release section
manifestPlaceholders = [usesCleartextTraffic:"false"]
在AndroidManifest.xml中的应用程序标记中
android:usesCleartextTraffic="${usesCleartextTraffic}"
我已经从已经存在的android清单文件中删除了这行
android:networkSecurityConfig="@xml/network_security_config"
并添加了
android:usesCleartextTraffic="true"
清单中的应用程序标记
<application
android:usesCleartextTraffic="true"
android:allowBackup="true"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme"
>
然后这个错误Cleartext HTTP流量到overlay.openstreetmap.nl不允许在我的android 9和10中消失。我希望这也适用于android 8,如果它有帮助,请不要忘记投票谢谢