web-dev-qa-db-fra.com

Configuration de Proguard pour Guava avec Obfuscation et optimisation

Vous recherchez une configuration proguard pour GUAVA qui obflure et optimisera, car la valeur par défaut fournie sur le site Web ne le fait pas.

Non seulement je ne peux pas l'obtenir pour exporter mon APK, je continue à obtenir:

Warning: com.google.common.collect.MinMaxPriorityQueue: 
    can't find referenced field 'int UNSET_EXPECTED_SIZE' in class  
    com.google.common.collect.MinMaxPriorityQueue$Builder
You should check if you need to specify additional program jars.
38
user1159819

A partir de GUAVA 17.0, c'est ce dont j'avais besoin dans Proguard Config:

-dontwarn javax.annotation.**
-dontwarn javax.inject.**
-dontwarn Sun.misc.Unsafe

Sinon la construction échoue avec des avertissements comme:

Warning: com.google.common.base.Absent: 
   can't find referenced class javax.annotation.Nullable

(C'est parce que GUAVA utilise des annotations qui ne font pas partie de Android Runtime (Android.jar). Dans ce cas, il va bien de couper les avertissements.)

Si vous utilisez gradle comme outil de construction , ce qui précède proguard-project.txt et ce qui suit dans build.gradle Produit une APK optimisée et obscopiée lors de l'utilisation de GUAVA.

buildTypes {
    release {
        minifyEnabled true
        proguardFile file('proguard-project.txt')
        proguardFile getDefaultProguardFile('proguard-Android-optimize.txt')
    }
}

Alternativement Vous pouvez inclure la dépendonnée de JSR305.jar en build.gradle Dépendances:

compile 'com.google.code.findbugs:jsr305:2.0.2'

...avec seulement -dontwarn Sun.misc.Unsafe Dans Proguard Config, mais j'ai préféré utiliser -dontwarn aussi pour le javax trucs.

43
Jonik

Je devais ajouter les lignes suivantes à mon fichier de règles de Proguard lors de la mise à niveau de 23,4-android vers 23.5-5-android:

-dontwarn afu.org.checkerframework.**
-dontwarn org.checkerframework.**

Les avertissements de PROGUARD que j'ai vu à partir de la mise à jour (avant d'ajouter de nouvelles règles) sont énumérés ci-dessous pour référence:

