将 vectorDrawables 与 androidx 一起使用的更新

update for using vectorDrawables with androidx

The solution given in this documentation page 似乎对 androidx 用户无效(我认为这是因为提到 android.support.v7.appcompat 没有被 androidx 用户使用,对吧?) .我可能弄错了,但添加了:vectorDrawables.useSupportLibrary = true 还有

static{
            AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
        }  

与 xml 的 app:srcCopmat

<ImageButton
                android:id="@+id/imageButton4"
                android:layout_width="26dp"
                android:layout_height="0dp"
                android:layout_marginEnd="8dp"
                app:layout_constraintBottom_toBottomOf="@+id/imageButton5"
                app:layout_constraintEnd_toStartOf="@+id/imageButton5"
                app:layout_constraintTop_toTopOf="@+id/imageButton5"
                app:srcCompat="@drawable/sync" />

没有帮助。它只会使应用程序崩溃。 logcat 显示:

file: /sys/class/power_supply/battery/hw_switch_point open error
E/dalvikvm(11099): Could not find class 'com.crashlytics.android.ndk.CrashlyticsNdk', referenced from method com.crashlytics.android.CrashlyticsInitProvider.ˊ
E/dalvikvm(11099): Could not find class 'dalvik.system.InMemoryDexClassLoader', referenced from method com.facebook.ads.internal.dynamicloading.DynamicLoaderFactory.createInMemoryClassLoader
E/dalvikvm(11099): Could not find class 'com.facebook.ads.internal.dynamicloading.DynamicLoaderImpl', referenced from method com.facebook.ads.internal.dynamicloading.DynamicLoaderFactory.doMakeLoader
E/dalvikvm(11099): Could not find class 'androidx.work.impl.background.systemjob.SystemJobService', referenced from method androidx.appcompat.view.ʖ.createBestAvailableBackgroundScheduler
E/dalvikvm(11099): Could not find class 'androidx.appcompat.view.ΐ$ˊ', referenced from method androidx.appcompat.view.ΐ.<init>
E/dalvikvm(11099): Could not find class 'android.graphics.BlendModeColorFilter', referenced from method androidx.appcompat.view.y40$if.ˊ
E/dalvikvm(11099): Could not find class 'android.graphics.BlendModeColorFilter', referenced from method androidx.appcompat.view.y40$if.ˋ
E/dalvikvm(11099): Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method androidx.appcompat.view.t40.ˊ
E/dalvikvm(11099): Could not find class 'dalvik.system.DelegateLastClassLoader', referenced from method x.a
E/dalvikvm(11099): Could not find class 'dalvik.system.DelegateLastClassLoader', referenced from method nj.a
E/dalvikvm(11099): Could not find class 'com.evozi.network.service.QSTileService', referenced from method androidx.appcompat.view.ᚁ.ˋ
E/dalvikvm(11099): Could not find class 'com.evozi.network.service.QSTileService', referenced from method androidx.appcompat.view.ᚁ.ˋ
E/dalvikvm(11099): Could not find class 'com.evozi.network.service.QSTileService', referenced from method androidx.appcompat.view.ᚁ.ᐝ
E/dalvikvm(11099): Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method com.facebook.ads.redexgen.X.OY.A09
E/dalvikvm(11099): Could not find class 'android.app.ActivityManager$AppTask', referenced from method com.facebook.ads.redexgen.X.NN.A02
E/FBAudienceNetwork(11099): You are using custom Application class and don't call AudienceNetworkAds.isInAdsProcess(). Multi-process support will be disabled. Please call AudienceNetworkAds.isInAdsProcess() if you want to support multi-process mode.
E/memtrack(11189): Couldn't load memtrack module (No such file or directory)
E/android.os.Debug(11189): failed to load memtrack module: -2
E/PackageManager(  617): parse pkg : /data/app/vmdl-410864310.tmp
E/PackageManager(  617): dexopt start
E/Finsky  (10651): [1] aabj.b(2): Package name null is not an installed package
E/PackageManager(  617): dexopt end
E/SamsungIME(  907): mOCRHelper is null
E/SamsungIME(  907): mOCRHelper is null
E/memtrack(11247): Couldn't load memtrack module (No such file or directory)
E/android.os.Debug(11247): failed to load memtrack module: -2
E/jdwp    (11287): Failed sending reply to debugger: Broken pipe
E/dalvikvm(11287): Could not find class 'androidx.core.view.ViewCompat', referenced from method androidx.core.view.ViewCompat.addOnUnhandledKeyEventListener
E/dalvikvm(11287): Could not find class 'android.view.WindowInsets', referenced from method androidx.core.view.ViewCompat.dispatchApplyWindowInsets
E/dalvikvm(11287): Could not find class 'android.view.WindowInsets', referenced from method androidx.core.view.ViewCompat.onApplyWindowInsets
E/dalvikvm(11287): Could not find class 'android.view.View$OnUnhandledKeyEventListener', referenced from method androidx.core.view.ViewCompat.removeOnUnhandledKeyEventListener
E/dalvikvm(11287): Could not find class 'androidx.core.view.ViewCompat', referenced from method androidx.core.view.ViewCompat.setOnApplyWindowInsetsListener
E/AndroidRuntime(11287): FATAL EXCEPTION: main
E/AndroidRuntime(11287): Process: com.makeit.www, PID: 11287
E/AndroidRuntime(11287): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.makeit.www/com.makeit.www.StartupSetup.LogoAnimation}: android.view.InflateException: Binary XML file line #13: Error inflating class ImageView
E/AndroidRuntime(11287):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
E/AndroidRuntime(11287):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2378)
E/AndroidRuntime(11287):        at android.app.ActivityThread.access0(ActivityThread.java:155)
E/AndroidRuntime(11287):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
E/AndroidRuntime(11287):        at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(11287):        at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(11287):        at android.app.ActivityThread.main(ActivityThread.java:5433)
E/AndroidRuntime(11287):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(11287):        at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(11287):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
E/AndroidRuntime(11287):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
E/AndroidRuntime(11287):        at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(11287): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class ImageView
E/AndroidRuntime(11287):        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:720)
E/AndroidRuntime(11287):        at android.view.LayoutInflater.rInflate(LayoutInflater.java:762)
E/AndroidRuntime(11287):        at android.view.LayoutInflater.inflate(LayoutInflater.java:499)
E/AndroidRuntime(11287):        at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
E/AndroidRuntime(11287):        at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
E/AndroidRuntime(11287):        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
E/AndroidRuntime(11287):        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
E/AndroidRuntime(11287):        at com.makeit.www.StartupSetup.LogoAnimation.onCreate(LogoAnimation.java:22)
E/AndroidRuntime(11287):        at android.app.Activity.performCreate(Activity.java:5301)
E/AndroidRuntime(11287):        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
E/AndroidRuntime(11287):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2291)
E/AndroidRuntime(11287):        ... 11 more
E/AndroidRuntime(11287): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ic_launcher_background.xml from drawable resource ID #0x7f060064
E/AndroidRuntime(11287):        at android.content.res.Resources.loadDrawable(Resources.java:3451)
E/AndroidRuntime(11287):        at android.content.res.TypedArray.getDrawable(TypedArray.java:614)
E/AndroidRuntime(11287):        at android.widget.ImageView.<init>(ImageView.java:134)
E/AndroidRuntime(11287):        at androidx.appcompat.widget.AppCompatImageView.<init>(AppCompatImageView.java:72)
E/AndroidRuntime(11287):        at androidx.appcompat.widget.AppCompatImageView.<init>(AppCompatImageView.java:68)
E/AndroidRuntime(11287):        at androidx.appcompat.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:187)
E/AndroidRuntime(11287):        at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:107)
E/AndroidRuntime(11287):        at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1407)
E/AndroidRuntime(11287):        at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1457)
E/AndroidRuntime(11287):        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
E/AndroidRuntime(11287):        ... 21 more
E/AndroidRuntime(11287): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #2: invalid drawable tag vector
E/AndroidRuntime(11287):        at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:969)
E/AndroidRuntime(11287):        at android.graphics.drawable.Drawable.createFromXml(Drawable.java:913)
E/AndroidRuntime(11287):        at android.content.res.Resources.loadDrawable(Resources.java:3447)
E/AndroidRuntime(11287):        ... 30 more
E/android.os.Debug(  617): !@Dumpstate > sdumpstate -k -t -z -d -m 11287 -o /data/log/dumpstate_app_error
E/dalvikvm(11371): Could not find class 'android.app.job.JobScheduler', referenced from method com.sec.android.app.samsungapps.SamsungApps.b
E/dalvikvm(11371): Could not find class 'android.app.NotificationChannel', referenced from method com.sec.android.app.samsungapps.vlibrary.concreteloader.Common.getGeneralNotificationChannel
E/dalvikvm(11371): Could not find class 'android.app.NotificationChannel', referenced from method com.sec.android.app.samsungapps.vlibrary.concreteloader.Common.getPromotionNotificationChannel
E/dalvikvm(11371): Could not find class 'android.app.NotificationChannel', referenced from method com.sec.android.app.samsungapps.vlibrary.concreteloader.Common.getRewardsNotificationChannel
E/dalvikvm(11371): Could not find class 'android.app.NotificationChannel', referenced from method com.sec.android.app.samsungapps.vlibrary.concreteloader.Common.getUpdateNotificationChannel
E/Watchdog(  617): !@Sync 380
E/ActivityThread(11371): Failed to find provider info for null
E/ActivityThread(11371): Failed to find provider info for com.sec.android.app.clockpackage.celebvoice
E/[SAUI]  (11371): Firebase initialize is success
E/SPPClientService(11407): ShipBuild Binary : True
E/SPPClientService(11407): [PackageInfoChangeReceiver] android.intent.action.PACKAGE_REMOVED
E/SPPClientService(11407): [PackageInfoChangeReceiver] [handlePkgRemovedEvent] PackageName : com.makeit.www, true, false
E/SPPClientService(11407): [PackageInfoChangeReceiver] [handlePkgRemovedEvent] Ignore Replacing case

