8

我正在使用Capacitor构建 Ionic 应用程序。这些是为了在 Android Studio 中打开 android 应用程序而运行的以下命令。

npx cap add android
ionic build
npx cap copy
npx cap open android

在 Android Studio 中,我运行构建并单击运行,之后我net::ERR_CLEARTEXT_NOT_PERMITTED在设备中看到错误。我已经看到各种帖子都有相同的错误,但那些是与Cordova构建有关的。就我而言,我没有使用 Cordova 来准备 android 应用程序。

错误信息

以下是我的 Ionic App 的一些摘录。

capacitor.config.json文件

{
  "appId": "com.abc",
  "appName": "abc",
  "bundledWebRuntime": false,
  "npmClient": "npm",
  "webDir": "www",
  "cordova": {
    "preferences": {
      "ScrollEnabled": "false",
      "android-minSdkVersion": "19",
      "BackupWebStorage": "none",
      "SplashMaintainAspectRatio": "true",
      "FadeSplashScreenDuration": "0",
      "SplashShowOnlyFirstTime": "false",
      "SplashScreen": "none",
      "SplashScreenDelay": "0"
    }
  },
  "server": {
    "url": "http://192.168.1.208:8100"
  }
}

我也在 Android Studio 的 LogCat 中看到了这个错误

W/cr_AwContents: Application attempted to call on a destroyed WebView
    java.lang.Throwable
        at org.chromium.android_webview.AwContents.a(PG:127)
        at org.chromium.android_webview.AwContents.a(PG:209)
        at com.android.webview.chromium.WebViewChromium.evaluateJavaScript(PG:8)
        at android.webkit.WebView.evaluateJavascript(WebView.java:1113)
        at com.getcapacitor.cordova.MockCordovaWebViewImpl$1.run(MockCordovaWebViewImpl.java:203)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6923)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870)
4

9 回答 9

14

AndroidManifest.xmlapplication元素中添加这个给你

<application
    android:usesCleartextTraffic="true"
于 2020-03-29T01:24:34.660 回答
8

When you specify the server in your Capacitor config, you should set server.cleartext to true to prevent this issue from happening. Example:

{
  "appId": "com.abc",
  "appName": "abc",
  "npmClient": "npm",
  "server": {
    "url": "http://192.168.1.208:8100", 
    "cleartext": true
  }
}

This is not very well documented - the only place I found this configuration being used was on https://capacitorjs.com/docs/guides/live-reload

于 2020-11-29T22:33:32.793 回答
6

这篇文章帮助我找到了解决问题的方法。

我删除了电容器.config.json 文件中的字段server使其工作。

"server": {
    "url": "http://localhost:8100"
}

现在我的电容器.config.json 看起来像

{
  "appId": "com.abc",
  "appName": "abc",
  "bundledWebRuntime": false,
  "npmClient": "npm",
  "webDir": "www",
  "cordova": {
    "preferences": {
      "ScrollEnabled": "false",
      "android-minSdkVersion": "19",
      "BackupWebStorage": "none",
      "SplashMaintainAspectRatio": "true",
      "FadeSplashScreenDuration": "0",
      "SplashShowOnlyFirstTime": "false",
      "SplashScreen": "none",
      "SplashScreenDelay": "0"
    }
  }
}
于 2020-03-28T21:50:51.930 回答
5

您可以通过在电容器.configcleartext文件中启用它来允许 Web 视图中的流量,方法是添加以下代码

server: {
  cleartext: true
}

构建您的项目以查看更改

ionic capacitor build android
于 2021-07-02T09:38:29.553 回答
2

将此代码添加到电容器配置中: server: { cleartext: true }

于 2021-07-23T21:46:37.920 回答
0

这对我有用: https ://www.basezap.com/fix-leartext-error-for-websites/

在 app/manifests/AndroidManifest.xml 编辑应用程序标签添加 android:usesCleartextTraffic="true" 属性

于 2021-05-25T13:22:46.787 回答
0

虽然上述解决方案可能有效,但也值得检查您的网络资产是否已正确复制到目录,因为缺少 index.html 会引发android/app/src/main相同的错误。net::ERR_CLEARTEXT_NOT_PERMITTED

在某些情况下,例如当cap sync无法删除之前的资产目录时,文件未正确复制,但进程不会因错误而退出。

于 2021-04-18T10:43:42.543 回答
0

进入capacitor.config.json并添加cleartext: true道具:

"server": {
  "cleartext": true
}

然后,运行npx cap copy,启动服务器,重新编译并再次从您的 IDE(Xcode/AndroidStudio)运行项目。

于 2022-02-08T15:59:45.250 回答
0

只需运行此命令:

ionic capacitor run android -l --ssl
于 2020-04-04T02:35:59.090 回答