安卓Robolectric与矢量图形可伸缩性的问题

8
我正在我的应用中使用一些矢量图形,但只限于 v21 及以上版本 - 它们在资源文件夹 drawable-anydpi-v21 中,并且还有其他 API 级别的备用位图版本 (drawable-hdpi.mdpi,...)。
当我运行一个具有此配置的 Robolectric 时,...
@Config(sdk = 16, application = MyApp.class, constants = BuildConfig.class, packageName = "com.company.app")

当使用这些可绘制对象时,膨胀视图时出现以下错误

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中获取可绘制对象。

  1. 这是Robolectric的一个bug吗?还是

  2. 有更好的方法来指定APK的级别吗?或者

  3. 有办法让Robolectric读取矢量标记吗?还是

  4. 还有其他方法吗?


你能展示一下你的 build.gradle 文件吗?以确保你如何使用矢量图形。 - Eugen Martynov
build.gradle 已添加 - siliconeagle
糟糕!实际上那就是我原本想要的,但我删错了行。我已经更新了它。 - siliconeagle
1
如果问题已经解决,请写下您的答案。 - Eugen Martynov
你能试试使用Robolectric 3.2-SNAPSHOT吗? - Steve C
显示剩余5条评论
2个回答

3
您是否需要您的测试专门针对 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")

希望这能帮到您。

0
你可以做一件事情。拿RoboElectric的源代码,然后替换所有的行。
ContextCompat.getDrawable(context, drawableId)

使用

AppCompatDrawableManager.get().getDrawable(context, drawableId)

编译Robolectric并使用它。这将允许Robolectric使用向量。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接