Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

java - ProGuard configuration for Guava with obfuscation and optimization

Looking for a ProGuard configuration for Guava that will obfuscate and optimize, as the default one that is provided on the website does not.

Not only that I cannot get it to export my apk, I keep getting:

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.
question from:https://stackoverflow.com/questions/9120338/proguard-configuration-for-guava-with-obfuscation-and-optimization

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As of Guava 17.0, this is what I needed in ProGuard config:

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

Otherwise build fails with warnings like:

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

(That's because Guava uses annotations that are not part of Android runtime (android.jar). In this case it's fine to just mute the warnings.)

If you are using Gradle as the build tool, the above proguard-project.txt and the following in build.gradle produces an optimised and obfuscated APK while using Guava.

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

Alternatively you can include dependecy to jsr305.jar in build.gradle dependencies:

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

...with only -dontwarn sun.misc.Unsafe in ProGuard config, but I preferred using -dontwarn also for the javax stuff.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...