Java – How to configure ProGuard to omit methods with SuppressWarnings (“unused”)

How to configure ProGuard to omit methods with SuppressWarnings (“unused”)… here is a solution to the problem.

How to configure ProGuard to omit methods with SuppressWarnings (“unused”)

I have methods used by the Android framework ObjectAnimator instance. So they don’t seem to be used (they are used by reflection) and I added the SuppressWarnings("unused") comment so IntelliJ doesn’t show warnings for them. However, ProGuard will still strip them and I need to explicitly tell him not to do so. This is tedious and seems redundant (violation of DRY). Can I configure ProGuard not to use the SuppressWarnings("unused") removal method?

Solution

The option to tell ProGuard to keep some classes and members (fields and methods) is described in the official documentation: http://proguard.sourceforge.net/manual/usage.html#keepoptions .

You can tell ProGuard to persist class members using:

-keepclassmembers class_specification

Your class_specification will look like this:

class * {
    @java.lang.SuppressWarnings <methods>;
}

So in your proguard.cfg you should have something like this:

-keepclassmembers class * {
    @java.lang.SuppressWarnings <methods>;
}

Related Problems and Solutions