我遇到了Robolectric我的AndroidManifest.xml出现问题的问题。我的应用运行良好,但Robolectric失败了我的所有测试,甚至assertTrue(true)
。错误是:
android.view.InflateException: XML file /[blah]/build/res/all/debug/layout/activity_main.xml line #-1 (sorry, not yet implemented): Error inflating class fragment
引起:
java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 4030500 but found 0. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
问题是我的清单中已经有了这一行。以下是一些相关文件:
的AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bryansills.studywithfriends"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:logo="@drawable/ic_action_icon"
android:label="@string/app_name"
android:theme="@style/study">
<activity
android:name="com.bryansills.studywithfriends.MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_maps_api_key_v2"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
</application>
</manifest>
的build.gradle:
buildscript {
repositories {
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
classpath 'com.squareup.gradle:gradle-android-test-plugin:0.9.1-SNAPSHOT'
}
}
apply plugin: 'android'
apply plugin: 'android-test'
repositories {
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 14
targetSdkVersion 18
}
}
dependencies {
compile 'com.android.support:support-v4:+'
compile 'com.google.android.gms:play-services:4.0.+'
testCompile 'junit:junit:4.11'
testCompile 'org.robolectric:robolectric:2.3-SNAPSHOT'
testCompile 'com.squareup:fest-android:1.0.+'
}
MainActivityTest.java:
package com.bryansills.studywithfriends;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.ActionProvider;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import com.bryansills.studywithfriends.MainActivity;
import dalvik.annotation.TestTargetClass;
import org.junit.Test;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.shadows.ShadowToast;
import org.robolectric.tester.android.view.TestMenuItem;
import com.bryansills.studywithfriends.RobolectricGradleTestRunner;
import java.lang.*;
import java.lang.CharSequence;
import java.lang.Exception;
import java.lang.Override;
import java.lang.System;
import static org.junit.Assert.assertTrue;
@RunWith(RobolectricGradleTestRunner.class)
public class MainActivityTest {
private MainActivity mainActivity;
private DrawerLayout drawer;
private MenuItem settingsMenuItem;
private MenuItem helpMenuItem;
@Before
public void setUp() throws Exception {
mainActivity = Robolectric.buildActivity(MainActivity.class).create().visible().get();
drawer = (DrawerLayout) mainActivity.findViewById(R.id.drawer);
settingsMenuItem = new TestMenuItem(R.id.settings);
helpMenuItem = new TestMenuItem(R.id.help);
}
@Test
public void testTrue() throws Exception {
assertTrue(true);
}
@Test
public void testActivity() throws Exception {
assertTrue(mainActivity != null);
}
@Test
public void settingsMenuItemShouldStartSettingsActivity() throws Exception {
mainActivity.onOptionsItemSelected(settingsMenuItem);
assertTrue(ShadowToast.getTextOfLatestToast().equals("Settings"));
}
@Test
public void helpMenuItemShouldStartSettingsActivity() throws Exception {
mainActivity.onOptionsItemSelected(helpMenuItem);
assertTrue(ShadowToast.getTextOfLatestToast().equals("Help"));
}
}
答案 0 :(得分:7)
注意:首先请务必确保在清单中添加Api密钥在here中创建后。
从Manifest中删除旧样式permission
。删除给定的权限(如果存在)。
<permission android:name ="com.example.locations.permission.MAPS_RECEIVE" android:protectionLevel="signature"></permission>
Google Map V2为清单添加新功能。将给定代码添加到清单..
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />// if this integer is not showing Add latest google play services
除了
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="ENTER_YOUR_API_KEY_HERE" />
请勿忘记在manifest
中输入 API密钥。
同时删除此..
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
答案 1 :(得分:0)
通过从Robolectric 2.1升级到2.3-SNAPSHOT(我没有测试2.2),这对我来说是固定的。但是,我确实遇到了另一个问题:
java.lang.IllegalStateException:应用程序的AndroidManifest.xml中的元数据标记没有正确的值。预期4030500但找到2131361793.您必须在元素中包含以下声明:&lt; meta-data android:name =“com.google.android.gms.version”android:value =“@ integer / google_play_services_version”/&gt; < / p>
这似乎是different bug,我通过将GMS版本硬编码到我的Manifest而不是引用它来“修复”。这可能不是一个好主意,但在错误修复之前,它才有效......
答案 2 :(得分:0)
您需要在AndroidManifest.xml
添加以下内容:
<application>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>