Tiago Tiago - 3 months ago 11
Java Question

Gradle - How to set it up so it displays which file the error is?

More often than not gradle does not show in which class a certain error is.

For instance, I have a class

A.java
which uses my other class
Utils.java
. Therefore, A.java has the following import:

import my.package.Utils;


If I go to class
A
, delete the import above and clean the project, gradle gives me the following message:

Information:25/08/15 9:18 AM - Compilation completed with 1 error and 0 warnings in 22s 800ms
Error:Gradle: Execution failed for task ':app:compileBauDebugAspectj'.
> Utils cannot be resolved


The problem is that message does not say anywhere the problem is in class A. So the only way of me finding that out is by manually opening all classes that uses my Utils class until I find out the one in which the import is missing (in this case, A).

Is there a way to make gradle say exactly which class the error is? I can't believe it throws such generic errors without pinpointing where they are (especially in these basic cases like missing an import).

Answer

If you are using Android Studio, use Build > Rebuild Project, not Build > Clean Project.

Using Rebuild Project, or command-line Gradle builds, will happily point out the specific location of errors. For example, here is the output of a missing import from the Gradle Console in Android Studio 1.3.1:

Executing tasks: [clean, :app:compileDebugSources, :app:compileDebugAndroidTestSources]

Configuration on demand is an incubating feature.
:app:clean
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportSupportV132300Library
:app:prepareComAndroidSupportSupportV42300Library
:app:prepareDebugDependencies
:app:compileDebugAidl
:app:compileDebugRenderscript
:app:generateDebugBuildConfig
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources
:app:mergeDebugResources
:app:processDebugManifest
:app:processDebugResources
:app:generateDebugSources
:app:processDebugJavaRes UP-TO-DATE
:app:compileDebugJavaWithJavac
/home/mmurphy/stuff/CommonsWare/projects/andprojector/app/src/main/java/com/commonsware/andprojector/MainActivity.java:40: error: cannot find symbol
    EventBus.getDefault().registerSticky(this);
    ^
  symbol:   variable EventBus
  location: class MainActivity
/home/mmurphy/stuff/CommonsWare/projects/andprojector/app/src/main/java/com/commonsware/andprojector/MainActivity.java:45: error: cannot find symbol
    EventBus.getDefault().unregister(this);
    ^
  symbol:   variable EventBus
  location: class MainActivity
Note: /home/mmurphy/stuff/CommonsWare/projects/andprojector/app/src/main/java/com/commonsware/andprojector/MainActivity.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
2 errors

 FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 1.971 secs

You get equivalent output from running gradle assembleDebug at the command line.