Android Robolectric和矢量drawables

前端之家收集整理的这篇文章主要介绍了Android Robolectric和矢量drawables前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的应用程序中使用了一些矢量drawables但仅适用于v21及更高版本 – 它们位于资源文件夹drawable-anydpi-v21中,并且还具有其他api级别的回退位图版本(drawable-hdpi.mdpi,…).

当我使用此配置运行robolectric时

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

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

  1. 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
  2. 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的相关部分是:

  1. android {
  2. compileSdkVersion 23
  3. buildToolsVersion "23.0.3"
  4. defaultConfig {
  5. applicationId "com.example.app"
  6. minSdkVersion 16
  7. targetSdkVersion 23
  8. versionCode 79
  9. versionName "0.39"
  10. // Enabling multidex support.
  11. multiDexEnabled true
  12. vectorDrawables.useSupportLibrary = true
  13.  
  14. testApplicationId "com.example.app.test"
  15. testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  16. }
  17. testOptions {
  18. unitTests.returnDefaultValues = true
  19. }
  20. }
  21. dependencies {
  22. compile 'com.android.support:support-vector-drawable:23.4.0'
  23. testCompile "org.robolectric:robolectric:3.1"
  24. testCompile "org.robolectric:shadows-multidex:3.1"
  25. testCompile "org.robolectric:shadows-support-v4:3.1"
  26. }

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

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

解决方法

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

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

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

  1. <ImageView
  2. android:contentDescription="@string/content_image_description"
  3. android:src="@drawable/banner"
  4. android:layout_gravity="right"
  5. android:layout_width="@dimen/banner_width"
  6. android:layout_height="@dimen/banner_height"
  7. />

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

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

希望这可以帮助.

猜你在找的Android相关文章