使用混淆和优化的ProGuard配置Guava

38

我正在寻找一份适用于Guava的ProGuard配置文件,可以对代码进行混淆和优化。默认的配置文件并不能实现这个功能。

不仅如此,我还无法将我的apk导出,而且一直收到以下错误信息:

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.

这更像是一个Proguard问题,我认为? - Louis Wasserman
更多是关于Guava的问题。Guava中的某些内容导致该类抛出警告。列出的答案应该解决这个问题。 - Slinkkay
啊。我更关注问题的前半部分:“一个可以混淆和优化的配置。” - Louis Wasserman
1
是的,两个问题有点烦人,但在这种情况下,两个部分都是关于proguard的。 - Kevin Bourrillion
10个回答

43
从 Guava 17.0 开始,这是我在 ProGuard 配置中所需的内容:
-dontwarn javax.annotation.**
-dontwarn javax.inject.**
-dontwarn sun.misc.Unsafe

否则,构建将失败并出现警告,例如:
Warning: com.google.common.base.Absent: 
   can't find referenced class javax.annotation.Nullable

因为Guava使用注解不属于Android运行时(android.jar)。在这种情况下,只需将警告静音即可。
如果您使用Gradle作为构建工具,则上述proguard-project.txt和以下内容在build.gradle中生成一个优化和混淆的APK,同时使用Guava。
buildTypes {
    release {
        minifyEnabled true
        proguardFile file('proguard-project.txt')
        proguardFile getDefaultProguardFile('proguard-android-optimize.txt')
    }
}

或者,您可以在build.gradle的依赖项中包含jsr305.jar的依赖项

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

...在ProGuard配置中只使用-dontwarn sun.misc.Unsafe,但我更喜欢对javax的内容也使用-dontwarn


2
顺便说一句,如果有人担心Guava依赖对APK大小的影响,不用担心。简单测试表明,没有ProGuard时,Guava会使APK大小增加约700KB,但使用ProGuard后,增加的大小可以忽略不计。 - Jonik
我正在使用Guava com.google.guava:guava:22.0-android,你的建议在我的proguard配置上不起作用。有什么想法吗? - Eftekhari

22

当我从23.4-android升级到23.5-android时,我不得不在我的proguard规则文件中添加以下行:

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

在添加新规则之前,我从更新中看到的Proguard警告如下:

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

对于那些感兴趣的人,我目前对guava的完整规则集如下:

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

在 build.gradle 的 dependencies 部分,您是否也包含了 jsr 库? - Prizoff
1
非常感谢,你救了我的一天!它也适用于“guava:24.0-android”。 - BitRulez

21

编辑(2023.10.23):看起来Guava在32.0.0版本中开始包含Proguard配置(相关commit)!


R8的其中一位工程师(Stephan Herhut)在Google I/O 2018的这个演讲中列出了最新Guava(25.0-android)的以下规则:
-dontwarn afu.org.checkerframework.**
-dontwarn org.checkerframework.**
-dontwarn com.google.errorprone.**
-dontwarn sun.misc.Unsafe
-dontwarn java.lang.ClassValue

视频还包括对规则的简短解释(前3个是静态分析注解框架,后2个是在Android上由Guava的运行时检查保护的不可用类)。
我在会议后与Stephan交谈,他说希望将这些内容包含在未来的Guava文档中,或者更好地包含在未来的Guava版本的consumerProguardFiles中。希望如此!

从技术上讲,Stephan的演讲使用了Guava 24.1-android,但我已经使用25.0-android测试了这些规则,并且它们运行良好。 - ashughes
感谢提供基础解释。几个月过去了,这仍然没有出现在Guava的文档中... - Johnny Doe
看起来Guava终于开始在https://github.com/google/guava/releases/tag/v32.0.0发布Proguard配置了。 - undefined

18
现在 Guava 19.0已经发布,我发现我必须添加这些规则。
-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

我正在使用Guava com.google.guava:guava:22.0-android,你的建议在我的proguard配置上不起作用。有什么想法吗? - Eftekhari

11

对于Guava 20.0,您还需要添加also

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

我正在使用Guava com.google.guava:guava:22.0-android,你的建议在我的proguard配置上不起作用。有什么想法吗? - Eftekhari
@Eftekhari 请确保您添加了所有规则,即不仅针对22.0,还包括20.0、19等。如果这样做没有帮助:您可以打开一个新问题,并粘贴构建错误信息。 - Ognyan

7

这条消息建议采用以下解决方法:

-dontwarn com.google.common.collect.MinMaxPriorityQueue 

我希望Proguard能够进行改进,使这个步骤变得不再必要,但我们还没有与其所有者核实。


5

Guava 20.0的Proguard规则。目前最新版本的guava 21不支持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 { *; }

不需要使用-keep。 - David Miguel
缺失 -dontwarn javax.annotation.** -dontwarn javax.inject.** -dontwarn sun.misc.Unsafe - Yoav R.

4

我想分享一下在我的应用程序中,以下内容在 build.gradle 中实际上是起作用的:

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

以下是在 proguard-project.txt 文件中处理 Guava 的内容。它基本上是结合了 TimTron 的回答和 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.**

3

Guava的jar文件是由Java编译器生成的,与javac不同的是,它在相同源文件中引用类的常量池中保留了额外的对私有内联常量的引用。这只会在Proguard 4.7上失败,因为它似乎优化掉了私有常量定义,但没有优化掉引用。未来的Guava jar文件将不再具有这样的常量池引用,但是Proguard 4.7可能存在错误,而使用标准javac生成的jar文件则不会表现出来。


如果您告诉ProGuard忽略可疑的引用,它将优化掉常量定义和引用。在编译后的代码中,一个类正在引用另一个类中的私有字段。据我理解,这样的引用(没有访问器方法)是不允许的(JVM规范2.17.3)。其他程序(如JVM)可能会因为惰性解析而更加宽容。我仍然很好奇哪个编译器生成了这段代码。 - Eric Lafortune

3

这些混淆规则对我非常有效: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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接