Proguard with Autovalue

2019-04-06 21:49发布

问题:

I have just started using AutoValue but I am unable to make it work with proguard. I have around 6000+ warnings that look like this

Warning:autovalue.shaded.com.google.common.auto.common.MoreElements$1: can't find superclass or interface javax.lang.model.util.SimpleElementVisitor6

The actually errors shows this...

Error:Execution failed for task ':transformClassesAndResourcesWithProguardForDebug'. java.io.IOException: Please correct the above warnings first.

How can i resolve this issue?

回答1:

The fix

This is happening since you have added the library as a compile dependency of your project. Something like this:

dependencies {
    compile 'com.google.auto.value:auto-value:1.2'
}

You need to make the library a provided dependency:

dependencies {
    provided 'com.google.auto.value:auto-value:1.2'
}

Note: The provided configuration is made available by the Android Gradle plugin. If you're using AutoValue in a pure Java library module, use the compileOnly configuration, added in Gradle 2.12.

The explanation

AutoValue is a library that generates code for you. Your only interaction with the library itself should via the @AutoValue annotations, which have RetentionPolicy.SOURCE - i.e. they are only available in your source code, not in the compiled code.

This means that your compiled code has no connection to the AutoValue library code whatsoever. So, it doesn't need to to compiled with your code - which is the code ProGuard runs on.