Android 中的离子 5/电容器 ERR_CLEARTEXT_NOT_PERMITTED

Pav*_*van 5 android ionic-framework capacitor ionic5

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

npx cap add android
ionic build
npx cap copy
npx cap open android
Run Code Online (Sandbox Code Playgroud)

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

错误信息

以下是我的 Ionic 应用程序的一些摘录。

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"
  }
}

Run Code Online (Sandbox Code Playgroud)

我也在 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)
Run Code Online (Sandbox Code Playgroud)

Mar*_*ika 12

您可以cleartext通过在Capacitor.config文件中启用 Web 视图中的流量,并添加以下代码

server: {
  cleartext: true
}
Run Code Online (Sandbox Code Playgroud)

构建您的项目以查看更改

ionic capacitor build android
Run Code Online (Sandbox Code Playgroud)


Aar*_*ers 10

将此添加到您AndroidManifest.xmlapplication元素中

<application
    android:usesCleartextTraffic="true"
Run Code Online (Sandbox Code Playgroud)


pal*_*sch 8

当您server在 Capacitor 配置中指定 时,您应该设置server.cleartexttrue以防止发生此问题。例子:

{
  "appId": "com.abc",
  "appName": "abc",
  "npmClient": "npm",
  "server": {
    "url": "http://192.168.1.208:8100", 
    "cleartext": true
  }
}
Run Code Online (Sandbox Code Playgroud)

这不是很好的记录 - 我发现使用此配置的唯一地方是在https://capacitorjs.com/docs/guides/live-reload


Pav*_*van 7

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

我删除了Capacitor.config.jsonserver文件中的字段以使其正常工作。

"server": {
    "url": "http://localhost:8100"
}
Run Code Online (Sandbox Code Playgroud)

现在我的电容器.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"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

将此代码添加到 Capacitor.config 中: server: {cleartext: true }