0

I wanted to run tasks by running scripts through Gradle with all the tasks run before compilation to automate generation of artifacts to run the Android app.

Tasks:

  1. I want to run a .cmd file that generates artifacts
  2. Copy the generated artifacts to the Android assets folder

Here's the module build.gradle:

task createArtifacts(type:Exec) {
    commandLine 'cmd', '/c', "$rootDir\\create-artifacts.cmd"
    workingDir "$rootDir"
}

task copyAssets(type: Copy) {
    from "$rootDir/../Artifacts/assets"
    into "src/main/assets"
}

afterEvaluate {
    android.applicationVariants.all { variant ->
        variant.javaCompiler.dependsOn[copyAssets] //How do I make this multiple in this part?
    }
}

So I want to do the task createArtifacts before copyAssets inside the afterEvaluate.

My reference is here.

Compaq LE2202x
  • 2,030
  • 9
  • 45
  • 62

2 Answers2

3

Let's say you have default build variants : debug and release.

This is how I do into my projects:

afterEvaluate {
   if (project.hasProperty("assembleRelease")) {
      assembleRelease.dependsOn copyAssets
      copyAssets.dependsOn createArtifacts
   }
   if (project.hasProperty("assembleDebug")) {
      assembleDebug.dependsOn copyAssets
      copyAssets.dependsOn createArtifacts
   }
}
punchlag
  • 238
  • 2
  • 6
  • I want to make a successive call of the tasks, making the copyAssets run after createArtifacts. – Compaq LE2202x Sep 19 '17 at 09:04
  • This is what the code I've posted is supposed to do. The build depends on "copyAssets" which depends on "createArtifacts". Thus "createArtifacts" is run before "copyAssets". – punchlag Sep 19 '17 at 09:12
1

I solved this by creating another task called order and call that final task inside afterEvaluate to ensure the order is followed.

task createArtifacts(type:Exec) {
    commandLine 'cmd', '/c', "$rootDir\\create-artifacts.cmd"
    workingDir "$rootDir"
}

task copyAssets(type: Copy) {
    from "$rootDir/../Artifacts/assets"
    into "src/main/assets"
}

task order {
    dependsOn 'createArtifacts'
    dependsOn 'copyAssets'
    tasks.findByName('copyAssets').mustRunAfter 'createArtifacts'
}

afterEvaluate {
    android.applicationVariants.all { variant ->
        variant.javaCompiler.dependsOn(order)
    }
}

Now, the batch scripts are run before the actual app build is done so my dependencies are always updated everytime I build the app.

Compaq LE2202x
  • 2,030
  • 9
  • 45
  • 62