Android Robolectric和矢量drawables

sil*_*gle 8 android robolectric android-vectordrawable

我在我的应用程序中使用了一些矢量drawables但仅适用于v21及更高版本 - 它们位于资源文件夹drawable-anydpi-v21中,并且还具有其他api级别的回退位图版本(drawable-hdpi.mdpi,...).

当我使用此配置运行robolectric时

@Config(sdk = 16, application = MyApp.class, constants = BuildConfig.class, packageName = "com.company.app")
Run Code Online (Sandbox Code Playgroud)

使用这些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
Run Code Online (Sandbox Code Playgroud)

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"
   }
Run Code Online (Sandbox Code Playgroud)

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

  1. 这是一个roboelectric的错误吗?要么

  2. 有没有更好的方法来指定APK级别是什么?要么

  3. 有没有办法让roboelectric读取矢量标签?要么

  4. 其他一些方法呢?

Emb*_*ous 0

你可以做一件事。取RoboElectric的源码并替换所有线路

ContextCompat.getDrawable(context, drawableId)
Run Code Online (Sandbox Code Playgroud)

AppCompatDrawableManager.get().getDrawable(context, drawableId)
Run Code Online (Sandbox Code Playgroud)

编译roboelectric并使用它。它将允许机器人电力使用矢量。