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

Ionic4适配Android12(SDK31)启动报找不到android.hardware.bluetooth崩溃

问题根因

本次启动崩溃和蓝牙权限配置无关,核心诱因是AndroidManifest合并时出现属性写入错位:
Android系统启动应用时,会读取<application>节点的android:name属性,实例化对应的Application类完成初始化;该属性仅支持填写自定义Application子类的全限定类名,留空时系统会调用默认的android.app.Application实现。你编译生成的Manifest中,<application>节点被错误追加了android:name="android.hardware.bluetooth"属性,系统启动时会尝试将android.hardware.bluetooth作为Application类实例化,但这个值原本是<uses-feature>标签的属性值,根本不是可实例化的应用类,自然会抛出ClassNotFoundException直接崩溃。

该错位问题是构建链版本兼容bug导致的:旧构建环境(JDK8+SDK27+低版本Gradle)下,Ionic4依赖的Cordova构建链Manifest合并规则容错性高,不会出现属性错写;升级到SDK31+JDK11+Gradle7后,低版本cordova-android和新版Manifest合并工具存在兼容缺陷,你在config.xml中配置的<uses-feature android:name="android.hardware.bluetooth" android:required="true" />没有被正确写入Manifest根节点下的feature列表,反而把属性错误注入到了<application>节点上。

修复方案

按以下步骤操作即可解决:

  • 修正Manifest配置规则,不要直接修改编译生成的临时Manifest文件,在项目根目录config.xml的<platform name="android">节点下添加配置,强制移除<application>节点上的错误属性:
<edit-config file="app/src/main/AndroidManifest.xml" mode="remove" target="/manifest/application">
    <application android:name="android.hardware.bluetooth" android:required="true" />
</edit-config>

同时校验蓝牙相关配置的写入层级,确保uses-feature和Android12要求的蓝牙权限是正确写入Manifest根节点的,参考配置如下:

<config-file parent="/" target="AndroidManifest.xml">
    <uses-feature android:name="android.hardware.bluetooth" android:required="true" />
    <!-- Android12 强制要求的蓝牙运行时权限,需同步配置 -->
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
</config-file>
  • 清理全量构建缓存,避免旧的错误Manifest缓存影响编译结果,在项目根目录执行以下命令:
# 移除旧构建产物
rm -rf platforms/android/build platforms/android/app/build
# 执行Gradle清理
cd platforms/android && ./gradlew clean && cd ../../
# 重新编译安装包
ionic cordova build android --release
  • 若上述操作后仍存在Manifest合并错位问题,说明你当前使用的cordova-android版本对SDK31兼容度过低,可升级到兼容Ionic4的最高稳定版cordova-android@9.1.0(不要升级到10+版本,否则会和Ionic4的JS桥接层出现兼容冲突),执行命令:
ionic cordova platform rm android
ionic cordova platform add android@9.1.0

重新配置完后,先检查编译生成的platforms/android/app/src/main/AndroidManifest.xml文件,确认<application>节点下不存在android:name="android.hardware.bluetooth"、android:required="true"两个错位属性,再安装到Android12设备测试即可正常启动。

内容的提问来源于stack exchange,提问作者Fractal Mind

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 14:48:18