Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Robolectric and vector drawables

I am using some vector drawables in my app but only for v21 and up - they are in the resource folder drawable-anydpi-v21 and also have fallback bitmap versions for the other api levels (drawable-hdpi.mdpi,...).

When I run a robolectric with this config

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

I get the following error on inflate of the views using these 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

the relevant parts of the 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 looks as though even though i have specified sdk=16 Robolectric seems to take the drawables from drawable-anydpi-v21.

  1. Is this a bug is roboelectric? or

  2. Is there a better way to specify what the APK level is? or

  3. Is there a way to let roboelectric read the vector tag? or

  4. Some other way of doing it?

like image 391
siliconeagle Avatar asked Jun 28 '16 10:06

siliconeagle


People also ask

What is vector drawable in Android?

A VectorDrawable is a vector graphic defined in an XML file as a set of points, lines, and curves along with its associated color information. The major advantage of using a vector drawable is image scalability.

What is not an advantage of using vector Drawables?

The drawback is that they might take longer to draw, etc since there's more of parsing and drawing going on than just using a bitmap. This should although be neglectable if you keep your vectors simple.

What is vector asset in Android studio?

Android Studio includes a tool called Vector Asset Studio that helps you add material icons and import Scalable Vector Graphic (SVG) and Adobe Photoshop Document (PSD) files into your project as vector drawable resources.

Which class do you use to create a vector drawable?

VectorDrawable Class (Android. Graphics. Drawables) | Microsoft Learn.


1 Answers

Do you specifically require your tests to target JELLYBEAN?

Given you do specifically require your tests to target JELLYBEAN, you may want to put your v21+ assets in the res/drawable-v21 folder instead of res/drawable-anydpi-21.

I too was recently getting the same error with tests after adding an ImageView to a layout that uses a 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 get my tests to pass again with the following config annotation:

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

Hope this helps.

like image 95
abest Avatar answered Oct 02 '22 06:10

abest