Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 
like image 984
user1159819 Avatar asked Feb 02 '12 21:02

user1159819


People also ask

How does ProGuard obfuscation work?

Simply by enabling Proguard, we at Gradeup reduced our app size by a whooping 40%! It obfuscates the code, which means that it renames classes, fields, and methods with semantically obscure names that, in addition to making the codebase smaller and more efficient, also makes it difficult to reverse engineer the app.

What is ProGuard optimization?

ProGuard optimizes Gson code by detecting which domain classes are serialized using the Gson library. It replaces the reflection-based implementation of GSON for reading and writing fields with injected and optimized code that accesses the fields of the domain classes directly when reading and writing JSON.

What is ProGuard configuration?

ProGuard is a tool to help minify, obfuscate, and optimize your code. It is not only especially useful for reducing the overall size of your Android application as well as removing unused classes and methods that contribute towards the intrinsic 64k method limit of Android applications.

How do you set ProGuard rules?

When you create a new project or module using Android Studio, the IDE creates a <module-dir>/proguard-rules.pro file for you to include your own rules. You can also include additional rules from other files by adding them to the proguardFiles property in your module's build.


1 Answers

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.

like image 194
Jonik Avatar answered Oct 12 '22 04:10

Jonik