Android ProGuard: cannot find reference class

Running ProGuard in my Android Studio project I get the following warnings:

Warning: com.google.common.collect.Maps: can't find referenced class javax.annotation.Nullable 

I could solve this with one of these options:

1

 -keep class com.google.common.collect.** { *; } -dontwarn com.google.common.collect.** 

2

 -keep class javax.annotation.** { *; } -dontwarn javax.annotation.** 

What is the best way to resolve the above warning? What is the difference between options 1. and 2.?

+6
source share
3 answers

This is the most common mistake related to the fact that β€œmany pre-compiled third-party libraries belong to other libraries that are not actually used and, therefore, are not present. This works fine in debug builds, but in build versions ProGuard expects everything to be libraries, so it can do the right static analysis. "

From: http://proguard.sourceforge.net/index.html#manual/examples.html

So this javax.annotation.Nullable may not be in your project, but the libraries you use have some classes that internally reference them.

However, you can avoid these warnings with -dontwarn javax.annotation.** or --dontwarn com.google.common.collect.** . But I do not think that -keep class javax.annotation.** { *; } -keep class javax.annotation.** { *; } and looks illogical.

So, if you do -keep class com.google.common.collect.** { *; } -keep class com.google.common.collect.** { *; } , you skip this package from all three steps of the Proguard (Shortening, Optimizing and Obfuscating), which makes sense according to my understanding.

+6
source

Do not forget to add a package

 -keep class com.package_name.** { *; } 
+1
source

Use Nullable Support Libraries instead:

 import android.support.annotation.Nullable; 
0
source

All Articles