Toolbar: IllegalStateException - configure your build for VectorDrawableCompat

老子叫甜甜 提交于 2019-12-05 06:02:30

This worked well for me

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
    applicationId "com.example.app"
    minSdkVersion 15
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    generatedDensities = []
}

// This is handled for you by the 2.0+ Gradle Plugin
aaptOptions {
    additionalParameters "--no-version-vectors"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

}

Notice this in the above code:

// This is handled for you by the 2.0+ Gradle Plugin
aaptOptions {
    additionalParameters "--no-version-vectors"
}

and

generatedDensities = []

from this google Issue

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

check this issue for more help.

UPDATE

configured your app build.gradle file as follows

android {
  defaultConfig {
    vectorDrawables.useSupportLibrary = true
  }
}

dependencies {
  compile 'com.android.support:appcompat-v7:23.3.0'
}

Source Age Of Vectors

AnV

Firstly, there are two types of gradle files.

  1. build.gradle(Project: ProjectName)
  2. build.gradle(Module: app)

For more information on these two files, go through this answer.

Coming to your question,

I've found a lot of answers on the net but none helped. I'm using Android Studio 2.1.2, my activity extends AppCompatActivity and added to the gradle (2 files) every thing I found relevant: but still the error appears.

From whatever you have posted, it seems like you didn't add right things at right places.

You shouldn't place your application dependencies in build.gradle(Project: ProjectName) - Android Studio says,

// NOTE: Do not place your application dependencies here; they belong

// in the individual module build.gradle files

Hence, replace your dependencies in build.gradle(Project: ProjectName) with below

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

Replace your dependencies in build.gradle(Module: app) with below

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.1.1'
    compile 'com.android.support:design:24.1.1'
}

After doing as mentioned above, you will see a "Sync Now" option on the Top Right. Click it. Android Studio will take care of remaining things.

Also this question similar is to yours. Go through it. It may help.

Add :

build.gradle

defaultConfig {
    ...
    vectorDrawables.useSupportLibrary = true
    ...
}

And on top of your Activity class:

static {
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}

And one last thing put your vector drawables inside any other drawables like selector, LayerList, etc. Something like below:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/drawable_vector" />
</selector>

And use this above drawable everywhere instead of setting vector drawables directly. This will at other places as well whenever you want to use vector drawables on pre-lollipop devices.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!