本文介绍了ServiceNotDeclaredException:在AndroidManifest.xml中未正确声明BeaconService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在Android项目中运行单元测试时出现以下错误(在构建应用程序并在设备上运行该应用程序时,我没有得到该错误):

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

Android Studio自动将Android Beacon库中的AndroidManifest.xml合并到我自己的AndroidManifest.xml中,从而无需在我的AndroidManifest.xml中声明Beacon服务.

这是我当前的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,但找不到任何问题./p>

这是包含Android Beacon库的部分:

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

您知道什么会导致此问题吗?

解决方案

在Android信标库中使用Robolectric测试时,只需将以下行添加到测试的顶部:

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

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=trueorg.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?

解决方案

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

这篇关于ServiceNotDeclaredException:在AndroidManifest.xml中未正确声明BeaconService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 04:01