1

tl;dr: Custom task in app's build.gradle runs okay when building from the command-line, but is not run when building in Android Studio. Why?

Details:

I have an Android Studio project to which I have added a custom task which runs an external script. Everything works fine when I build from the command-line using the gradle wrapper, but the script is not being run when I build it under Android Studio.

Here is the app's build.gradle file with the custom buildData task:

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "ie.wattless.wattapp"
        minSdkVersion 14
        targetSdkVersion 19
    }

    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    task buildData(type:Exec) {
        workingDir '.'
        commandLine './src/scripts/dummy.sh'
    }

    gradle.projectsEvaluated {
        preBuild.dependsOn(buildData)
    }
}

dependencies {
    // This seems to fix a build problem described here:
    // http://stackoverflow.com/questions/24438170/manifest-merger-failed-uses-sdkminsdkversion-14
    compile 'com.android.support:support-v4:20.0.0'
    // do not use dynamic updating.
    //compile 'com.android.support:support-v4:+'
}

I have replaced the original script with this one, to keep things simple:

#!/bin/bash

echo 'Dummy script running.'

touch /tmp/dummy

I can do a command-line build using the gradle wrapper:

MBP2:app lorcan$ ls /tmp/dummy
ls: /tmp/dummy: No such file or directory
MBP2:app lorcan$ ../gradlew :app:assembleDebug
Relying on packaging to define the extension of the main artifact has been deprecated and is scheduled to be removed in Gradle 2.0
:app:buildData
Dummy script running.
:app:preBuild
:app:compileDebugNdk
:app:preDebugBuild
:app:checkDebugManifest
:app:preReleaseBuild
:app:prepareComAndroidSupportSupportV42000Library
: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:compileDebugJava
:app:preDexDebug
:app:dexDebug
:app:processDebugJavaRes UP-TO-DATE
:app:validateDebugSigning
:app:packageDebug
:app:zipalignDebug
:app:assembleDebug

BUILD SUCCESSFUL

Total time: 9.459 secs
MBP2:app lorcan$ ls /tmp/dummy
/tmp/dummy

So, the script was run; you can see the lines :app:buildData and Dummy script running. near the start of the output, and the /tmp/dummy file exists.

I can then clean everything down with:

../gradlew clean && git clean -f -x && rm /tmp/dummy

Then I start up Android Studio on the project, and click the "Run" button on the toolbar. This is the output that appears in the Gradle Console window:

Executing tasks: [:app:assembleDebug]

Configuration on demand is an incubating feature.
Relying on packaging to define the extension of the main artifact has been deprecated and is scheduled to be removed in Gradle 2.0
:app:preBuild
:app:compileDebugNdk
:app:preDebugBuild
:app:checkDebugManifest
:app:preReleaseBuild
:app:prepareComAndroidSupportSupportV42000Library
: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:compileDebugJava
:app:preDexDebug
:app:dexDebug
:app:processDebugJavaRes UP-TO-DATE
:app:validateDebugSigning
:app:packageDebug
:app:zipalignDebug
:app:assembleDebug

BUILD SUCCESSFUL

Total time: 10.089 secs

Note the absence of these lines:

:app:buildData
Dummy script running.

which appeared when building on the command-line. And afterwards, the /tmp/dummy file does not exist. Clearly, the script is not being run.

Any ideas why?

In case it matters, I'm using the latest version of Android Studio (0.8.2) and it is running on a MacBook Pro on OS X 10.9.4.

This is my first StackOverflow question. Constructive criticism welcome. :-)

Thanks, in advance.

Lorcan
  • 11
  • 1
  • It feels like `gradle.projectsEvaluated` is not executing. Try moving it below the `android` closure, rather than having it inside the `android` closure. – CommonsWare Aug 01 '14 at 11:32
  • Thanks for the suggestion, @CommonsWare, but I tried that and sadly it made no difference. I moved the gradle.projectsEvaluated block outside the Android one - the behaviour from both the command-line and Android Studio was still exactly as described above. I also tried moving the buildData task outside the android block and that too made no difference. – Lorcan Aug 01 '14 at 13:24
  • 1
    Just to be clear, for the benefit of others who have the same problem; I "fixed" it by simply removing the `gradle.projectsEvaluated` wrapper around the `dependsOn` stuff. So the relevant bit now looks like: ` task buildData(type:Exec) { workingDir '.' commandLine './src/scripts/dummy.sh' } preBuild.dependsOn(buildData) }` – Lorcan Aug 01 '14 at 14:02
  • The right way is to flag the question as a duplicate, which I just did here, to help steer people to the right answer. I'm a bit surprised that `projectsEvaluated` behavior differs between command-line and Android Studio, but I'm glad to hear that it worked for you! – CommonsWare Aug 01 '14 at 15:49

0 Answers0