简体   繁体   English

android支持库的推荐proguard设置是什么?

[英]What are the recommended proguard settings for android support library?

I am using android-support-library-v7 in my project and of course I would like to use progurad to minimize and obfuscate my code. 我在我的项目中使用android-support-library-v7,当然我想使用progurad来最小化和混淆我的代码。 The problem is that if I use proguard I get errors similar to this: 问题是,如果我使用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: <init> [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 is used in my preferences.xml file and I guess it is accessed via reflection. PreferenceCategory在我的preferences.xml文件中使用,我想它是通过反射访问的。 Proguard if probably removes this method (or class) if it is not referenced in code when shrinking. 如果在缩小时代码中未引用此方法(或类),则可能会删除Proguard。 And even if not it surely obfuscates the name. 即使不是,它肯定会混淆这个名字。 Google in itßs usual fashion provides absolutely no documentation on the subject. 谷歌以其通常的方式提供绝对没有关于这个主题的文件。

So, what are the optimum settings to achieve good shrinking and good obfuscation. 那么,实现良好收缩和良好混淆的最佳设置是什么。 There are some similar questions on Stack Overflow, but they boil down to: Stack Overflow上有一些类似的问题,但它们归结为:

-keep class android.support.v7.** { *; }

which cleary defeats the purpose of shrinking and obfuscating. 这种清晰无视萎缩和混淆的目的。

Trying to randomly guess the settings or by try and error method is extremely time consuming. 尝试随机猜测设置或尝试和错误方法是非常耗时的。

The Support Library uses the consumerProguardFiles feature to automatically include the appropriate ProGuard if you're using Gradle, meaning you don't need to manually include anything. 如果您正在使用Gradle,则支持库使用consumerProguardFiles功能自动包含相应的ProGuard,这意味着您无需手动包含任何内容。

Looking at the ProGuard file for preferences-v7 (stored in the proguard.txt file within the AAR), it contains the following lines: 查看ProGuard文件中的preferences-v7 (存储在AAR中的proguard.txt文件中),它包含以下行:

# Preference objects are inflated via reflection
-keep public class android.support.v7.preference.Preference {
  public <init>(android.content.Context, android.util.AttributeSet);
}
-keep public class * extends android.support.v7.preference.Preference {
  public <init>(android.content.Context, android.util.AttributeSet);
}

Which covers the exact method that it says you are missing (as PreferenceCategory indirectly extends Preference ). 它涵盖了它说你缺少的确切方法(因为PreferenceCategory间接扩展了Preference )。 Check to make sure you are using the full Gradle dependency. 检查以确保您使用完整的Gradle依赖项。

在Github上查看这个项目 ,该项目具有流行库的proguard规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM