tutorial studio ofuscar desofuscar codigo java android guava proguard

java - ofuscar - proguard android studio tutorial



Configuración de ProGuard para Guava con ofuscación y optimización (10)

A partir de Guava 17.0 , esto es lo que necesitaba en la configuración de ProGuard:

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

De lo contrario, la construcción falla con advertencias como:

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

(Eso es porque Guava usa anotaciones que no son parte del tiempo de ejecución de Android (android.jar). En este caso, está bien silenciar las advertencias).

Si está utilizando Gradle como la herramienta de compilación , proguard-project.txt y lo siguiente en build.gradle produce una APK optimizada y ofuscada mientras usa Guava.

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

Alternativamente , puede incluir la dependencia a jsr305.jar en build.gradle dependencias de build.gradle :

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

... con solo -dontwarn sun.misc.Unsafe en configuración de ProGuard, pero preferí usar -dontwarn también para las cosas javax .

Buscando una configuración de ProGuard para Guava que ofusque y optimice, ya que el predeterminado que se proporciona en el sitio web no lo hace.

No solo no puedo obtener que exporte mi apk, sigo recibiendo:

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.


Ahora que se lanzó Guava 19.0 , descubrí que tenía que agregar estas reglas.

-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


Estas reglas de defensa funcionaron para mí: 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


Los archivos jar de guava fueron producidos por un compilador Java que (a diferencia de javac) mantuvo referencias adicionales a las constantes internas en el conjunto constante de clases de referencia en el mismo archivo fuente, que falla solo con proguard 4.7, que parece optimizar las definiciones de constantes privadas , sin optimizar las referencias. Los futuros archivos jar de guayaba no tendrán tales referencias de grupo constantes, pero probablemente haya un error en proguard 4.7 que no se manifiesta con archivos jar producidos por javac estándar.


Para Guava 20.0 necesita agregar también :

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


Regla de protección total para la guayaba 20.0. Por ahora, la última guava 21 no es compatible con Android.

-dontwarn com.google.common.base.** -keep class com.google.common.base.** {*;} -dontwarn com.google.errorprone.annotations.** -keep class com.google.errorprone.annotations.** {*;} -dontwarn com.google.j2objc.annotations.** -keep class com.google.j2objc.annotations.** { *; } -dontwarn java.lang.ClassValue -keep class java.lang.ClassValue { *; } -dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement -keep class org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement { *; }


Solo quiero compartir lo que funciona actualmente para mi aplicación con lo siguiente en build.gradle:

compile ''com.google.guava: guava: 23.5-android''

Lo siguiente está en proguard-project.txt para tratar con Guava. Es esencialmente la combinación de la respuesta de TimTron y la sugerencia 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.**


Tuve que agregar las siguientes líneas a mi archivo de reglas proguard al actualizar de 23.4-android a 23.5-android:

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

Las advertencias de protección que vi de la actualización (antes de agregar nuevas reglas) se enumeran a continuación para referencia:

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

Para los interesados, el conjunto completo de reglas que tengo para la guayaba actualmente es:

# 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://.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.**


Uno de los ingenieros que construyó el R8 (Stephan Herhut) enumeró las siguientes reglas para la última guayaba ( 25.0-android ) en esta sesión de Google I / O 2018 :

-dontwarn afu.org.checkerframework.** -dontwarn org.checkerframework.** -dontwarn com.google.errorprone.** -dontwarn sun.misc.Unsafe -dontwarn java.lang.ClassValue

El video también incluye una breve explicación de las reglas (los primeros 3 son marcos de anotación de análisis estáticos, los últimos 2 son clases no disponibles en Android que están protegidas por verificaciones en tiempo de ejecución en Guava).

Hablé con Stephan después de la sesión y dijo que con suerte se incluirán en la documentación de Guava en el futuro o mejor aún en los consumerProguardFiles de las futuras versiones de Guava. ¡Aquí está esperando!


Este mensaje sugiere la siguiente solución alternativa:

-dontwarn com.google.common.collect.MinMaxPriorityQueue

Me gustaría pensar que Proguard podría cambiarse para hacer esto innecesario, pero no hemos consultado con sus propietarios.