Android Robolectric and Vector Drawings

I use some vector drawings in my application, but only for v21 and higher - they are in the drawable-anydpi-v21 resources folder, and also have versions of rollback bitmaps for other api levels (drawable-hdpi.mdpi, ...).

When I run robolectric with this configuration

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

I get the following error when inflating views with these drawings

 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 

The relevant parts of build.gradle are:

  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" } 

So, it seems that although I specified sdk = 16, Robolectric seems to be taking the drawings from drawable-anydpi-v21.

  • Is this a roboelectric bug? or

  • Is there a better way to indicate what the level of the APK is? or

  • Is there a way for roboelectric to read a vector tag? or

  • Any other way to do this?

+7
android robolectric android-vectordrawable
source share
2 answers

Do you specifically require your tests to target JELLYBEAN ?

If you specifically require your tests to target JELLYBEAN , you can put your v21 + resources in the res/drawable-v21 instead of res/drawable-anydpi-21 .

I also recently got the same test error after adding ImageView to a layout that uses VectorDrawable as its source.

 <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" /> 

Using robolectric v3.1, I was able to run my tests again using the following configuration annotation:

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

Hope this helps.

+3
source share

You can do one thing. Take the source of RoboElectric and replace all rows

 ContextCompat.getDrawable(context, drawableId) 

from

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

Compile roboelectric and use it. This will allow the use of roboelectric vectors.

0
source share

All Articles