Warpzit
Warpzit

Reputation: 28162

How to run a jar before compiling with Gradle in Android Studio?

Okay so I have had several issues with running a jar before the project was build but I've finally found a solution. The jar would generate translation files based on a cvs file.

If I run ./gradlew taskname the project is build, the script runs and the files are generated. If I run from Android studio everything is fine too but if I run ./gradlew (no target) the project will be build and the script will seem to run but the files that the jar would generate doesn't appear!!!

Any ideas why this is?

Note: The jar uses mkdirs and works without any issues.

Gradle code:

task translationsTask(type: Exec) {
    println("Running jar from gradle with shell script.")
    workingDir rootDir
    commandLine './test.sh'
}
preBuild.dependsOn(translationsTask)

android {
...
}

Upvotes: 0

Views: 157

Answers (1)

ToYonos
ToYonos

Reputation: 16833

A Gradle build has three distinct phases including the configuration one and the execution one.

Your translationsTask task is an Exec task. workingDir and commandLine are here to configure it. These parameters will be used during the execution phase. The println statement written like this will be included in the configuration phase. The doFirst anddoLast closures allow to add custom actions during the execution phase.

Here is your task with proper messages :

task translationsTask(type: Exec) {
    println("Configuring the translationsTask")
    workingDir rootDir
    commandLine './test.sh'
    doFirst {
            println("Running jar from gradle with shell script, before the command line")
    }
    doLast {
            println("Running jar from gradle with shell script, after the command line")
    }
}

Only calling gradlew will only display Configuring the translationsTask (no execution phase as no target task). Calling gradlew taskname will display all messages as there is a target task and the Android preBuild task is invoked (and so is translationsTask as preBuild depends on it)

Upvotes: 2

Related Questions