我在我的项目中使用android-support-library-v7,当然我想使用progurad来最小化和混淆我的代码.问题是,如果我使用proguard,我会收到与此类似的错误:
FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.project/org.example.project.ActivityMain}: android.view.InflateException: Binary XML file line #12: Error inflating class android.support.v7.preference.PreferenceCategory at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135) at android.app.ActivityThread.access$700(ActivityThread.java:140) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4921) 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:1027) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) at dalvik.system.NativeStart.main(Native Method) Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class android.support.v7.preference.PreferenceCategory at android.support.v7.preference.g.a(Unknown Source) at android.support.v7.preference.g.a(Unknown Source) at android.support.v7.preference.g.a(Unknown Source) at android.support.v7.preference.g.a(Unknown Source) at android.support.v7.preference.g.a(Unknown Source) at android.support.v7.preference.h.a(Unknown Source) at android.support.v7.preference.h.b(Unknown Source) at org.exampple.project.ActivityMain.onCreate(Unknown Source) at android.app.Activity.performCreate(Activity.java:5206) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074) ... 11 more Caused by: java.lang.NoSuchMethodException:[class android.content.Context, interface android.util.AttributeSet] at java.lang.Class.getConstructorOrMethod(Class.java:460) at java.lang.Class.getConstructor(Class.java:431) ... 22 more
PreferenceCategory在我的preferences.xml文件中使用,我想它是通过反射访问的.如果在缩小时代码中未引用此方法(或类),则可能会删除Proguard.即使不是,它肯定会混淆这个名字.谷歌以其通常的方式提供绝对没有关于这个主题的文件.
那么,实现良好收缩和良好混淆的最佳设置是什么.Stack Overflow上有一些类似的问题,但它们归结为:
-keep class android.support.v7.** { *; }
这种清晰无视萎缩和混淆的目的.
尝试随机猜测设置或尝试和错误方法是非常耗时的.
consumerProguardFiles
如果您使用Gradle,支持库会使用该功能自动包含相应的ProGuard,这意味着您无需手动包含任何内容.
查看ProGuard文件preferences-v7
(存储在AAR中的proguard.txt文件中),它包含以下行:
# Preference objects are inflated via reflection -keep public class android.support.v7.preference.Preference { public(android.content.Context, android.util.AttributeSet); } -keep public class * extends android.support.v7.preference.Preference { public (android.content.Context, android.util.AttributeSet); }
其中涵盖了您所缺少的确切方法(PreferenceCategory
间接延伸Preference
).检查以确保您使用完整的Gradle依赖项.