【问题标题】:Is "@Keep support annotation" rule in proguard-android-optimize.txt still applicable when using AndroidX?使用 AndroidX 时,proguard-android-optimize.txt 中的“@Keep 支持注释”规则是否仍然适用?
【发布时间】:2020-03-01 09:59:31
【问题描述】:

通常将proguard-android-optimize.txt 应用为proguard 文件。

proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-project.txt'

但是,当我浏览proguard-android-optimize.txt的内容时

...

-keepclassmembers class **.R$* {
    public static <fields>;
}

# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version.  We know about them, and they are safe.
-dontwarn android.support.**

# Understand the @Keep support annotation.
-keep class android.support.annotation.Keep

-keep @android.support.annotation.Keep class * {*;}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <methods>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <fields>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <init>(...);
}

我想知道,如果我们使用 AndroidX 而不是支持库,这样的规则是否仍然适用?我们是否应该在我们的项目proguard-project.txt 中明确添加以下规则?

# Understand the @Keep support annotation.
-keep class androidx.annotation.Keep

-keep @androidx.annotation.Keep class * {*;}

-keepclasseswithmembers class * {
    @androidx.annotation.Keep <methods>;
}

-keepclasseswithmembers class * {
    @androidx.annotation.Keep <fields>;
}

-keepclasseswithmembers class * {
    @androidx.annotation.Keep <init>(...);
}

【问题讨论】:

    标签: android proguard androidx android-r8


    【解决方案1】:

    我刚刚检查了它并用androidx.annotation.Keep 注释类保留了它们。无需额外配置。

    【讨论】:

      【解决方案2】:

      如果你不使用proguard-android-optimize.txt或proguard-android.txt,你应该添加

      -keep class androidx.annotation.Keep {*;}
      

      或者,您的@Keep 将转向@a

      如果您使用 proguard-android-optimize.txt 或 proguard-android.txt,它仍然可以工作。

      希望它对你有用。

      【讨论】:

        猜你喜欢
        • 2018-02-05
        • 2015-08-26
        • 2019-11-16
        • 2019-08-28
        • 2010-09-05
        • 2011-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多