【问题标题】:Android instrumentation test errors due to com.google.android.gms.version meta data由于 com.google.android.gms.version 元数据导致的 Android 仪器测试错误
【发布时间】:2016-08-03 21:06:33
【问题描述】:

我正在尝试为我的 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 库。

【问题讨论】:

  • 不要添加google_play_services_version。它将通过 google play services AAR 自动添加。
  • @JaredBurrows 你能澄清一下吗?我已尝试完全删除 标记,也尝试仅删除“android:value”属性,在这两种情况下,测试运行时我仍然收到相同的错误。
  • 显示 build.gradle。
  • @JaredBurrows 添加了 build.gradle

标签: android unit-testing testing android-camera android-vision


【解决方案1】:

发现问题

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

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

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

支持this random answer,上下文列表对我有帮助。

【讨论】:

  • 很好,看来你已经明白了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-22
  • 1970-01-01
  • 2019-01-20
  • 1970-01-01
  • 2021-10-01
相关资源
最近更新 更多