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.

  • Is this a bug is roboelectric? or

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

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

  • Some other way of doing it?


  • 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.


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

    ContextCompat.getDrawable(context, drawableId)
    

    with

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

    Compile roboelectric and use it. It will allow roboelectric to use vectors.

    链接地址: http://www.djcxy.com/p/34972.html

    上一篇: 有没有办法来改善在Visual Studio代码中突出显示的错误?

    下一篇: Android Robolectric和矢量绘图