Zennichimaro
Zennichimaro

Reputation: 5306

How to make tasks execute one after another?

Basically I have 4 tasks that I need to run sequentially, but I cannot make them do so, I have to run it one by one on the command line as so:

gradle :drmdexsecondary:compileReleaseJava --info --debug --stacktrace
gradle :drmdexsecondary:dexClasses --info --debug --stacktrace
gradle :drmdexsecondary:jar --info --debug --stacktrace

Here's my build.gradle:

evaluationDependsOnChildren();

    task dexClasses( type:Exec ) {
    
    //    compileJava.execute()
    
        String cmdExt = Os.isFamily(Os.FAMILY_WINDOWS) ? '.bat' : ''
    
        println("${buildDir}")
        println("${androidSdkDir}\\build-tools\\${buildToolsVersion}\\dx${cmdExt} --dex --output=${buildDir}\\classes\\classes.dex ${buildDir}\\classes\\release")
    
        commandLine "cmd", "/c", "${androidSdkDir}\\build-tools\\${buildToolsVersion}\\dx${cmdExt} --dex --output=${buildDir}\\classes\\classes.dex ${buildDir}\\classes\\release"
    }
    
    task jar(type: Jar) {
        from ("${buildDir}\\classes\\classes.dex")
    }

My problem is this:

  1. the dependsOn keyword doesn't work... it just get ignored without any log message
  2. taskname.execute() function doesn't work... it just get ignored without any log message
  3. compileReleaseJava is not recognized inside build.gradle with this particular error: Could not find property 'compileJava' on task ':drmdexsecondary:dexClasses'.

Would anyone please help?

I've consulted and copy paste from the documentation but none of them seems work. I've even tried to re-install Gradle... there is so few sample codes there and although I understand the concept, it seems so difficult to translate my intention into working Gradle code, so if there is any good resources to help, it will be very appreciated as well.

Upvotes: 32

Views: 56174

Answers (5)

Tomas Bjerre
Tomas Bjerre

Reputation: 3500

You can create a task of type GradleBuild and define the tasks inside of that task.

  task releaseAfterMath(type: GradleBuild) {
      tasks = [
        'clean', 
        'build',
        'publish',
        'publishToMavenLocal',
        'commitNewVersionTask', 
        'gitChangelogTask', 
        'commitChangelogTask'
      ]
  }

And you can trigger releaseAfterMath like any other task. Running code here:

https://github.com/tomasbjerre/gradle-scripts/

Upvotes: 5

lixiaodaoaaa
lixiaodaoaaa

Reputation: 470

The Best Answer is Here

task myTask1() {
  println("this is task1 running") 
}

task task2() { dependsOn myTask1
  println("this is task2 running") 
}

when you execute gradle task2 .

this wil first execute task1 ,and then execute task2

you should use dependsOn key words

For example


task buildMyApk(type: Exec) {
    def myCommond = ['gradle', "assembleRelease"]
    commandLine myCommond
}



task moveApkFileToProjectFolder() { dependsOn buildMyApk
    def releaseApkFilePath = "${buildDir}/outputs/apk/release/"
    def targetApkFolder = "${rootProject.getProjectDir()}/apkFolder"
    mkdir "${targetApkFolder}"
    copy{
        from releaseApkFilePath
        into targetApkFolder
    }
}

above answer will not work task moveApkFileToProjectFolder run ,will first run buildMyApk.

my example task ,first will build apks .and then move apk File to my Project/apkFile folder .and execute success.

Upvotes: 6

tschumann
tschumann

Reputation: 3246

task1.finalizedBy task2 will run task2 after task1.

Upvotes: 34

Sharif Mamun
Sharif Mamun

Reputation: 3554

For sequential or whatever the sequence you want to run your tasks, I do the following thing in my build.gradle file:

def modules = ["X", "Y", "Z", "ZZ"]

if (modules.size() > 1) {
    for(j in 1 .. modules.size()-1 ) {
        tasks[modules[j]].mustRunAfter  modules[values[j-1]]        
     }
}

Upvotes: 1

ditkin
ditkin

Reputation: 7044

You should read about gradle tasks and more about tasks. You want to depend on things rather then invoke things.

Also I think you should read about, and use the gradle android plugin.

To your original question, when you define a task the code between the braces is run at configuration time. A task's actions are run when the task is executed and must be added to the task's action list. This is done by using the task's doFirst, doLast or the << operator.

Here is an example from the gradle documentation.

task taskX << {
    println 'taskX'
}
task taskY << {
    println 'taskY'
}
task taskZ << {
    println 'taskZ'
}
taskX.dependsOn taskY
taskY.dependsOn taskZ
taskZ.shouldRunAfter taskX

Upvotes: 33

Related Questions