DexGuard和矢量可绘制对象 [英] DexGuard and Vector Drawables

查看:162
本文介绍了DexGuard和矢量可绘制对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DexGuard编译我的应用程序.我的应用程序使用矢量可绘制对象.在调试版本中以及将Proguard用作发行版时,我的所有设置均正确无误,并且可以正常工作.使用DexGuard时,它仅在发行版中崩溃.所以我知道这是DexGuard的问题. 我正在使用DexGuard v7.0,由于许可证限制,我无法更新. 我已经添加了 -keepresourcefiles "res/drawable/**" -keep class android.support.v7.** { *; } 到我的dexguard文件中以进行测试和更清晰的堆栈跟踪,因此我们知道这不是问题. 跟随以下stacktrace崩溃;

I am using DexGuard to compile my app. My app uses vector drawables. I have all the settings correct, and it works correctly, both in the debug version, and when I use Proguard for the release version. It only crashes in the release version when using DexGuard. So I know it's a DexGuard issue. I am using DexGuard v7.0 and I cannot update due to license restrictions. I have added -keepresourcefiles "res/drawable/**" -keep class android.support.v7.** { *; } to my dexguard file for testing purposes and for clearer stacktraces, so we know that is not the issue. It crashes with following stacktrace;

 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时,android.support.v7.widget.AppCompatDrawableManager中的方法loadDrawableFromDelegates(从getDrawable调用)返回空值.因此,getDrawable继续并调用ContextCompat.getDrawable,它在之前的Lollipop设备中不支持Vector,并且它返回带有invalid drawable tag vector日志的null,因此导致ResourcesNotFoundException.
使用调试版本时,loadDrawableFromDelegates确实返回Drawable,因此不会调用ContextCompat.getDrawable,并且一切正常. 没有人有任何想法如何避免这种情况.我在Google上进行了搜索,还有其他人遇到了类似的问题,但这些问题大多是appcompat向量设置问题,不是DexGuard.

Upon following the code, I follow that when using DexGuard, the method loadDrawableFromDelegates (called from getDrawable) in android.support.v7.widget.AppCompatDrawableManager is returning null. Therefore getDrawable continues and calls ContextCompat.getDrawable, which does not support Vectors in pre-Lollipop devices, and it returns null with an invalid drawable tag vector log, and therefore causes a ResourcesNotFoundException.
When using the debug version, loadDrawableFromDelegates does return a Drawable, therefore ContextCompat.getDrawable does not get called, and everything works fine. Does anyone have any ideas how to avoid this. I Googled, and there are others who had similiar issues, but they were mostly appcompat vector setting issues, not DexGuard.

推荐答案

经过12个小时的工作,我终于弄清楚了,而这只是DexGuard配置中的1行.您不需要我上面提到的任何设置.您只需要

Well after 12 hours of work, I finally figured it out, and all it was is 1 line in the DexGuard config. You do not need any of the settings I mentioned above. all you need is

-keepresourcexmlattributenames vector/**

这样做的原因是AppCompat库不会通过id查找向量的属性,例如viewportWidth,而是使用实际的属性名称.默认情况下,Dexguard会删除名称,因此它永远不会找到该值.通过添加以上行,您告诉DexGuard保留所有矢量文件的属性名称.
我希望这可以使其他人免于心痛.

Reason for this is that the AppCompat library does not look for the attributes of the vector, like viewportWidth by id, but it uses the actual atribute name. Dexguard by default strips the name, thus it never found the value. By adding the above line, you are telling DexGuard to leave the attribute names for all vector files.
I hope this saves others of much heartache.

这篇关于DexGuard和矢量可绘制对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