Can't lookup Gradle classes
Answered
I'm using the gradle wrapper, and the have defined the "all" distribution in the gradle-wrapper.properties:
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-all.zip
When editing a build.gradle, it picks this up, and I'm able to navigate from "task" in my gradle build to org.gradle.api.Project.task, and from there to org.gradle.api.Task.java, with source. This makes editing gradle builds very nice. However, when I try to look up a class directly (Ctrl-N). It doesn't find it, and those classes aren't "in" my project as dependencies, so I can't find them from the Project view, either.
Is there some way to view the gradle api, in the same way that you would an External Library, such that you can look up classes and browse the package structure?
Please sign in to leave a comment.
Gradle is a build system that is external to the project, it makes no sense to add Gradle classes to the project as the dependencies unless you are developing a plug-in for Gradle.
So, if you need navigation to Gradle class files, you need to make it a part of your project by adding it to the dependencies of one of the modules: https://www.jetbrains.com/help/idea/2017.1/working-with-module-dependencies.html.
I wasn't suggesting adding gradle classes to the dependencies. I said that I want to be able to look up the classes "in the same way" (perhaps I should have said "similar way") as you can look up classes in an external library. In fact, I was trying to avoid adding it as a dependency, because, yes, adding it as a dependency just to see it makes no sense.
But if you are implying that it makes no sense to be able to see the source for a gradle class while writing a build.gradle that makes use of it, then I respectfully disagree. And as I said, I can already navigate to these classes through the build.gradle (Navigate> Declaration while on "task", for example). But I need a reference to the class to start with. I don't know of a way to look up an arbitrary gradle class to view the source. It would be very convenient to be able to do that. In my opinion, what makes no sense is that I would be able to navigate into a class from a reference in a .gradle file, but not be able to look that class up by name.
You can use the IDEA completion feature for the lookup. It should suggest you the gradle api and classes available in the classpath of the build script.
So
Not pretty, but it works, sort of.
Hi...
Um, I would have to even less-respectfully disagree about this;
build.gradle files are groovy scripts, and 99% of them should be referencing org.gradle.api types.
Not having completion for these leads to perfectly valid build files filled with squiggly red lines and complaints everywhere.
The frustrating thing is that _sometimes_ it works good _for some projects_.
Please guys, this is not just annoying, it makes my superiors who are less familiar with gradle + IJ think that it's second-rate code because the IDE is complaining. Even worse: The more type safety you add to your groovy, the _worse_ the IDE errors get.
>build.gradle files are groovy scripts, and 99% of them should be referencing org.gradle.api types.
>Not having completion for these leads to perfectly valid build files filled with squiggly red lines and complaints everywhere.
For IDE to recognize them as Gradle build files (and so to be aware of the Gradle types) these build.gradle files should be imported as Gradle projects. Then there should be no "red" code. Do you have them opened as Gradle projects?
> Do you have them opened as Gradle projects?
I assume you are asking Jamie Nelson. But to be clear, I did not (and do not) have an issue with the gradle code showing errors. And they are imported as gradle projects. It was just a matter of not being able to look up various elements of the gradle api in the same way that you can a class in the project.
Also, that problem has been fixed (or maybe there was always a way to do it, and I just didn't realize it, and nobody pointed it out): If you do Ctrl-N (Navigate > Class...) and type "org.gradle.api.Copy" (or just "gradle.Copy", or even just "Copy)" it finds it, though you may have to check "Include non-project items".