android - Cordova ionic 应用程序无法在 Android 9 && 10 上运行

标签 android cordova ionic-framework android-9.0-pie android-10.0

我的 ionic 应用程序无法在 Android 9 设备上运行,但在版本低于 9 的 Android 设备上运行良好。 加载登陆页面后应用程序不会连接到后端。 有什么想法吗?

我使用的环境: ionic :

ionic ( ionic CLI):^5.0.0 角度^7.2.2

Cordova :

Cordova ( Cordova CLI):8.0.0 Cordova 平台: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 list 中添加一些配置来实现,或者创建一个插件来在每次从头开始重建应用程序时更新 list 。

您需要向 <application> 添加属性 list 中的标记,因此为了不覆盖所有内容,您需要使用 <edit-config>标记为 mode="merge" (参见:https://cordova.apache.org/docs/en/latest/plugin_ref/spec.html#edit-config)

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

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>

关于android - Cordova ionic 应用程序无法在 Android 9 && 10 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60309622/

相关文章:

javascript - 具有 WebView 和 native View 的混合应用程序的优缺点是什么?

Android Hybrid App Keyboard/Dropdown popup resize issue for offscreen navigation

android - 如何在所有页面顶部显示 ionic 旋转器/加载器

javascript - Ionic ionic View 标题未显示

java - 从 ListView 中删除项目时出错

java - 单击自定义 ListView 的项目时如何启动新 Activity

javascript - 可以对覆盖的 div 使用 iscroll 捏合/缩放吗?

javascript - ionic 输入日期默认设置为今天

android - Espresso 不会等待 ViewPager 上的滑动操作完成

cordova - 如果我只有项目的 www 目录,如何使用 CLI 初始化 Cordova 项目?