关于如何为 androidx 用户使用 Lollipop[=32= 下的版本vectorDrawables(或简单的矢量图像),稍微 运行 了解一下] 将是一个巨大的帮助。

编辑:XML 布局文件是:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main.MainScreenActivity">

    <LinearLayout
        android:id="@+id/Toolbar_holder"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        >


        <androidx.appcompat.widget.Toolbar
            android:id="@+id/app_toolbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/actionBarSize"
            android:textSize="@dimen/actionBarTextSize"
            android:background="@color/colorPrimary">
        </androidx.appcompat.widget.Toolbar>

        <View
            android:layout_width="match_parent"
            android:layout_height="8dp"
            android:background="@drawable/shadow_top_to_down" />


    </LinearLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/C_makeControlMain"
        android:layout_width="0dp"
        android:layout_height="227dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/C_makeControlHeader"
            android:layout_width="0dp"
            android:layout_height="46dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/fileTitle"
                android:layout_width="170dp"
                android:layout_height="21dp"
                android:layout_marginStart="8dp"
                android:layout_marginEnd="8dp"
                android:gravity="center"
                android:text="@string/fileTitle"
                app:layout_constraintEnd_toStartOf="@+id/imageButton3"
                app:layout_constraintStart_toEndOf="@+id/imageButton2"
                app:layout_constraintTop_toTopOf="parent" />

            <ImageButton
                android:id="@+id/imageButton"
                android:layout_width="34dp"
                android:layout_height="29dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="8dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/add_new_information" />

            <ImageButton
                android:id="@+id/imageButton2"
                android:layout_width="35dp"
                android:layout_height="0dp"
                android:layout_marginStart="8dp"
                app:layout_constraintBottom_toBottomOf="@+id/imageButton"
                app:layout_constraintStart_toEndOf="@+id/imageButton"
                app:layout_constraintTop_toTopOf="@+id/imageButton"
                app:srcCompat="@drawable/save_information" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="8dp"
                android:gravity="center"
                android:text="@string/artist_name"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="@+id/fileTitle"
                app:layout_constraintHorizontal_bias="0.513"
                app:layout_constraintStart_toStartOf="@+id/fileTitle"
                app:layout_constraintTop_toBottomOf="@+id/fileTitle" />

            <ImageButton
                android:id="@+id/imageButton3"
                android:layout_width="28dp"
                android:layout_height="0dp"
                android:layout_marginEnd="8dp"
                app:layout_constraintBottom_toBottomOf="@+id/imageButton4"
                app:layout_constraintEnd_toStartOf="@+id/imageButton4"
                app:layout_constraintTop_toTopOf="@+id/imageButton4"
                app:srcCompat="@drawable/add_normal_browser" />

            <ImageButton
                android:id="@+id/imageButton4"
                android:layout_width="26dp"
                android:layout_height="0dp"
                android:layout_marginEnd="8dp"
                app:layout_constraintBottom_toBottomOf="@+id/imageButton5"
                app:layout_constraintEnd_toStartOf="@+id/imageButton5"
                app:layout_constraintTop_toTopOf="@+id/imageButton5"
                app:srcCompat="@drawable/sync_browsers" />

            <ImageButton
                android:id="@+id/imageButton5"
                android:layout_width="29dp"
                android:layout_height="0dp"
                android:layout_marginEnd="8dp"
                app:layout_constraintBottom_toBottomOf="@+id/imageButton2"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="@+id/imageButton2"
                app:srcCompat="@drawable/add_singalong_browser" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

这个错误实际上是合乎逻辑的,熟悉阅读 logcat 的人会在 5 分钟内找到它。

启用使用 vectorDrawables.useSupportLibrary = true 的矢量后,您以后必须使用 app:srcCompat= @drawable/xyz 引用 所有 矢量图像,如果您的任何 xml 文件改为使用 android:src="@drawable/vector 那么应用程序将崩溃,如上所示。

旁注:文档中提到的解决方案也适用于 androidx 用户,只是为了清楚。