Warning: afu.org.checkerframework.checker.experimental.tainting_qual.qual.Untainted: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.experimental.tainting_qual.qual.Untainted: can't find referenced class afu.com.Sun.source.tree.Tree
Warning: afu.org.checkerframework.checker.experimental.tainting_qual.qual.Untainted: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.experimental.tainting_qual.qual.Untainted: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.fenum.qual.FenumBottom: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.fenum.qual.FenumBottom: can't find referenced class afu.com.Sun.source.tree.Tree
Warning: afu.org.checkerframework.checker.fenum.qual.FenumBottom: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.formatter.qual.FormatBottom: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.formatter.qual.FormatBottom: can't find referenced class afu.com.Sun.source.tree.Tree
Warning: afu.org.checkerframework.checker.formatter.qual.FormatBottom: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.guieffect.qual.AlwaysSafe: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.guieffect.qual.AlwaysSafe: can't find referenced class afu.com.Sun.source.tree.Tree
Warning: afu.org.checkerframework.checker.guieffect.qual.AlwaysSafe: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.i18n.qual.Localized: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.i18n.qual.Localized: can't find referenced class afu.com.Sun.source.tree.Tree
Warning: afu.org.checkerframework.checker.i18n.qual.Localized: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.i18n.qual.Localized: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.i18n.qual.Localized: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.i18n.qual.Localized: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.i18n.qual.Localized: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.i18n.qual.Localized: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.i18nformatter.qual.I18nFormatBottom: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.i18nformatter.qual.I18nFormatBottom: can't find referenced class afu.com.Sun.source.tree.Tree
Warning: afu.org.checkerframework.checker.i18nformatter.qual.I18nFormatBottom: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.initialization.qual.FBCBottom: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.initialization.qual.FBCBottom: can't find referenced class afu.com.Sun.source.tree.Tree
Warning: afu.org.checkerframework.checker.initialization.qual.FBCBottom: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.interning.qual.Interned: can't find referenced class afu.org.checkerframework.framework.type.AnnotatedTypeMirror$AnnotatedPrimitiveType
Warning: afu.org.checkerframework.checker.interning.qual.Interned: can't find referenced class afu.org.checkerframework.framework.type.AnnotatedTypeMirror
Warning: afu.org.checkerframework.checker.interning.qual.Interned: can't find referenced class afu.com.Sun.source.tree.LiteralTree
Warning: afu.org.checkerframework.checker.interning.qual.Interned: can't find referenced class afu.org.checkerframework.framework.type.AnnotatedTypeMirror$AnnotatedPrimitiveType
Warning: afu.org.checkerframework.checker.nullness.qual.KeyForBottom: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.nullness.qual.KeyForBottom: can't find referenced class afu.com.Sun.source.tree.Tree
Warning: afu.org.checkerframework.checker.nullness.qual.KeyForBottom: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.nullness.qual.NonNull: can't find referenced class afu.org.checkerframework.framework.type.AnnotatedTypeMirror$AnnotatedPrimitiveType
Warning: afu.org.checkerframework.checker.nullness.qual.NonNull: can't find referenced class afu.org.checkerframework.framework.type.AnnotatedTypeMirror
Warning: afu.org.checkerframework.checker.nullness.qual.NonNull: can't find referenced class afu.org.checkerframework.framework.type.AnnotatedTypeMirror$AnnotatedNoType
Warning: afu.org.checkerframework.checker.nullness.qual.NonNull: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.nullness.qual.NonNull: can't find referenced class afu.com.Sun.source.tree.Tree
Warning: afu.org.checkerframework.checker.nullness.qual.NonNull: can't find referenced class javax.lang.model.type.TypeKind
Warning: afu.org.checkerframework.checker.nullness.qual.NonNull: can't find referenced class afu.org.checkerframework.framework.type.AnnotatedTypeMirror$AnnotatedPrimitiveType
Warning: afu.org.checkerframework.checker.nullness.qual.NonNull: can't find referenced class afu.org.checkerframework.framework.type.AnnotatedTypeMirror$AnnotatedNoType
Warning: afu.org.checkerframework.checker.nullness.qual.NonNull: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.nullness.qual.NonNull: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.nullness.qual.NonNull: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.nullness.qual.NonNull: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.nullness.qual.NonNull: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.nullness.qual.NonNull: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.nullness.qual.NonNull: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.nullness.qual.NonNull: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.nullness.qual.NonNull: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.nullness.qual.NonNull: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.nullness.qual.Nullable: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.nullness.qual.Nullable: can't find referenced class afu.com.Sun.source.tree.Tree
Warning: afu.org.checkerframework.checker.nullness.qual.Nullable: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.oigj.qual.Mutable: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.oigj.qual.Mutable: can't find referenced class afu.com.Sun.source.tree.Tree
Warning: afu.org.checkerframework.checker.oigj.qual.Mutable: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.oigj.qual.Mutable: can't find referenced class javax.lang.model.type.TypeKind
Warning: afu.org.checkerframework.checker.oigj.qual.OIGJMutabilityBottom: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.oigj.qual.OIGJMutabilityBottom: can't find referenced class afu.com.Sun.source.tree.Tree
Warning: afu.org.checkerframework.checker.oigj.qual.OIGJMutabilityBottom: can't find referenced class afu.org.checkerframework.framework.type.AnnotatedTypeMirror$AnnotatedPrimitiveType
Warning: afu.org.checkerframework.checker.oigj.qual.OIGJMutabilityBottom: can't find referenced class afu.org.checkerframework.framework.type.AnnotatedTypeMirror
Warning: afu.org.checkerframework.checker.oigj.qual.OIGJMutabilityBottom: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.oigj.qual.OIGJMutabilityBottom: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.oigj.qual.OIGJMutabilityBottom: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.oigj.qual.OIGJMutabilityBottom: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.oigj.qual.OIGJMutabilityBottom: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.oigj.qual.OIGJMutabilityBottom: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.oigj.qual.OIGJMutabilityBottom: can't find referenced class afu.org.checkerframework.framework.type.AnnotatedTypeMirror$AnnotatedPrimitiveType
Warning: afu.org.checkerframework.checker.regex.classic.qual.RegexBottom: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.regex.classic.qual.RegexBottom: can't find referenced class afu.com.Sun.source.tree.Tree
Warning: afu.org.checkerframework.checker.regex.classic.qual.RegexBottom: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.signature.qual.SignatureBottom: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.signature.qual.SignatureBottom: can't find referenced class afu.com.Sun.source.tree.Tree
Warning: afu.org.checkerframework.checker.signature.qual.SignatureBottom: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.tainting.classic.qual.Untainted: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.tainting.classic.qual.Untainted: can't find referenced class afu.com.Sun.source.tree.Tree
Warning: afu.org.checkerframework.checker.tainting.classic.qual.Untainted: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.tainting.classic.qual.Untainted: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.checker.units.qual.UnitsRelations: can't find referenced class afu.org.checkerframework.checker.units.UnitsRelations
Warning: afu.org.checkerframework.common.aliasing.qual.MaybeAliased: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.common.aliasing.qual.MaybeAliased: can't find referenced class afu.com.Sun.source.tree.Tree
Warning: afu.org.checkerframework.common.aliasing.qual.MaybeAliased: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.common.reflection.qual.ClassValBottom: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.common.reflection.qual.ClassValBottom: can't find referenced class afu.com.Sun.source.tree.Tree
Warning: afu.org.checkerframework.common.reflection.qual.ClassValBottom: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.common.reflection.qual.MethodValBottom: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.common.reflection.qual.MethodValBottom: can't find referenced class afu.com.Sun.source.tree.Tree
Warning: afu.org.checkerframework.common.reflection.qual.MethodValBottom: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.common.value.qual.BottomVal: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.common.value.qual.BottomVal: can't find referenced class afu.com.Sun.source.tree.Tree
Warning: afu.org.checkerframework.common.value.qual.BottomVal: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.framework.qual.ImplicitFor: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.framework.qual.ImplicitFor: can't find referenced class afu.com.Sun.source.tree.Tree
Warning: afu.org.checkerframework.framework.qual.ImplicitFor: can't find referenced class afu.com.Sun.source.tree.Tree$Kind
Warning: afu.org.checkerframework.framework.qual.ImplicitFor: can't find referenced class afu.com.Sun.source.tree.Tree
Warning: afu.org.checkerframework.framework.qual.ImplicitFor: can't find referenced class javax.lang.model.type.TypeKind
Warning: afu.org.checkerframework.framework.qual.ImplicitFor: can't find referenced class afu.org.checkerframework.framework.type.AnnotatedTypeMirror
Warning: org.checkerframework.checker.interning.qual.Interned: can't find referenced class javax.lang.model.type.TypeKind
Warning: org.checkerframework.checker.interning.qual.Interned: can't find referenced class javax.lang.model.type.TypeKind
Warning: org.checkerframework.checker.interning.qual.Interned: can't find referenced class javax.lang.model.type.TypeKind
Warning: org.checkerframework.checker.interning.qual.Interned: can't find referenced class javax.lang.model.type.TypeKind
Warning: org.checkerframework.checker.interning.qual.Interned: can't find referenced class javax.lang.model.type.TypeKind
Warning: org.checkerframework.checker.interning.qual.Interned: can't find referenced class javax.lang.model.type.TypeKind
Warning: org.checkerframework.checker.interning.qual.Interned: can't find referenced class javax.lang.model.type.TypeKind
Warning: org.checkerframework.checker.interning.qual.Interned: can't find referenced class javax.lang.model.type.TypeKind
Warning: org.checkerframework.checker.lock.qual.GuardedBy: can't find referenced class javax.lang.model.type.TypeKind
Warning: org.checkerframework.checker.lock.qual.GuardedBy: can't find referenced class javax.lang.model.type.TypeKind
Warning: org.checkerframework.checker.lock.qual.GuardedBy: can't find referenced class javax.lang.model.type.TypeKind
Warning: org.checkerframework.checker.lock.qual.GuardedBy: can't find referenced class javax.lang.model.type.TypeKind
Warning: org.checkerframework.checker.lock.qual.GuardedBy: can't find referenced class javax.lang.model.type.TypeKind
Warning: org.checkerframework.checker.lock.qual.GuardedBy: can't find referenced class javax.lang.model.type.TypeKind
Warning: org.checkerframework.checker.lock.qual.GuardedBy: can't find referenced class javax.lang.model.type.TypeKind
Warning: org.checkerframework.checker.lock.qual.GuardedBy: can't find referenced class javax.lang.model.type.TypeKind
Warning: org.checkerframework.checker.nullness.qual.NonNull: can't find referenced class javax.lang.model.type.TypeKind
Warning: org.checkerframework.checker.nullness.qual.NonNull: can't find referenced class javax.lang.model.type.TypeKind
Warning: org.checkerframework.checker.nullness.qual.NonNull: can't find referenced class javax.lang.model.type.TypeKind
Warning: org.checkerframework.checker.nullness.qual.NonNull: can't find referenced class javax.lang.model.type.TypeKind
Warning: org.checkerframework.checker.nullness.qual.NonNull: can't find referenced class javax.lang.model.type.TypeKind
Warning: org.checkerframework.checker.nullness.qual.NonNull: can't find referenced class javax.lang.model.type.TypeKind
Warning: org.checkerframework.checker.nullness.qual.NonNull: can't find referenced class javax.lang.model.type.TypeKind
Warning: org.checkerframework.checker.nullness.qual.NonNull: can't find referenced class javax.lang.model.type.TypeKind
Warning: org.checkerframework.checker.nullness.qual.NonNull: can't find referenced class javax.lang.model.type.TypeKind
Warning: org.checkerframework.checker.units.qual.UnitsRelations: can't find referenced class org.checkerframework.checker.units.UnitsRelations
Warning: org.checkerframework.framework.qual.ImplicitFor: can't find referenced class javax.lang.model.type.TypeKind

