2016-06-28 135 views
7

我在我的應用程序中使用了一些矢量繪圖,但僅適用於v21及更高版本 - 它們位於資源文件夾drawable-anydpi-v21中,並且還具有用於其他api級別的回退位圖版本(drawable-hdpi .mdpi,...)。Android Robolectric和矢量繪圖

當我運行一個robolectric這個配置

@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似乎t從drawable-anydpi-v21中獲取drawables。

  1. 這是一個bug是roboelectric?或者

  2. 有更好的方法來指定APK級別是什麼嗎?或

  3. 有沒有辦法讓船電閱讀矢量標籤?或

  4. 其他一些做法呢?

+0

你可以顯示你的'build.gradle'嗎?爲了確保你如何使用矢量繪圖 –

+0

build.gradle添加 – siliconeagle

+0

它不工作像這樣你有'defaultConfig vectorDrawables.useSupportLibrary = true }'? –

回答

0

你可以做一件事。就拿RoboElectric的源和

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

編譯roboelectric全部更換線路

ContextCompat.getDrawable(context, drawableId) 

,並使用它。它將允許輪船使用載體。

3

您是否明確要求您的測試目標爲JELLYBEAN

鑑於你明確要求你的測試目標傑利貝恩,你可以把它放到你的V21 +資產在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") 

希望這有助於。