问题
I'm using Android Studio 2.1.2. I started a new project with minSdkVersion as 19. My activity extends AppCompatActivity. The project starts with an empty activity using a fragment.
When previewing content_main.xml with API 24, all is good. when previewing API 19, I get the following rendering problem:
The following classes could not be instantiated:
- android.support.v7.widget.Toolbar
java.lang.IllegalStateException: This app has been built with an incorrect configuration. Please configure your build for VectorDrawableCompat
I have added every thing I found relevant to the gradle (2 files):
classpath 'com.android.tools.build:gradle:2.1.2'
**vectorDrawables.useSupportLibrary = true**
buildToolsVersion "24.0.1"
compile 'com.android.support:appcompat-v7:24.1.1'
**compile "com.android.support:support-v4:24.1.1"**
compile 'com.android.support:design:24.1.1
But still the error appears. I've found a lot of answers on internet. But none helped. Is there a problem using the new toolbar with API 19?
回答1:
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 = []
回答2:
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
回答3:
Firstly, there are two types of gradle files.
build.gradle(Project: ProjectName)
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.
回答4:
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.
来源:https://stackoverflow.com/questions/38869556/toolbar-illegalstateexception-configure-your-build-for-vectordrawablecompat