我在我的应用程序中使用了一些矢量可绘制对象,但仅用于v21及更高版本-它们位于资源文件夹drawable-anydpi-v21中,并且具有其他api级别的后备位图版本(drawable-hdpi.mdpi,...)。

当我使用此配置运行robolectric

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

我在使用这些drawable的 View 膨胀时收到以下错误
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中获取了drawable。
  • 这是robotelectric的错误吗?或
  • 是否有更好的方法来指定APK级别是多少?或
  • 有没有办法让roboelectric读取矢量标签?或
  • 还有其他方法吗?
  • 最佳答案

    您是否特别需要针对 JELLYBEAN 的测试?

    鉴于您确实确实需要针对 JELLYBEAN 进行测试,因此您可能希望将v21 + Assets 放置在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 Robolectric和矢量可绘制对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38074260/

    10-16 22:17