AndroidP行为变更

270 阅读1分钟

一、不可多进程使用同一WebView

1.举例:程序集成极光推送,在推送的通知里触发一个H5页面跳转,跳转时调用的是一个公共的WebActivity。

由于极光推送会单独开一个进程,程序会闪退,并报错:

java.lang.RuntimeException: Using WebView from more than one process at once with the same data directory is not supported.

2.解决方案: 在自定义的Appliaction类中做如下处理:


public class MyApplication extends Application {
    @Override
    public void onCreate() {
        initPieWebView();
    }

    private static final String PROCESS = "xxxxxx";//包名
    private void initPieWebView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            String processName = getProcessName(this);
            if (!PROCESS.equals(processName)) {
                WebView.setDataDirectorySuffix(processName);
            }
        }
    }
}

二、默认采用Https进行数据访问

也就是运行在9.0 系统的应用没有做Http适配的话,所有Http无法访问,解决这个问题有两种方案可以参考(两种选择一个即可)

1)全局关闭检查

2)局部关闭检查,特定域名关闭检查(需谨慎排查、包括三方SDK)

第一步:需要在Application节点配置xml文件

<application android:networkSecurityConfig="@xml/network_security_config">
    <!-- ... -->
</application>

第二步:

如果全局关闭检查,network_security_config.xml文件内容:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true"/>
</network-security-config>

如果局部关闭检查,特定域名关闭检查

<network-security-config>
    <domain-config cleartextTrafficPermitted="false">
        <domain includeSubdomains="true">http://www.baidu.com</domain>  // 域名
    </domain-config>
</network-security-config>

备注:

1.如果你对自己接手的项目不是特别了解,配置全局就可以。

2.xml配置语法较多 具体查看官方文档

developer.android.google.cn/training/ar…

三、Apache HTTP API 使用变更

要继续使用 Apache HTTP 客户端,以 Android 9 及更高版本为目标的应用可以向其 AndroidManifest.xml 添加以下内容:

<uses-library android:name="org.apache.http.legacy" android:required="false"/>

备注:拥有最低 SDK 版本 23 或更低版本的应用需要 android:required="false" 属性,因为在 API 级别低于 24 的设备上,org.apache.http.legacy 库不可用。 (在这些设备上,Apache HTTP 类在 bootclasspath 中提供。)