java.lang.RuntimeException: Unable to instantiate application android.hardware.bluetooth on path: DexPathList

I want to use Ionic 4 to compile a Android 12 app.

When I compile my ionic v4 project with JDK8 + SDK Level 27.0.3 for Android <= 10 and it works.

But for Android 12 I need to use SDK level 31.0.0 + JDK 11 + Gradle 7.* and it crashes

After the update when I open my app I see the splash screen for half a second then it crashes.

After I adb shell > logcat I get

E AndroidRuntime: FATAL EXCEPTION: main
E AndroidRuntime: Process: com.myapp, PID: 28761
E AndroidRuntime: java.lang.RuntimeException: Unable to instantiate application android.hardware.bluetooth package com.myapp: java.lang.ClassNotFoundException: Didn't find class "android.hardware.bluetooth" on path: DexPathList[[zip file "/data/app/~~8T8KfSjt3r4E8wgw-yQ0KA==/com.myapp-3qG2CR3HTq76WQg0MM5V1Q==/base.apk"],nativeLibraryDirectories=[/data/app/~~8T8KfSjt3r4E8wgw-yQ0KA==/com.myapp-3qG2CR3HTq76WQg0MM5V1Q==/lib/arm64, /data/app/~~8T8KfSjt3r4E8wgw-yQ0KA==/com.myapp-3qG2CR3HTq76WQg0MM5V1Q==/base.apk!/lib/arm64-v8a, /system/lib64, /system/system_ext/lib64]]
E AndroidRuntime:   at android.app.LoadedApk.makeApplication(LoadedApk.java:1464)
E AndroidRuntime:   at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7458)
E AndroidRuntime:   at android.app.ActivityThread.access$1700(ActivityThread.java:310)
E AndroidRuntime:   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2281)
E AndroidRuntime:   at android.os.Handler.dispatchMessage(Handler.java:106)
E AndroidRuntime:   at android.os.Looper.loopOnce(Looper.java:226)
E AndroidRuntime:   at android.os.Looper.loop(Looper.java:313)
E AndroidRuntime:   at android.app.ActivityThread.main(ActivityThread.java:8663)
E AndroidRuntime:   at java.lang.reflect.Method.invoke(Native Method)
E AndroidRuntime:   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567)
E AndroidRuntime:   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
E AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "android.hardware.bluetooth" on path: DexPathList[[zip file "/data/app/~~8T8KfSjt3r4E8wgw-yQ0KA==/com.myapp-3qG2CR3HTq76WQg0MM5V1Q==/base.apk"],nativeLibraryDirectories=[/data/app/~~8T8KfSjt3r4E8wgw-yQ0KA==/com.myapp-3qG2CR3HTq76WQg0MM5V1Q==/lib/arm64, /data/app/~~8T8KfSjt3r4E8wgw-yQ0KA==/com.myapp-3qG2CR3HTq76WQg0MM5V1Q==/base.apk!/lib/arm64-v8a, /system/lib64, /system/system_ext/lib64]]
E AndroidRuntime:   at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:218)
E AndroidRuntime:   at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
E AndroidRuntime:   at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
E AndroidRuntime:   at android.app.AppComponentFactory.instantiateApplication(AppComponentFactory.java:76)
E AndroidRuntime:   at androidx.core.app.CoreComponentFactory.instantiateApplication(CoreComponentFactory.java:52)
E AndroidRuntime:   at android.app.Instrumentation.newApplication(Instrumentation.java:1177)
E AndroidRuntime:   at android.app.LoadedApk.makeApplication(LoadedApk.java:1456)
E AndroidRuntime:   ... 10 more

