Android Robolectric和矢量drawables

我在我的应用程序中使用了一些矢量drawables但仅适用于v21及更高版本 – 它们位于资源文件夹drawable-anydpi-v21中,并且还具有其他api级别的回退位图版本(drawable-hdpi.mdpi,…).

当我使用此配置运行robolectric时

@Config(sdk = 16,application = MyApp.class,constants = BuildConfig.class,packageName = "com.company.app")

我使用这些drawables对视图的膨胀产生以下错误

Caused by: android.content.res.Resources$NotFoundException: File ./app/build/intermediates/data-binding-layout-out/dev/debug/drawable-anydpi-v21/ic_info_outline_white_24dp.xml from drawable resource ID #0x7f02010e
Caused by: org.xmlpull.v1.XmlPullParserException: XML file ./app/build/intermediates/data-binding-layout-out/dev/debug/drawable-anydpi-v21/ic_info_outline_white_24dp.xml line #-1 (sorry,not yet implemented): invalid drawable tag vector

build.gradle的相关部分是:

android {
      compileSdkVersion 23
      buildToolsversion "23.0.3"
      defaultConfig {
        applicationId "com.example.app"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 79
        versionName "0.39"
        // Enabling multidex support.
        multiDexEnabled true
        vectorDrawables.useSupportLibrary = true

        testApplicationId "com.example.app.test"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
      }
      testOptions {
        unitTests.returnDefaultValues = true
      }
   }
   dependencies {
    compile 'com.android.support:support-vector-drawable:23.4.0'
    testCompile "org.robolectric:robolectric:3.1"
    testCompile "org.robolectric:shadows-multidex:3.1"
    testCompile "org.robolectric:shadows-support-v4:3.1"
   }

所以即使我已经指定了sdk = 16,Robolectric似乎也从drawable-anydpi-v21中获取了drawables.

>这是一个roboelectric的错误吗?要么
>有没有更好的方法来指定APK级别是什么?要么
>有没有办法让roboelectric读取矢量标签?要么
>其他一些方法呢?

解决方法

您是否特别要求您的测试以JELLYBEAN为目标?

鉴于您确实要求您的测试以JELLYBEAN为目标,您可能希望将v21资源放在res / drawable-v21文件夹而不是res / drawable-anydpi-21中.

我最近在将ImageView添加到使用VectorDrawable作为其源的布局后,也遇到了与测试相同的错误.

<ImageView
    android:contentDescription="@string/content_image_description"
    android:src="@drawable/banner"
    android:layout_gravity="right"
    android:layout_width="@dimen/banner_width"
    android:layout_height="@dimen/banner_height"
    />

使用robolectric v3.1,我能够通过以下配置注释让我的测试再次通过:

@Config(constants = BuildConfig.class,sdk = Build.VERSION_CODES.LOLLIPOP,packageName = "com.package")

希望这可以帮助.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...