我正在使用 DexGuard 编译我的应用程序。我的应用程序使用矢量绘图。我的所有设置都是正确的,并且在调试版本和使用 Proguard 发布版本时都能正常工作。它只在使用 DexGuard 时在发布版本中崩溃。所以我知道这是一个 DexGuard 问题。我正在使用 DexGuard v7.0,由于许可证限制,我无法更新。我已经添加
-keepresourcefiles "res/drawable/**"
-keep class android.support.v7.** { *; }
到我的 dexguard 文件中以用于测试目的和更清晰的堆栈跟踪,所以我们知道这不是问题。它与以下堆栈跟踪崩溃;
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lionscribe.myapp/com.lionscribe.elist.main.MainActivity}: android.content.res.Resources$NotFoundException: File res/drawable/abc_ic_ab_back_material.xml from drawable resource ID #0x7f020018
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/abc_ic_ab_back_material.xml from drawable resource ID #0x7f020018
at android.content.res.Resources.loadDrawable(Resources.java:3440)
at android.content.res.Resources.getDrawable(Resources.java:1917)
at o.?.?(:354)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(:193)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(:181)
at android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(:689)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(:186)
at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(:77)
at android.support.v7.app.AppCompatDelegateImplBase.<init>(:83)
at android.support.v7.app.AppCompatDelegateImplV7.<init>(:146)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(:28)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(:41)
at android.support.v7.app.AppCompatDelegate.create(:193)
at android.support.v7.app.AppCompatDelegate.create(:173)
at android.support.v7.app.AppCompatActivity.getDelegate(:511)
at android.support.v7.app.AppCompatActivity.onCreate(:71)
at o.hF.onCreate(:29)
at com.lionscribe.elist.main.MainActivity.onCreate(:121)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #17: invalid drawable tag vector
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:986)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:930)
at android.content.res.Resources.loadDrawable(Resources.java:3436)
at android.content.res.Resources.getDrawable(Resources.java:1917)
at o.?.?(:354)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(:193)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(:181)
at android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(:689)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(:186)
at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(:77)
at android.support.v7.app.AppCompatDelegateImplBase.<init>(:83)
at android.support.v7.app.AppCompatDelegateImplV7.<init>(:146)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(:28)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(:41)
at android.support.v7.app.AppCompatDelegate.create(:193)
at android.support.v7.app.AppCompatDelegate.create(:173)
at android.support.v7.app.AppCompatActivity.getDelegate(:511)
at android.support.v7.app.AppCompatActivity.onCreate(:71)
at o.hF.onCreate(:29)
at com.lionscribe.elist.main.MainActivity.onCreate(:121)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
遵循代码后,我发现在使用 DexGuard 时,方法loadDrawableFromDelegates
(从 调用getDrawable
)android.support.v7.widget.AppCompatDrawableManager
返回 null。因此getDrawable
continue 并调用ContextCompat.getDrawable
,它在棒棒糖之前的设备中不支持向量,它返回 null 并带有invalid drawable tag vector
日志,因此导致ResourcesNotFoundException
.
使用调试版本时,loadDrawableFromDelegates
确实返回一个 Drawable,因此ContextCompat.getDrawable
不会被调用,并且一切正常。有谁知道如何避免这种情况。我用谷歌搜索过,还有其他人也有类似的问题,但主要是 appcompat 矢量设置问题,而不是 DexGuard。