I use in config.xml

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.myappid" version="4.4.49" xmlns="http://www.w3.org/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android">
    <name>My app Title</name>
    <description>Blablabla</description>
    <author email="sales@myapp.com" href="http://myapp.com/">MyApp</author>
    <content src="index.html" />
    <access origin="*" />
    <allow-navigation href="*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="ws://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <preference name="ScrollEnabled" value="false" />
    <preference name="android-minSdkVersion" value="28" />
    <preference name="android-targetSdkVersion" value="31" />
    <preference name="BackupWebStorage" value="none" />
    <preference name="SplashMaintainAspectRatio" value="true" />
    <preference name="FadeSplashScreenDuration" value="300" />
    <preference name="SplashShowOnlyFirstTime" value="false" />
    <preference name="SplashScreen" value="screen" />
    <preference name="SplashScreenDelay" value="3000" />
    <preference name="loadUrlTimeoutValue" value="700000" />
    <platform name="android">
        <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application">
            <application android:usesCleartextTraffic="true" />
            <application android:requestLegacyExternalStorage="true" />
            <activity android:exported="true" />
            <uses-permission android:name="android.permission.BLUETOOTH" />
            <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
            <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
            <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
            <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
            <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
            <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
            <uses-feature android:name="android.hardware.bluetooth" android:required="true" />
        </edit-config>
        <resource-file src="resources/android/xml/network_security_config.xml" target="app/src/main/res/xml/network_security_config.xml" />
        <preference name="SplashMaintainAspectRatio" value="true" />
        <preference name="SplashShowOnlyFirstTime" value="true" />
        <preference name="AndroidPersistentFileLocation" value="Compatibility" />
        <preference name="loadUrlTimeoutValue" value="700000" />
        <icon density="ldpi" src="resources/android/icon/drawable-ldpi-icon.png" />
        <icon density="mdpi" src="resources/android/icon/drawable-mdpi-icon.png" />
        <icon density="hdpi" src="resources/android/icon/drawable-hdpi-icon.png" />
        <icon density="xhdpi" src="resources/android/icon/drawable-xhdpi-icon.png" />
        <icon density="xxhdpi" src="resources/android/icon/drawable-xxhdpi-icon.png" />
        <icon density="xxxhdpi" src="resources/android/icon/drawable-xxxhdpi-icon.png" />
    </platform>

And it translate to my AndroidManifest.xml

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="40449" android:versionName="4.4.49" package="com.myapp" xmlns:android="http://schemas.android.com/apk/res/android">
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:exported="true" android:hardwareAccelerated="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:name="android.hardware.bluetooth" android:requestLegacyExternalStorage="true" android:required="true" android:supportsRtl="true" android:usesCleartextTraffic="true">
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode" android:exported="true" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@style/Theme.AppCompat.NoActionBar" android:windowSoftInputMode="adjustResize">
            <intent-filter android:label="@string/launcher_name">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <provider android:authorities="${applicationId}.cordova.plugin.camera.provider" android:exported="false" android:grantUriPermissions="true" android:name="org.apache.cordova.camera.FileProvider">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/camera_provider_paths" />
        </provider>
        <activity android:clearTaskOnLaunch="true" android:configChanges="orientation|keyboardHidden|screenSize" android:exported="false" android:name="com.google.zxing.client.android.CaptureActivity" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:windowSoftInputMode="stateAlwaysHidden" />
        <activity android:label="Share" android:name="com.google.zxing.client.android.encode.EncodeActivity" />
    </application>
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <queries>
        <intent>
            <action android:name="android.media.action.IMAGE_CAPTURE" />
        </intent>
        <intent>
            <action android:name="android.intent.action.GET_CONTENT" />
        </intent>
        <intent>
            <action android:name="android.intent.action.PICK" />
        </intent>
        <intent>
            <action android:name="com.android.camera.action.CROP" />
            <data android:mimeType="image/*" android:scheme="content" />
        </intent>
    </queries>
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.FLASHLIGHT" />
    <uses-feature android:name="android.hardware.camera" android:required="true" />
</manifest>

As you can see I think I have all the right permission for bluetooth but why on earth it crash on Android 12!?

Leave a Comment