Pour les personnes intéressées, le jeu de règles complète que j'ai pour GUAVA est actuellement:

# Guava (official)
## Not yet defined: follow https://github.com/google/guava/issues/2117
# Guava (unofficial)
## https://github.com/google/guava/issues/2926#issuecomment-325455128
## https://stackoverflow.com/questions/9120338/proguard-configuration-for-guava-with-obfuscation-and-optimization
-dontwarn com.google.common.base.**
-dontwarn com.google.errorprone.annotations.**
-dontwarn com.google.j2objc.annotations.**
-dontwarn Java.lang.ClassValue
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
# Added for guava 23.5-Android
-dontwarn afu.org.checkerframework.**
-dontwarn org.checkerframework.**
22
developer_7

Un des ingénieurs qui a construit R8 (Stephan Herhut) a énuméré les règles suivantes pour la dernière Guava (25.0-Android) Dans cette session de Google I/O 2018 :

-dontwarn afu.org.checkerframework.**
-dontwarn org.checkerframework.**
-dontwarn com.google.errorprone.**
-dontwarn Sun.misc.Unsafe
-dontwarn Java.lang.ClassValue

La vidéo comprend également une explication courte des règles (les 3 premiers sont des cadres d'annotation d'analyse statique, les 2 derniers sont des classes indisponibles sur Android qui sont protégés par des chèques d'exécution à Guava).

J'ai parlé avec Stephan après la session et il a dit que ceux-ci seront espérons être inclus dans la documentation de Guava à l'avenir ou mieux encore dans le consumerProguardFiles des futures rejets de goyaves. En espérant!

21
ashughes

Maintenant que GUAVA 19.0 est publié , j'ai constaté que je devais ajouter ces règles.

-keep class com.google.j2objc.annotations.** { *; }
-dontwarn   com.google.j2objc.annotations.**
-keep class Java.lang.ClassValue { *; }
-dontwarn   Java.lang.ClassValue
-keep class org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement { *; }
-dontwarn   org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
18
Graham Borland

Pour Guava 20.0, vous devez ajouter aussi:

# Guava 20.0
-dontwarn com.google.errorprone.annotations.CanIgnoreReturnValue
-dontwarn com.google.errorprone.annotations.concurrent.LazyInit
-dontwarn com.google.errorprone.annotations.ForOverride
11
Ognyan

Ce message suggère la solution de contournement suivante:

-dontwarn com.google.common.collect.MinMaxPriorityQueue 

J'aimerais penser que PROGUARD pourrait être changé pour faire cela inutile, mais nous n'avons pas vérifié ses propriétaires.

7
Chris Povirk

Je veux juste partager ce qui fonctionne actuellement pour mon application avec ce qui suit dans Build.Gradle:

compiler 'com.google.guava: GUAVA: 23.5-Android'

Ce qui suit est dans Proguard-Project.txt pour faire face à Guava. C'est essentiellement la combinaison de la réponse de Timtron et du conseil de Develper_7:

-dontwarn Sun.misc.Unsafe
-dontwarn com.google.common.collect.MinMaxPriorityQueue
-dontwarn com.google.common.util.concurrent.FuturesGetChecked**
-dontwarn javax.lang.model.element.Modifier
-dontwarn afu.org.checkerframework.**
-dontwarn org.checkerframework.**
4
Hong

les fichiers JAR de Guava ont été produits par un Java Compiler qui (contrairement à Javac) ont conservé des références supplémentaires à des constantes inlinées privées dans le pool constant de classes de référencement dans le même fichier source, qui ne manque qu'avec Proguard 4.7, Ce qui semble optimiser les définitions constantes privées, sans optimiser les références. Les futurs fichiers JAR GUAVA ne disposeront pas de telles références de pool constantes, mais il y a probablement un bogue dans PROGUARD 4.7 qui ne manifeste pas avec les fichiers JAR produites par Standard Javac.

3
Martin Buchholz

Ces règles de Proguard ont fonctionné pour moi: Guava 23.3-Android

-dontwarn Sun.misc.Unsafe
-dontwarn com.google.common.collect.MinMaxPriorityQueue
-dontwarn com.google.common.util.concurrent.FuturesGetChecked**
-dontwarn javax.lang.model.element.Modifier
3
TmTron