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

问题描述 投票:0回答:2

我的离子应用程序无法在android 9设备上运行,在版本低于9的android设备上可以正常运行。载入目标网页后,应用程序未连接到后端。有什么想法吗?

我正在使用的环境:离子:

ionic(Ionic CLI):^ 5.0.0角^ 7.2.2

科尔多瓦:

cordova(Cordova CLI):8.0.0Cordova平台:android 8.0.0

系统:

Android SDK工具:29NodeJS:第10版npm:6.2.0

android cordova ionic-framework android-9.0-pie android-10.0
2个回答
0
投票

原因

这是由于此政策从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>

解决方案

用https URL替换所有http呼叫。

或...

强制HTTP(不安全)

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

[您需要在清单中的<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>

-2
投票

thnx到Mostafa Harb,他是对的,我在后端使用http请求而不是https

© www.soinside.com 2019 - 2024. All rights reserved.