我遇到了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")); } }
解决方法
注意:首先安静地将清单中的Api键添加到
here中.
<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" />
不要忘记在清单中输入API密钥.
也删除这个..
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>