ServiceNotDeclaredException: The BeaconService is

2019-07-07 03:47发布

问题:

I get the following error when i run a Unit Test in my Android project (I don't get the error while building the app and running it on a device):

The BeaconService is not properly declared in AndroidManifest.xml. If using Eclipse, please verify that your project.properties has manifestmerger.enabled=true org.altbeacon.beacon.BeaconManager$ServiceNotDeclaredException: The BeaconService is not properly declared in AndroidManifest.xml. If using Eclipse, please verify that your project.properties has manifestmerger.enabled=true

Android Studio automatically merges the AndroidManifest.xml from the Android Beacon Library into my own AndroidManifest.xml, which makes it unnecessary to declare the Beacon Services in my AndroidManifest.xml.

This is my current AndroidManifest.xml (located in my /src/main/ folder, I don't have one in my /src/test/ folder):

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xxx.xxx">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

    <permission
        android:name="com.xxx.xxx.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="com.xxx.xxx.permission.C2D_MESSAGE" />

    <application
        android:name=".xxxApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".LoginActivity"
            android:label="@string/title_activity_login" />

        <receiver
            android:name="org.jboss.aerogear.android.unifiedpush.gcm.AeroGearGCMMessageReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="com.xxx.xxx.android.test.xxxApp" />
            </intent-filter>

            <meta-data
                android:name="DEFAULT_MESSAGE_HANDLER_KEY"
                android:value="com.xxx.xxx.push.AGMessageListener" />
        </receiver>
    </application>
</manifest>

I already looked in my manifest-merger-release-report.txt inside my build folder (app/build/outputs/apk/manifest-merger-release-report.txt), but couldn't find any problems.

This is the part which contains the Android Beacon Library:

uses-permission#android.permission.BLUETOOTH
ADDED from org.altbeacon:android-beacon-library:2.1.3:11:5
    android:name
        ADDED from org.altbeacon:android-beacon-library:2.1.3:11:22
uses-permission#android.permission.BLUETOOTH_ADMIN
ADDED from org.altbeacon:android-beacon-library:2.1.3:12:5
    android:name
        ADDED from org.altbeacon:android-beacon-library:2.1.3:12:22
uses-permission#android.permission.RECEIVE_BOOT_COMPLETED
ADDED from org.altbeacon:android-beacon-library:2.1.3:13:5
    android:name
        ADDED from org.altbeacon:android-beacon-library:2.1.3:13:22
receiver#org.altbeacon.beacon.startup.StartupBroadcastReceiver
ADDED from org.altbeacon:android-beacon-library:2.1.3:16:9
    android:name
        ADDED from org.altbeacon:android-beacon-library:2.1.3:16:19
intent-filter#android.intent.action.ACTION_POWER_CONNECTED+android.intent.action.ACTION_POWER_DISCONNECTED+android.intent.action.BOOT_COMPLETED
ADDED from org.altbeacon:android-beacon-library:2.1.3:17:13
action#android.intent.action.BOOT_COMPLETED
ADDED from org.altbeacon:android-beacon-library:2.1.3:18:17
    android:name
        ADDED from org.altbeacon:android-beacon-library:2.1.3:18:25
action#android.intent.action.ACTION_POWER_CONNECTED
ADDED from org.altbeacon:android-beacon-library:2.1.3:19:17
    android:name
        ADDED from org.altbeacon:android-beacon-library:2.1.3:19:25
action#android.intent.action.ACTION_POWER_DISCONNECTED
ADDED from org.altbeacon:android-beacon-library:2.1.3:20:17
    android:name
        ADDED from org.altbeacon:android-beacon-library:2.1.3:20:25
service#org.altbeacon.beacon.service.BeaconService
ADDED from org.altbeacon:android-beacon-library:2.1.3:24:9
    android:enabled
        ADDED from org.altbeacon:android-beacon-library:2.1.3:26:13
    android:label
        ADDED from org.altbeacon:android-beacon-library:2.1.3:29:13
    android:exported
        ADDED from org.altbeacon:android-beacon-library:2.1.3:27:13
    android:isolatedProcess
        ADDED from org.altbeacon:android-beacon-library:2.1.3:28:13
    android:name
        ADDED from org.altbeacon:android-beacon-library:2.1.3:25:13
service#org.altbeacon.beacon.BeaconIntentProcessor
ADDED from org.altbeacon:android-beacon-library:2.1.3:30:9
    android:enabled
        ADDED from org.altbeacon:android-beacon-library:2.1.3:32:13
    android:exported
        ADDED from org.altbeacon:android-beacon-library:2.1.3:33:13
    android:name
        ADDED from org.altbeacon:android-beacon-library:2.1.3:31:13

Any idea what could cause this issue?

回答1:

When using Robolectric tests with the Android Beacon Library, simply add this line to the top of your test:

BeaconManager.setsManifestCheckingDisabled(true);

This will disable the checking for the proper manifest entries when creating an instance of the BeaconManager. This is necessary, because Robolectric does not give you access to a real AndroidManifest when tests are run. I had encountered the same problem myself when building Robolectric tests inside the library itself. You can see an example of this here:

https://github.com/AltBeacon/android-beacon-library/blob/master/src/test/java/org/altbeacon/beacon/service/scanner/ScanFilterUtilsTest.java#L44