【问题标题】:Android Robolectric and vector drawablesAndroid Robolectric 和矢量绘图
【发布时间】:2016-06-28 10:54:45
【问题描述】:

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

当我使用此配置运行机器人时

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

我在使用这些可绘制对象膨胀视图时收到以下错误

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

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

所以看起来即使我指定了 sdk=16 Robolectric 似乎也从 drawable-anydpi-v21 中获取了可绘制对象。

  1. 这是一个机器人电动虫吗?或

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

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

  4. 还有其他方法吗?

【问题讨论】:

  • 你能显示你的build.gradle吗?确定如何使用矢量绘图
  • 已添加build.gradle
  • 这样不行你有defaultConfig { vectorDrawables.useSupportLibrary = true } 吗?
  • 哇!实际上这就是我所拥有的,但我删除了错误的行。我已经更新了
  • 解决了吗,写下你的答案

标签: android robolectric android-vectordrawable


【解决方案1】:

您是否特别要求您的测试针对 JELLYBEAN

鉴于您确实要求您的测试针对 JELLYBEAN,您可能希望将 v21+ 资产放在res/drawable-v21 文件夹中而不是 res/drawable-anydpi-21。 p>

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

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

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

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

希望这会有所帮助。

【讨论】:

    【解决方案2】:

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

    ContextCompat.getDrawable(context, drawableId)
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-24
      • 2018-04-18
      • 2018-10-31
      • 1970-01-01
      • 2016-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多