1

I am attempting to write an instrumentation test for my Android app. When I run my test, and it hits the section of my code that attempts to use the com.google.android.gms.vision.face.FaceDetector library, the test framework throws the follow error:

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" />

However, my AndroidManifest.xml already does contain that exact meta data tag, right here:

<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" />

What am I missing? My instrumented test looks something like this:

@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);
    }
}

EDIT: adding 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'
}

Any help is appreciated. I have already found a miriad of SO answers that all say add the meta-data tag to the manifest (this one, for example), however I already have it added and the test still will not run past the FaceDetector library.

4

1 回答 1

1

Found the issue

The test runner context does not have a manifest file. I needed to pass the correct context into the method i was testing. My test now looks like this (note the swap to getTargetContext):

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

FaceDetector was able to be initialized. Now I can finish actually implementing my test. Hopefully this helps someone.

Props to this random answer, the list of context's is what helped me.

于 2016-08-03T23:08:43.447 回答