21

I'm new with Gradle, so there's a chance that I am missing something obvious here. I am working on using Gradle for automated building of our projects. I've tackled most of them, but one is causing me unending trouble. Since this is for my company, I can't share any real code, but I will do my best to provide realistic pseudo-code.

I am attempting to compile a web application with a flex front-end and java back-end. Right now, I am focused solely on creating a .war file with the java in it, then I will move into the flex compilation. When I run the following build.gradle script, it gives me countless "cannot find symbol" errors.

apply plugin: 'eclipse'
apply plugin: 'war'

sourceCompatibility = 1.6

repositories {
   mavenCentral()
}

sourceSets.main.java.srcDir 'src'
sourceSets.main.resources.srcDir 'src'

webAppDirName = "WebContent"

war.archiveName "Gradle_test.war"

dependencies {
    compile fileTree(dir: 'WebContent/WEB-INF/lib', include: '*.jar')
    providedCompile 'org.apache.tomcat:catalina:6.0.13'
    providedCompile 'org.apache.tomcat:dbcp:6.0.13'
}

An example of the errors that were occurring (with fake package and class names):

/Users/user/Documents/eclipse/ProjectName/src/com/companyName/teamName/projectName/core/release/subProjectName/projectVersion/RenamedObject.java:385: cannot find symbol
symbol  : class OtherObject
location: class com.companyName.teamName.projectName.core.release.subProjectName.projectVersion.RenamedObject
    public class InnerClass extends OtherObject implements Indexable<String>, Serializable {

The class InnerClass is declared within the RenamedObject.java file along with the RenamedObject class. The symbol it cannot find, OtherObject, is not from a .jar. It is a source file stored at /Users/user/Documents/eclipse/ProjectName/src/com/companyName/teamName/projectName/core/release/subProjectName/projectVersion/superTypes/OtherObject.java. The object is imported at the top of RenamedObject.java and can be built and run properly via eclipse without any special settings. These errors occur during the :compileJava task after executing gradle war from terminal.

Is there any reason that Gradle would have an issue with handling source imports from a sub-package like this? Am I just doing something wrong? Let me know if there is any more info that can help and I will provide it if I can.

EDIT: I got it to work. All 130 of the errors were resolved with one change. If I add the fully qualified package name to OtherObject in the extends, so it looks like this:

public class InnerClass extends com.companyName.teamName.projectName.core.release.subProjectName.projectVersion.superTypes.OtherObject implements Indexable<String>, Serializable {

That one change made it all work, but I still don't understand the reason. That is not the only reference to OtherObject in the project and it is not the only place that an inner class extends a class defined in another source package.

JorganPubshire
  • 777
  • 1
  • 6
  • 16
  • I'm having a similar issue myself. Were you able to solve this? – amadib Mar 02 '15 at 04:37
  • 1
    As I said in the edit, fully qualifying the type fixed the issue, but I still have never found out why. The type name does match a type that is available in the native library, but our version is imported at the top of the java file. – JorganPubshire Mar 02 '15 at 14:01

7 Answers7

11

Symptoms

Could not execute build using Gradle distribution 'https://services.gradle.org/distributions/gradle-2.8-bin.zip'

... .... .....

Caused by: org.gradle.api.internal.tasks.compile.CompilationFailedException: Compilation failed; see the compiler error output for details.

The Gradle Build Reports that you should check the compiler Java Output.

Check the output and see which class name it cites in the output which it is unable to resolve.

symbol:  class <referenced_classname_here
location: class <parent_classname_here

Solution

Provide the Fully Qualified Domain Name wherever you reference the class that is mentioned as the symbol not resolved.

In my case, I have seen this when I defined an Inner Class which extends a class in a library that I had declared as a dependency in my build.gradle.

P.S:

I have seen that the solution for this has already been mentioned as a comment to the question by the OP.

I could not find this question since it did not have the right keywords for google and also there was no accepted solution which further reduced its visibility.

The result of this is ONE FULL DAY WASTED, several strands of hair plucked out before I realized this stupid hackish solution :( :(

I have tried everything under the Sun that is remotely related to dependency resolution to fix this "dependency issue" which was never an issue with my codebase.

Hope the Gradle Team fixes this ASAP before more people pluck their hair.

Btw... I Love Gradle. :)

acthota
  • 557
  • 5
  • 22
  • Same issue here, but with XmlRootElement. I had to put javax.xml.bind.annotation.XmlRootElement instead. The strange thing was I have two classes with the same annotation, and only one was causing this issue. – gmazzo Jul 16 '16 at 19:37
  • It's been a while since I've been on gradle (moved on to another project which uses Maven). Just curious... Is this issue fixed in the later versions of Gradle ? – acthota Jan 22 '17 at 02:00
  • I spent several hours on this before converting inner classes to top level classes :( Did not want to use FQDN in front of class names. This is still a problem with gradle 3.1 – Y2i Mar 26 '17 at 19:59
  • 2
    Just following up that this is still a problem in Gradle 4.5. – Paul Lammertsma Apr 06 '18 at 13:39
1

Just adding to the knowledge base, but I had just the same type of problem with:

public static class GetNetworkInterfacesCallable implements java.util.concurrent.Callable<java.util.Set<java.net.InetAddress>> {
    public GetNetworkInterfacesCallable(String networkPrefix) {
        this.networkPrefix=networkPrefix;
    }
    @Override public Set<InetAddress> call() throws Exception {
        Thread.currentThread().setName(getClass().getName());
        return null; //myInetAddresses(networkPrefix);
    }
    final String networkPrefix;
}

Using gradle 2.9 and 2.10.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Ray Tayek
  • 9,841
  • 8
  • 50
  • 90
1

I had same problem here today. If you are working with gradle, be sure your class is in src/main/java not src/test/java otherwise your IDE (for example Eclipse) will say that all compiles find, but when you try to build with gradle, test classes are not included by default (and it's okay!), so it will not compile.

Vokail
  • 630
  • 8
  • 27
1

Had almost the same issue: tried to build module with dependency on another one which was changed. In first module were calls to methods in second module, and these methods were not visible. Solution was to delete folders "build" and "out" in second module, rebuild it and build first module.

DistantBlue
  • 1,424
  • 1
  • 9
  • 14
0

Got the similar thing, but that's only because I turned on parallel build (org.gradle.parallel=true in ~/.gradle.properties). Weird though.

Error I got: error: package com.xxx.yyy does not exist error: cannot find symbol

Once I set org.gradle.parallel=false in ~/.gradle.properties, the world starts to seem normal again.

Wei Zhang
  • 11
  • 3
0

I get this error all the time and even though your problem is likely related to the previous answers, for me this is usually just local caching issues that gets solved by running gradle clean.

Can't hurt to try it.

0

In my case I was trying to use a Kotlin class as opposed to a Java class.

Emmanuel Conradie
  • 345
  • 1
  • 5
  • 20