Android vectorDrawables.useSupportLibrary = true正在停止应用

Ale*_*der 24 android android-appcompat android-support-library android-vectordrawable

如果我在gradle中使用vectorDrawables.useSupportLibrary = true,那么不幸地运行它停止的应用程序.如果我删除vectorDrawables.useSupportLibrary = true,该应用程序将起作用.

我的朋友:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        vectorDrawables.useSupportLibrary = true
        applicationId "com.helikanon.firstapp"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

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

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.android.support:design:25.1.0'
    compile 'com.jakewharton:butterknife:8.4.0'
    compile 'com.android.support:support-v4:25.1.0'
    testCompile 'junit:junit:4.12'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
    compile 'uk.co.chrisjenx:calligraphy:2.2.0'
    compile 'com.google.android.gms:play-services-ads:10.0.1'
}
Run Code Online (Sandbox Code Playgroud)

错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.helikanon.firstapp/com.helikanon.firstapp.activities.MainActivity}: android.view.InflateException: Binary XML file line #62: Error inflating class Button
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
                      at android.app.ActivityThread.access$600(ActivityThread.java:130)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
                      at android.os.Handler.dispatchMessage(Handler.java:99)
                      at android.os.Looper.loop(Looper.java:137)
                      at android.app.ActivityThread.main(ActivityThread.java:4745)
                      at java.lang.reflect.Method.invokeNative(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:511)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                      at dalvik.system.NativeStart.main(Native Method)
                   Caused by: android.view.InflateException: Binary XML file line #62: Error inflating class Button
                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
.
.
.
Run Code Online (Sandbox Code Playgroud)

当我使用Api 16和Api 19时应用程序崩溃但是当我使用Api 23时它可以工作.

Vip*_*sri 30

除了pre-lollipop 之外,您不能在任何其他视图中使用Vector DrawablesImageView.

请参阅此SO答案由谷歌开发者倡导者.

对于AppCompat用户,由于版本23.2.0/23.2.1 [ https://code.google]中的实施中发现的问题,我们决定删除允许您使用Lollipop前设备上的资源使用矢量绘图的功能. com/p/android/issues/detail?id = 205236,https://code.google.com/p/android/issues/detail ?id = 204708 ].使用app:srcCompatsetImageResource()继续工作.

如果你想使用Vector Drawables pre-lollipop,使用可以通过将其转换为drawable来以编程方式设置它.

Drawable drawable;

if (android.os.Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
     drawable = context.getResources().getDrawable(drawableResId, context.getTheme());
} else {
     drawable = VectorDrawableCompat.create(context.getResources(), drawableResId, context.getTheme());
}

button.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
Run Code Online (Sandbox Code Playgroud)

  • `ContextCompat.getDrawable(context,R.drawable.a);`已经有了一个包装器. (3认同)
  • `ContextCompat.getDrawable()` 不支持矢量绘图,使用 AndroidX 提供的方法可以:`AppCompatResources.getDrawable(context, R.drawable.image)` (2认同)

小智 9

要使用 VectorDrawableCompat,您需要设置:

android.defaultConfig.vectorDrawables.useSupportLibrary = true
Run Code Online (Sandbox Code Playgroud)

要使用 VectorDrawableCompat,您需要对项目进行两次修改。首先,在 build.gradle 文件中设置 android.defaultConfig.vectorDrawables.useSupportLibrary = true,然后使用 app:srcCompat 而不是 android:src 来引用矢量可绘制对象。

  1. 转到您的build.gradle(模块:应用程序)并将以下行添加到 android 块。它应该是这样的。

    android {
        compileSdkVersion 27
        defaultConfig {
            applicationId "..."
            minSdkVersion 15
            targetSdkVersion 27
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
            vectorDrawables.useSupportLibrary = true // <------
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将源引用srcCompat以允许您的应用程序使用矢量图形。

    应用程序:srcCompat="@drawable/plane"

  • 如果“android.defaultConfig.”部分嵌套在“android { defaultConfig { } }”块中,则不需要再次指定它。 (3认同)

RBT*_*RBT 7

我当时在一个练习项目中,尝试学习Android中的后台任务和服务。当我提取他们的示例代码时,由于这个错误,最初它没有被编译。所以我在应用程序的build.gradle文件中添加了以下标记,以摆脱编译问题:

android {
    defaultConfig{
        vectorDrawables.useSupportLibrary = true
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,我添加此配置的那一刻,我的应用程序也开始完全按照OP的描述在Microsoft Android Simulator中崩溃。

由于我目前的重点是学习后台任务,因此我想开始调试我的应用程序。因此,我再次删除了上面的配置设置。除此之外我也删除命名的属性android:fillColorpath我的绘图资源之一的标签(或者你也可以替换值@color/colorAccent与十六进制代码等#FF000000)。删除(或使用十六进制代码进行更改)之后,我的初始编译错误没有出现:

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="80dp"
    android:height="80dp"
    android:viewportWidth="24"
    android:viewportHeight="24">

    <path
        android:fillColor="@color/colorAccent"
        android:pathData="M16.01 7L16 3h-2v4h-4V3H8v4h-0.01C7 6.99 6 7.99 6 8.99v5.49L9.5
18v3h5v-3l3.5-3.51v-5.5c0-1-1-2-1.99-1.99z" />
    <path
        android:pathData="M0 0h24v24H0z" />
</vector>
Run Code Online (Sandbox Code Playgroud)

删除后,它看起来像:

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="80dp"
    android:height="80dp"
    android:viewportWidth="24"
    android:viewportHeight="24">

    <path
        android:pathData="M16.01 7L16 3h-2v4h-4V3H8v4h-0.01C7 6.99 6 7.99 6 8.99v5.49L9.5
18v3h5v-3l3.5-3.51v-5.5c0-1-1-2-1.99-1.99z" />
    <path
        android:pathData="M0 0h24v24H0z" />
</vector>
Run Code Online (Sandbox Code Playgroud)

我的MS Android模拟器安装了Kitkat(4.4)。正如我在Vipul接受的答案中提到的那样,我的应用程序试图在棒棒糖之前的Android版本上使用矢量可绘制对象,因此代码崩溃了。


nor*_*DEV 5

如果您的 minSdkVersion 21 或更高版本,则不需要它。

Android 5.0 正式支持矢量绘图。

https://developer.android.com/guide/topics/graphics/vector-drawable-resources

更新:正如 StuStirling 所提到的,在 API24 (Android 7.0) 以下,如果您在矢量文件中引用其他资源,则需要它。

更新:

如果您删除了vectorDrawables.useSupportLibrary = true从向量中分析生成 png 的 APK。如果您发现 ex:xxxhdpi-v4在可绘制对象中放回 useSupportLibrary 行。(我没有从向量中引用其他可绘制对象)

  • 严格来说并不正确。要在 &lt; 24 上引用矢量文件中的其他资源,您需要添加 `useSupportLibrary = true` (4认同)