当我在Android项目中运行单元测试时出现以下错误(在构建应用程序并在设备上运行时,我没有收到错误):
未在AndroidManifest.xml中正确声明BeaconService。如果使用Eclipse,请验证您的project.properties是否有manifestmerger.enabled = true org.altbeacon.beacon.BeaconManager $ ServiceNotDeclaredException:未在AndroidManifest.xml中正确声明BeaconService。如果使用Eclipse,请验证您的project.properties是否有manifestmerger.enabled = true
Android Studio会自动将Android Beacon Library中的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,但是找不到任何问题
这是包含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
知道什么可能导致这个问题吗?
答案 0 :(得分:4)
在Android Beacon Library中使用Robolectric测试时,只需将此行添加到测试的顶部:
BeaconManager.setsManifestCheckingDisabled(true);
这将在创建BeaconManager
的实例时禁用对正确清单条目的检查。这是必要的,因为在运行测试时,Robolectric不允许您访问真正的AndroidManifest。在库内部构建Robolectric测试时,我自己遇到了同样的问题。你可以在这里看到一个例子: