Cordova 离子应用程序不适用于 Android 9 && 10

2 android cordova ionic-framework android-9.0-pie android-10.0

我的 ionic 应用程序无法在 android 9 设备上运行,但在版本低于 9 的 android 设备上运行良好。加载登录页面时,应用程序未连接到后端。有任何想法吗 ?

我使用的环境:离子:

离子(离子 CLI):^5.0.0 角度 ^7.2.2

科尔多瓦:

科尔多瓦(科尔多瓦 CLI):8.0.0 科尔多瓦平台:安卓 8.0.0

系统:

Android SDK 工具:29 NodeJS:v10 npm:6.2.0

小智 5

原因

这是由于此政策从 Android 9 开始:https : //developer.android.com/training/articles/security-config.html#CleartextTrafficPermitted

打算仅使用安全连接连接到目标的应用程序可以选择不支持这些目标的明文(使用未加密的 HTTP 协议而不是 HTTPS)。

注意:本节中的指南仅适用于面向 Android 8.1(API 级别 27)或更低版本的应用。从 Android 9(API 级别 28)开始,默认情况下禁用明文支持。

明文(HTTP 流量)现在默认是禁用的,所以它相当于有这样的东西

<domain-config cleartextTrafficPermitted="false">
  <domain includeSubdomains="true">*</domain>
</domain-config>
Run Code Online (Sandbox Code Playgroud)

解决方案

用 https URL 替换所有 http 调用。

或者...

强制 HTTP(不安全)

如果你真的需要允许某些请求的 HTTP 流量,你可以通过在你的 Android Manifest 中添加一些配置来实现,或者创建一个插件来在你每次从头重建应用程序时更新清单。

您需要为<application>清单中的标签添加一个属性,因此为了不覆盖所有内容,您需要使用该<edit-config>标签mode="merge"(请参阅:https : //cordova.apache.org/docs/en/latest/plugin_ref/spec.html #编辑配置

HTTP 不被视为安全协议,发送的每个请求(及其负载)都可以被攻击者读取,所以要小心

plugin.xml

<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
        xmlns:android="http://schemas.android.com/apk/res/android"
        id="phonegap-plugin-old-http-support-android" version="0.0.1">
    <name>OldHttpSupportPlugin</name>

    <description>Force HTTP for Android 9+</description>
    <license>MIT</license>

    <keywords>cordova,android,http</keywords>
  
    <engines>
        <engine name="cordova" version=">=6.0.0"/>
        <engine name="cordova-android" version=">=7.0.0"/>
    </engines>
  

    <platform name="android">
        <config-file target="res/xml/config.xml" parent="/*">
            <feature name="OldHttpSupportPlugin">
                <param name="android-package" value="YOUR.PACKAGE.plugin.OldHttpSupportPlugin"/>
            </feature>
        </config-file>


        <!-- Source file for Android -->
        <source-file src="src/android/network_security_config.xml" target-dir="res/xml/" />

        <edit-config file="AndroidManifest.xml" target="/manifest/application" mode="merge">
            <application android:networkSecurityConfig="@xml/network_security_config" />
        </edit-config>
    </platform>
</plugin>
Run Code Online (Sandbox Code Playgroud)

network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">my.domain.com</domain>
        <domain includeSubdomains="true">example.org</domain>
        <domain includeSubdomains="true">111.222.333.444</domain>
    </domain-config>
</network-security-config>
Run Code Online (Sandbox Code Playgroud)