Cordova 离子应用程序无法在 Android 9 && 10 上运行

Cordova ionic app not working on Android 9 && 10

我的离子应用程序无法在 android 9 台设备上运行,但在 android 版本低于 9 的设备上运行良好。 应用程序在加载着陆页时未连接到后端。 有什么想法吗?

我使用的环境: 离子:

ionic(离子 CLI):^5.0.0 Angular^7.2.2

科尔多瓦:

科尔多瓦(科尔多瓦 CLI):8.0.0 科尔多瓦平台:android 8.0.0

系统:

Android SDK 工具:29 节点JS:v10 npm:6.2.0

原因

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

Applications intending to connect to destinations using only secure connections can opt-out of supporting cleartext (using the unencrypted HTTP protocol instead of HTTPS) to those destinations.

Note: The guidance in this section applies only to apps that target Android 8.1 (API level 27) or lower. Starting with Android 9 (API level 28), cleartext support is disabled by default.

现在默认情况下禁用明文(HTTP 流量),因此相当于拥有这样的东西

<domain-config cleartextTrafficPermitted="false">
  <domain includeSubdomains="true">*</domain>
</domain-config>

解决方案

将所有 http 调用替换为 https URL。

或者...

强制 HTTP(不安全)

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

您需要向清单中的 <application> 标签添加一个属性,以便不覆盖所有内容,您需要将 <edit-config> 标签与 mode="merge" 一起使用(请参阅:https://cordova.apache.org/docs/en/latest/plugin_ref/spec.html#edit-config )

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>

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>

您可以在 config.xml 内的 android 平台部分添加以下内容:

<edit-config file="AndroidManifest.xml" mode="merge" target="/manifest/application">
        <application android:usesCleartextTraffic="true" />
</edit-config>

最终结果:

<platform name="android">
...
<edit-config file="AndroidManifest.xml" mode="merge" target="/manifest/application">
    <application android:usesCleartextTraffic="true" />
</edit-config>
...
</platform>

我在使用 Ionic 3 and Cordova Android 9.0

时遇到了这个问题

我通过将这些行放在 config.xml 文件中解决了这个问题。我的解决方案与 @Emilio's Ferrer 解决方案

略有不同

首先您需要在文件开头的 <widget> 目标中添加 xmlns:android="http://schemas.android.com/apk/res/android"。如果没有此设置,您将无法构建您的应用程序。

您的<widget>目标将如下所示。

<widget id="your.id" version="x.x.x" xmlns="http://www.w3.org/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cdv="http://cordova.apache.org/ns/1.0">

然后将其添加到 <platform name="android" 标签内

<edit-config src="platforms/android/app/src/main/AndroidManifest.xml" target="AndroidManifest.xml">
        <application android:usesCleartextTraffic="true" />
</edit-config>

请注意,这是允许通过 HTTP 请求的解决方法,当然建议您只向 SSL 证书下的地址发出请求。