gpt4 book ai didi

Android Robolectric 和矢量绘图

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:57:27 24 4
gpt4 key购买 nike

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

当我使用此配置运行 robolectric 时

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

我在使用这些可绘制对象扩展 View 时遇到以下错误

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. 这是 roboelectric 的 bug 吗?或者

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

  3. 有没有办法让roboelectric读取vector标签?或者

  4. 还有其他方法吗?

最佳答案

您是否特别要求您的测试以 JELLYBEAN 为目标?

鉴于您确实特别要求您的测试以 JELLYBEAN 为目标,您可能希望将 v21+ Assets 放在 res/drawable-v21 文件夹中而不是 res/drawable-anydpi-21 的强>。

在将 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")

希望这对您有所帮助。

关于Android Robolectric 和矢量绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38074260/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com