'Is "@Keep support annotation" rule in proguard-android-optimize.txt still applicable when using AndroidX?
It is common to apply proguard-android-optimize.txt
as proguard file.
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-project.txt'
However, when I went through the content of 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>(...);
}
I was wondering, if such rule still applicable, if we were using AndroidX instead of support library? Should we add the following rules explicitly in our project 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>(...);
}
Solution 1:[1]
I just checked it and annotating classes with the androidx.annotation.Keep
keeps them. No additional configuration required.
Solution 2:[2]
If you don't use proguard-android-optimize.txt or proguard-android.txt, you should add
-keep class androidx.annotation.Keep {*;}
Or, your @Keep
will turn to @a
If you use proguard-android-optimize.txt or proguard-android.txt, it still work.
Hope it will work for you.
Solution 3:[3]
The androidx.annotation:annotation:x.x.z
has its own proguard declaration:
-keep,allowobfuscation @interface 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>(...);
}
-keepclassmembers,allowobfuscation class * {
@androidx.annotation.DoNotInline <methods>;
}
This file is located in:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Krzysztof Misztal |
Solution 2 | Jaimil Patel |
Solution 3 | heqingbao |