compilation fails when I use Guava's Optional class: duplicate class: com.google.common.collect.AbstractIterator

I see the problem is described here http://stackoverflow.com/questions/17104455/guava-abstractiterator-duplicate-class pretty well. we can’t use Optional class from Google Guava because it triggers the compilation error:

\.gradle\caches\artifacts-24\filestore\com.google.guava\guava-gwt2.0\jar7db9cba72410110a02558741de7766939b864a0\guava-gwt-12.
0.jar(com/google/common/collect/AbstractIterator.java):64: error: duplicate class: com.google.common.collect.AbstractIterator
public abstract class AbstractIterator<T> extends UnmodifiableIterator<T> {
                ^
.gradle\caches\artifacts-24\filestore\com.google.guava\guava-gwt2.0\jar7db9cba72410110a02558741de7766939b864a0\guava-gwt-12.
0.jar(com/google/common/base/Optional.java):216: error: cannot access AbstractIterator
        return new AbstractIterator<T>() {
                   ^
  bad source file: .gradle\caches\artifacts-24\filestore\com.google.guava\guava-gwt2.0\jar7db9cba72410110a02558741de7766939b
864a0\guava-gwt-12.0.jar(com/google/common/base/AbstractIterator.java)
    file does not contain class com.google.common.base.AbstractIterator
    Please remove or make sure it appears in the correct subdirectory of the sourcepath.

we already have

compileJava.options.compilerArgs = ["-implicit:none"]

in the build.gradle file.

compilation in the IDE (Jetbrains IDEA) works fine.

Just to add to this - we are using Guava with GWT (just like in the StackOverflow article).

Can you provide a minimal self-contained sample build that allows to reproduce the problem?

Here is an example project which exhibits the same problem

https://github.com/matheeeny/guava-gwt-gradle-problem

I’ve also included an ant buildfile which has no problem compiling the same code.

Setting an empty source path solves the problem:

tasks.withType(JavaCompile) {
    options.compilerArgs += ["-sourcepath", ""]
}

this helps, thank you.

Hate to dredge up an answered question, but could someone give me a quick explanation of why omitting the source path helps in this instance? Thanks!

By default, the class path is also used as source path. This can be prevented by setting a source path explicitly.

Wow thanks for such a quick reply. So the issue in the case of the GWT situation is that, because the JAR includes the source, it creates an ugly conflict? Thanks, E

Yes, that’s what happens.

perfect, thanks.