I am running into the issue where Robolectric is having trouble with my AndroidManifest.xml. My app runs fine, but Robolectric is failing all my tests, even assertTrue(true)
. The error is:
android.view.InflateException: XML file /[blah]/build/res/all/debug/layout/activity_main.xml line #-1 (sorry, not yet implemented): Error inflating class fragment
caused by:
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" />
The problem is that I already have that line in my manifest. Below are some of the relevant files:
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"));
}
}
Note : First be quiet sure to add the Api key in the manifest After creating it in here.
Remove the old style permission
from the Manifest. Remove given permission if exists.
<permission android:name ="com.example.locations.permission.MAPS_RECEIVE" android:protectionLevel="signature"></permission>
Google Map V2 add new feature to manifest. Add given code to the manifest..
<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
besides
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="ENTER_YOUR_API_KEY_HERE" />
Don't forget to enter API key in manifest
.
Also remove this..
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>