Android 由于 com.google.android.gms.version 元数据导致的仪器测试错误

Android instrumentation test errors due to com.google.android.gms.version meta data

我正在尝试为我的 Android 应用程序编写仪器测试。当我 运行 我的测试,它命中了我的代码中试图使用 com.google.android.gms.vision.face.FaceDetector 库的部分时,测试框架抛出以下错误:

A required meta-data tag in your app's AndroidManifest.xml does not exist.  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 已经包含那个确切的元数据标签,就在这里:

<application
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@drawable/icon"
        android:label="NeuralEye-FaceID-2"
        android:theme="@style/Theme.AppCompat"
        android:largeHeap="true">
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.gms.vision.DEPENDENCIES"
            android:value="face" />

我错过了什么?我的仪器测试看起来像这样:

@RunWith(AndroidJUnit4.class)
public class FeatureExtractionTest {


    public FeatureExtraction mFeatureExtraction;

    public ConcurrentSkipListSet<String> skipList = new ConcurrentSkipListSet();

    @Before
    public void createFeatureExtraction() {
        mFeatureExtraction = new FeatureExtraction();
    }

    @Test
    public void testGetFeatures() throws Exception {
        File file = new File("new.txt");
        Context ctx = InstrumentationRegistry.getContext();
        mFeatureExtraction.getFeatures(file, skipList, ctx);
    }
}

编辑:添加 build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "tuan.search"
        minSdkVersion 21
        targetSdkVersion 23
        versionCode 1
        versionName "0.5 "
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile files('libs/opencsv-3.7.jar')
    compile 'com.android.support:support-v4:23.4.0'
    compile 'com.google.android.gms:play-services:9.0.1'
    compile 'com.android.support:design:23.4.0'
    compile 'org.projectlombok:lombok:1.16.8'
    compile 'com.android.support:multidex:1.0.0'
    compile 'org.apache.directory.studio:org.apache.commons.io:2.4'
    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-core:1.10.19'
    androidTestCompile 'com.android.support.test:runner:0.4'
    androidTestCompile 'com.android.support.test:rules:0.4'
    androidTestCompile 'com.android.support:support-annotations:23.4.0'
    androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
}

感谢任何帮助。我已经找到了无数的 SO 答案,都说将元数据标记添加到清单中(this one, for example),但是我已经添加了它并且测试仍然不会 运行 通过 FaceDetector 库.

找到问题

测试运行器上下文没有清单文件。我需要将正确的上下文传递到我正在测试的方法中。我的测试现在看起来像这样(注意交换到 getTargetContext):

@Test
public void testGetFeatures() throws Exception {
    File file = new File("new.txt");
    Context ctx = InstrumentationRegistry.getTargetContext();
    mFeatureExtraction.getFeatures(file, skipList, ctx);
}

FaceDetector 可以初始化。现在我可以完成实际实施我的测试了。希望这对某人有所帮助。

的支持,上下文列表对我有帮助。