You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ionic应用Chrome正常运行,Android真机/模拟器报net::ERR_CONNECTION_REFUSED

Ionic Android真机/模拟器加载失败:网页未找到/ERR_CONNECTION_REFUSED

问题现象

  • ionic serve在Chrome浏览器中正常显示首页
  • ionic cordova run android --device部署到真机时提示网页未找到
  • 模拟器运行时出现net::ERR_CONNECTION_REFUSED错误
  • 已尝试配置:
    • network_security_config.xml中设置cleartextTrafficPermitted="true"
    • config.xml添加以下偏好:
      <preference name="hostname" value="localhost" /> 
      <preference name="AndroidInsecureFileModeEnabled" value="true" />
      

Application Error net::ERR_CONNECTION_REFUSED


环境信息

ionic info

Ionic:

   Ionic CLI                     : 7.2.0 (/home/user/npm-global/lib/node_modules/@ionic/cli)
   Ionic Framework               : @ionic/angular 8.4.1
   @angular-devkit/build-angular : 18.2.12
   @angular-devkit/schematics    : 18.2.12
   @angular/cli                  : 18.2.12
   @ionic/angular-toolkit        : 12.1.1

Cordova:

   Cordova CLI       : 12.0.0 (cordova-lib@12.0.2)
   Cordova Platforms : android 13.0.0
   Cordova Plugins   : cordova-plugin-ionic-keyboard 2.2.0, cordova-plugin-ionic-webview 5.0.0, (and 15 other plugins)

Utility:

   cordova-res : 0.15.4
   native-run  : 2.0.1

System:

   Android SDK Tools : 26.1.1 (/home/user/Android/Sdk)
   NodeJS            : v22.12.0 (/usr/local/bin/node)
   npm               : 10.9.0
   OS                : Linux 5.15

ionic cordova requirements android

Requirements check results for android:
Java JDK: installed 21.0.5
Android SDK: installed true
Android target: not installed 
Command failed with exit code 1: avdmanager list target
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema
        at com.android.repository.api.SchemaModule$SchemaModuleVersion.<init>(SchemaModule.java:156)
        at com.android.repository.api.SchemaModule.<init>(SchemaModule.java:75)
        at com.android.sdklib.repository.AndroidSdkHandler.<clinit>(AndroidSdkHandler.java:81)
        at com.android.sdklib.tool.AvdManagerCli.run(AvdManagerCli.java:213)
        at com.android.sdklib.tool.AvdManagerCli.main(AvdManagerCli.java:200)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlSchema
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
        ... 5 more
Gradle: installed /opt/gradle/gradle-8.7/bin/gradle
Some of requirements check failed

gradle -v

------------------------------------------------------------
Gradle 8.7
------------------------------------------------------------

Build time:   2024-03-22 15:52:46 UTC
Revision:     650af14d7653aa949fce5e886e685efc9cf97c10

Kotlin:       1.9.22
Groovy:       3.0.17
Ant:          Apache Ant(TM) version 1.10.13 compiled on January 4 2023
JVM:          21.0.5 (Ubuntu 21.0.5+11-Ubuntu-1ubuntu120.04)
OS:           Linux 5.15.0-127-generic amd64

package.json 核心依赖(节选)

{
  "dependencies": {
    "@ionic/angular": "^8.4.1",
    "cordova-android": "13.0.0",
    "cordova-plugin-ionic-webview": "^5.0.0"
  },
  "cordova": {
    "platforms": [
      "ios",
      "android"
    ]
  }
}

config.xml 相关配置

<preference name="hostname" value="localhost" />
<preference name="AndroidInsecureFileModeEnabled" value="true" />
<platform name="android">
        <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android">
            <application android:usesCleartextTraffic="true" />
            <application android:networkSecurityConfig="@xml/network_security_config" />
        </edit-config>
</platform>

/resources/android/xml/network_security_config.xml

<domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="true">localhost</domain>
</domain-config>

解决方案

1. 修复Java与Android SDK工具兼容性问题

从requirements检查结果看,JDK 21与Android SDK Tools 26.1.1不兼容(JDK 17+移除了javax.xml.bind模块),导致无法识别Android Target:

  • 方案一:降级Java到JDK 11(Android SDK Tools 26.x最高兼容版本)
  • 方案二:升级Android SDK Tools到最新版本,并安装对应cordova-android 13.0.0的Android Target(Android 13)

2. 修正AndroidManifest与网络安全配置

当前edit-config重复写入<application>标签会导致属性被覆盖,需合并为单个标签:

<platform name="android">
    <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android">
        <application android:usesCleartextTraffic="true" android:networkSecurityConfig="@xml/network_security_config" />
    </edit-config>
</platform>

完善network_security_config.xml,确保包含完整根节点并支持模拟器访问:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">localhost</domain>
        <domain includeSubdomains="true">10.0.2.2</domain> <!-- Android模拟器访问宿主机的别名 -->
        <domain includeSubdomains="true">127.0.0.1</domain>
    </domain-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>

3. 更新Cordova WebView插件并配置

当前cordova-plugin-ionic-webview版本较旧,与Ionic 8/Angular 18兼容性不足,升级并添加本地访问权限:

ionic cordova plugin remove cordova-plugin-ionic-webview
ionic cordova plugin add cordova-plugin-ionic-webview@latest

在config.xml中添加WebView本地访问偏好:

<preference name="AllowFileAccess" value="true" />
<preference name="AllowUniversalAccessFromFileURLs" value="true" />

4. 执行完整构建流程

避免直接运行run命令,先执行生产构建确保资源打包正确:

ionic cordova build android --prod
ionic cordova run android --device

5. 模拟器网络排查

若模拟器仍报ERR_CONNECTION_REFUSED:

  • 测试模拟器网络连通性(打开内置浏览器访问公共网站)
  • 若需访问宿主机后端服务,替换localhost为10.0.2.2(Android模拟器的宿主机映射IP)

内容的提问来源于stack exchange,提问作者HongKong Simfusion

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 04:52:05