android.support.v7.widget.Toolbar VectorDrawableCo

2019-07-13 01:02发布

问题:

I've started a new project in Android Studio and configured my gradle build settings. When I add an android.support.v7.widget.Toolbar to a layout, I get the following error when in design view. Keep note that this error only shows up when previewing with an API less than 21 (specifically APIs 16 - 19). Everything works fine when I preview with API 21 - 24.

I have looked at a number of solutions and tried them, but none have solved the issue. One thing I tried doing was adding this to my gradle:

vectorDrawables.useSupportLibrary = true  

But that didn't solve the issue. I tried configuring my compiledSdkVersion to 23, buildToolsVersion to 23.0.3. This did not change anything. If I change the support library versions to 23.4.0 or below, then the issue is resolved. As soon as I change it back up to 24.0.0 or higher then it returns.

Clearing the cache, cleaning the project and rebuilding, and invalidating the cache and restarting did not solve the issue.

The following classes could not be instantiated:
- android.support.v7.widget.Toolbar (Open Class, Show Exception, Clear Cache)

Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when in the IDE

Exception Details
java.lang.IllegalStateException: This app has been built with an incorrect configuration.
Please configure your build for VectorDrawableCompat.
at android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawableManager.java:692)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:186)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:181)
at android.support.v7.widget.TintTypedArray.getDrawable(TintTypedArray.java:67)
at android.support.v7.widget.Toolbar.<init>(Toolbar.java:298)
at android.support.v7.widget.Toolbar.<init>(Toolbar.java:229)
...

invalid drawable tag vector

Here is my app build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1" // https://developer.android.com/studio/releases/build-tools.html

    defaultConfig {
        applicationId "org.path.path"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 0
        versionName "0.0.0"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    ext {
        supportLibVersion = '24.0.0' // https://developer.android.com/topic/libraries/support-library/revisions.html
    }

    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile "com.android.support:appcompat-v7:${supportLibVersion}"
    compile "com.android.support:design:${supportLibVersion}"
    compile "com.android.support:cardview-v7:${supportLibVersion}"
    compile "com.android.support:recyclerview-v7:${supportLibVersion}"
}

My project build.gradle is using classpath 'com.android.tools.build:gradle:2.1.2'

This is the toolbar xml:

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar_main"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:minHeight="?attr/actionBarSize"
android:elevation="4dp"
android:background="@color/colorPrimary"
app:theme="@style/ActionBarThemeOverlay"
app:popupTheme="@style/ActionBarPopupThemeOverlay"/>

And finally, my styles.xml:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<!-- ActionBar -->
<style name="ActionBarPopupThemeOverlay" parent="ThemeOverlay.AppCompat.Dark" />
<style name="ActionBarThemeOverlay" parent="ThemeOverlay.AppCompat.Dark">
    <item name="android:textColorPrimary">@color/text_on_primary</item>
    <item name="android:textColorSecondary">@color/subtitle_on_primary</item>
</style>

Is this a bug, or am I just not able to use the support library versions 24 or higher? I'm not aware of any other configurations to change as the exception tried to direct.

回答1:

Check this issue:

appcompat-v7 24.0.0 is incompatible with rasterized vectors



回答2:

Make sure you are using gradle version greater than 2.0

buildscript {
  ...
  dependencies {
    classpath 'com.android.tools.build:gradle:2.1.0'
  }
}