Exec tasks no longer working in Android Studio, but fine with gradlew

916 views
Skip to first unread message

Matt Gaunt

unread,
Feb 11, 2014, 11:00:59 AM2/11/14
to adt...@googlegroups.com
Hey everyone,

Since updating to 0.4, I've been able to run command line tools from gradle.

I used to have the following gradle build task:

task buildWebApp(type: Exec) {
    executable = Os.isFamily(Os.FAMILY_WINDOWS) ? "grunt.cmd" : "grunt"

    args = ["build"]
}

This would pick the appropriate grunt command and run it with the argument "build".

This worked without any kind of issue

In Android Studio 0.4, using Gradle 1.9 and it's no longer working. Instead I get the following error:

Execution failed for task ':BrowserPages:buildWebApp'. A problem occurred starting process 'command 'grunt''

Normally at this point I would debug with gradlew, but running the following command, throws no errors:

$ ./gradlew buildWebApp

I've also tried running the full task list to still not find any problems.

The way this is executed is from my main build.gradle file with this dependency

copyWebApplication.dependsOn ':BrowserPages:buildWebApp'

Anyone have any ideas?

I've tried to run executable = "node" with arg = ["-v"] and get the same error where it can't find node. Node is globally accessible on my terminal, so I can only assume that the command is being run in a different environment to the terminal. There is the option for setting the environment on the task but I can't find any examples of how it should be used.

Scott Barta

unread,
Feb 11, 2014, 11:45:29 AM2/11/14
to adt...@googlegroups.com
Is there some issue with your paths that it now can't find the command? Perhaps you should give it a fully-qualified path to the grunt executable. Also, do you have the same problems on all platforms?

When you execute Gradle tasks from Android Studio, it doesn't make an attempt to set the working directory, though I don't know that it's changed in recent versions -- that might also be something to investigate.


--
You received this message because you are subscribed to the Google Groups "adt-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adt-dev+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Xavier Ducrohet

unread,
Feb 11, 2014, 1:57:44 PM2/11/14
to adt...@googlegroups.com
We actually had a discussion about this recently.
The tooling api doesn't allow you to set the working dir so it's not possible.

Your build script really shouldn't make any assumption regarding the working directory.
--
Xavier Ducrohet
Android SDK Tech Lead
Google Inc.
http://developer.android.com | http://tools.android.com

Please do not send me questions directly. Thanks!

Luke Daley

unread,
Feb 11, 2014, 7:51:59 PM2/11/14
to adt...@googlegroups.com
Just chiming in on why Gradle doesn't allow you to set the working
directory…

The JVM doesn't support changing the working directory mid process. It
can be done with JNA/JNI (and Gradle does do this under certain
circumstances), but it's problematic to use extensively. Regardless,
relying on the working directory in any sense is inherently fragile.

> Xavier Ducrohet <mailto:x...@android.com>
> 12 February 2014 4:57 am
> We actually had a discussion about this recently.
> The tooling api doesn't allow you to set the working dir so it's not
> possible.
>
> Your build script really shouldn't make any assumption regarding the
> working directory.
>
>
>
>
>
> --
> Xavier Ducrohet
> Android SDK Tech Lead
> Google Inc.
> http://developer.android.com | http://tools.android.com
>
> Please do not send me questions directly. Thanks!
> --
> You received this message because you are subscribed to the Google
> Groups "adt-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to adt-dev+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
> Scott Barta <mailto:sba...@google.com>
> 12 February 2014 2:45 am
> Is there some issue with your paths that it now can't find the
> command? Perhaps you should give it a fully-qualified path to the
> grunt executable. Also, do you have the same problems on all platforms?
>
> When you execute Gradle tasks from Android Studio, it doesn't make an
> attempt to set the working directory, though I don't know that it's
> changed in recent versions -- that might also be something to investigate.
>
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "adt-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to adt-dev+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
> Matt Gaunt <mailto:matt.t...@gmail.com>
> 12 February 2014 2:00 am
> Hey everyone,
>
> Since updating to 0.4, I've been able to run command line tools from
> gradle.
>
> I used to have the following gradle build task:
>
> |task buildWebApp(type: Exec) {
> executable= Os.isFamily(Os.FAMILY_WINDOWS) ? "grunt.cmd" : "grunt"
>
> args= ["build"]

Matt Gaunt

unread,
Feb 12, 2014, 8:59:17 AM2/12/14
to adt...@googlegroups.com
So if I can't assume the working directory and I can't change the working directory (Despite there be an option to do so with the exec task), is it not possible to run tasks on the command line?

It also still confuses me how I can get this to run on the CLI with the gradle wrapper, but not Android Studio.

Just an FYI, changing the task to the following:

task buildWebApp(type: Exec) {
    workingDir '/Users/matt/Programming/Workspace/webview-browser/BrowserPages/'

    //on windows:
    commandLine 'grunt.exe', 'build'

    //on linux
    commandLine 'grunt', 'build'
}

Will run with ./gradlew buildWebApp but fails in Android Studio. 

Has anyone managed to execute shell scripts or other command line tools via Gradle run in Android Studio?

Xavier Ducrohet

unread,
Feb 12, 2014, 11:13:42 AM2/12/14
to adt...@googlegroups.com
The Android plugin uses a LOT of command line tools (aapt, dx, aidl, zipalign, renderscript compiler, ...) so it's obviously possible.

In your case, I'm guessing it might not be a working dir issue actually, but a path issue. It might simply not find grunt.

I'm guessing somehow the path my be inherited from the terminal when running from the command line, but when running from Studio the custom PATH might not be configured.

Matt Gaunt

unread,
Feb 12, 2014, 11:37:06 AM2/12/14
to adt...@googlegroups.com
Cool, so it's definitely my task and agree with everything you are saying regarding the PATH being the issue.

I guess that leaves the question of what changed to cause the PATH to differ from the terminal in 0.4 and above? I'll have a look and see if I can find out a way to get grunt to run.

Daniel Lew

unread,
Feb 20, 2014, 10:32:04 AM2/20/14
to adt...@googlegroups.com
I'm running into this still on 0.4.6.  It's definitely a PATH problem but I'm not sure how to solve it - setting up a PATH variable in the settings doesn't work.

-Daniel
Reply all
Reply to author
Forward
0 new messages