Android Studio Api Questions: Gradle nature & Parsing

Greetings!
I'm working on a plugin for the Android Studio EAP and I was curious about some aspects of the API. For example, Maven has an isMavenModule "org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule". Is there a similar attribute for gradle?  How about for Gradle/Android? (I guess, I can always parse the build.gradle files for apply 'Android' but I'm not sure if that is the most robust solution)

Also, is there any way to access Gradle ASTs via any API? IntelliJ has fantastic support for Java code manipulation, and I'd love an equivalent to the "JavaRecursiveElementVisitors" objects in Java.

Any suggestions would be greatly appreciated!

Jake

5 comments

Hi Jake,

  1. There is no dedicated utility method like 'isGradleProject()' at the moment but, having a reference to a Module object, you can check result of 'module.getOptionValue(ExternalSystemConstants.EXTERNAL_SYSTEM_ID_KEY)' (the result can be checked against GradleConstants.SYSTEM_ID) and 'module.getOptionValue(ExternalSystemConstants.LINKED_PROJECT_PATH_KEY)';
  2. We've just started implementing gradle-specific editing support. It's being implemented in terms of regular IJ groovy support at the moment but we're discussing options to expand gradle api at that directorion with gradle team;

Denis

0

Awesome!
This is helpful.

Actually, one thing I think would be really useful for plugin developers is the ability to trigger a task / evaluate a gradle script programmatiacally.  For example, I'd love to be able to programmatically extract user configured properties in their source set from their gradle script.

0

Well, you can execute a task/build project model automatically.

The first one is achieved via com.intellij.openapi.externalSystem.util.ExternalSystemUtil.refreshProject(). You should use org.jetbrains.plugins.gradle.util.GradleConstants.SYSTEM_ID as an external system id. The result is delivered to the ExternalProjectRefreshCallback that you used as an argument at ExternalSystemUtil.refreshProject(). Alternatively, you can use low-level api for doing the same job - ExternalSystemResolveProjectTask. You can check ExternalSystemUtil.refreshProject() processing for usage scenario.

Regarding task execution - it's backed by com.intellij.openapi.externalSystem.service.internal.ExternalSystemExecuteTaskTask class. You can use IJ run configuration facilties to execute gradle tasks as well.

Denis

0

Ah interesting. That seems pretty useful. Thank you again.

Is there anything like the toolingApi, http://www.gradle.org/docs/current/userguide/embedding.html , where I can query the intelliJ api for the project model of a gradle project?

As a sort of extreme case, say I wanted to read some property declared in the gradle file (e.g., I wanted to know the main.java.srcDir in from a build.gradle).  Using only the program runner util, I guess I could write a gradle plugin that adds a task which outputs the path to the user's main.java.srcDir to another file, have the user add the apply 'plugin' to their build script, then call it via ProgramRunnerUtil.executeConfiguration and trigger my gradle task, then read the output file. (although that feels a bit roundabout :) )

0

IJ uses gradle tooling api internally but we're limited to the data view offered by gradle (IdeaProject, IdeaModule etc, check GradleProjectResolver for more details). That's why there is no straightforward solution for reading a gradle script property, it should be exposed in terms of tooling api or something like the approach you described (using external storage).

Denis

0

Please sign in to leave a comment.