ServiceNotDeclaredException:BeaconService 未在 AndroidManifest.xml 中正确声明

ServiceNotDeclaredException: The BeaconService is not properly declared in AndroidManifest.xml

当我在我的 Android 项目中 运行 进行单元测试时出现以下错误(我在构建应用程序并 运行 在设备):

BeaconService 未在 AndroidManifest.xml 中正确声明。如果使用 Eclipse,请确认您的 project.properties 有 manifestmerger.enabled=true org.altbeacon.beacon.BeaconManager$ServiceNotDeclaredException:BeaconService 未在 AndroidManifest.xml 中正确声明。如果使用 Eclipse,请确认您的 project.properties 具有 manifestmerger.enabled=true

Android Studio 自动将 Android Beacon 库中的 AndroidManifest.xml 合并到我自己的 AndroidManifest.xml 中,这样就无需在我的 [= 中声明 Beacon Services 32=].

这是我当前的 AndroidManifest.xml(位于我的 /src/main/ 文件夹中,我的 /src/test/ 文件夹中没有):

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

我已经查看了构建文件夹 (app/build/outputs/apk/manifest-merger-release-report.txt) 中的 manifest-merger-release-report.txt,但没有发现任何问题。

这是包含 Android 信标库的部分:

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

知道是什么导致了这个问题吗?

将 Robolectric 测试与 Android 信标库一起使用时,只需将此行添加到测试的顶部:

BeaconManager.setsManifestCheckingDisabled(true);

这将在创建 BeaconManager 的实例时禁用对正确清单条目的检查。这是必要的,因为当测试 运行 时,Robolectric 不会让您访问真正的 AndroidManifest。在库本身内部构建 Robolectric 测试时,我自己也遇到了同样的问题。您可以在此处查看示例:

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