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


(Alex Sko) #1

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.


(David Ackerman) #2

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


(Peter Niederwieser) #3

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


(andrew.j.matheny) #4

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.


(Peter Niederwieser) #5

Setting an empty source path solves the problem:

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

(Alex Sko) #6

this helps, thank you.


(Evan Ruff) #7

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!


(Peter Niederwieser) #8

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


(Evan Ruff) #9

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


(Peter Niederwieser) #10

Yes, that’s what happens.


(Evan Ruff) #11

perfect, thanks.