为什么我在升级到 Cordova Android 8 后看到 net::ERR_CLEARTEXT_NOT_PERMITTED 错误?

新手上路,请多包涵

升级到 Cordova Android 8.0 后,我在尝试连接到 http:// 目标时看到 net::ERR_CLEARTEXT_NOT_PERMITTED 错误。

为什么会这样,我该如何解决?

原文由 Oliver Salzburg 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.3k
2 个回答

Cordova Android 平台中的默认 API 级别已升级。在 Android 9 设备上,现在 默认禁用 明文通信。

要再次允许明文通信,请将 android:usesCleartextTraffic application 标签上的 --- 设置为 true

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

如评论中所述,如果您之前没有定义 android XML 命名空间,您将在构建期间收到 error: unbound prefix 。这表明您需要将其添加到您的 widget 标记中相同的 config.xml 中,如下所示:

 <widget id="you-app-id" version="1.2.3"
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">

原文由 Oliver Salzburg 发布,翻译遵循 CC BY-SA 4.0 许可协议

我正在使用 Vue 和 Capacitor 3 运行 Ionic 5,并且在不支持 https 的网站上使用 InAppBrowser 时遇到此错误。对于电容器应用程序,不使用 config.xml AndroidManifest.xml 而直接编辑 —。

首先,在此处创建网络安全配置文件 YOUR_IONIC_APP_ROOT\android\app\main\res\xml\network_security_config.xml

 <?xml version="1.0" encoding="utf-8"?>
<network-security-config>
   <domain-config cleartextTrafficPermitted="true">
        <domain>www.example.com</domain>
   </domain-config>
</network-security-config>

然后编辑 YOUR_IONIC_APP_ROOT\android\app\main\AndroidManifest.xml 添加 android:networkSecurityConfig="@xml/network_security_config"application

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="io.ionic.starter">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"

        android:networkSecurityConfig="@xml/network_security_config"

        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <!-- ... -->
    </application>
    <!-- ... -->
</manifest>

原文由 wsamoht 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题