Is there a way to execute gradle task once after project Sync with Gradle files is complete?
I've tried to set task dependency to preBuild, as I've seen gradle:build is triggered when Sync is executing. But the problem is that dependency doesn't seem to work, task is not executed and I have to manually start the task after each Sync.
This is basically what I've tried so far
apply plugin: 'com.android.library'
...
task myTask {
...
}
gradle.projectsEvaluated {
preBuild.dependsOn(myTask)
}
I've also tried to set task dependency to other tasks that I see are triggered (:generate{Something}), but that wasn't successful either.
Is there anything I can to do force the gradle task to be executed after each Sync?
I'm using Gradle 2.2.1 + Android Studio 1.0.2
Finally, I've managed to trigger the task on every Sync event.
Apparently gradle.projectsEvaluated is either not executed at all when syncing, or it is executed after build task, so the solution is to get rid of it completely
apply plugin: 'com.android.library'
...
task myTask {
...
}
preBuild.dependsOn(myTask)
Inside the Gradle menu (usually located on the upper right corner of Android Studio), there is a list of tasks. By right clicking on the task, it is possible to set Execute After Sync.
Some while ago JetBrains extended their idea gradle plugin and now you can write something like
idea.project.settings {
taskTriggers {
afterSync tasks.getByName("myTask")
}
}
You must apply the plugin, such as
plugins {
id "org.jetbrains.gradle.plugin.idea-ext" version "0.7"
}
according to docs:
A Gradle build has three distinct phases
Initialization...
Configuration During this phase the project objects are configured.
The build scripts of all projects which are part of the build are
executed.
Execution...
Our build.gradle files are executed during Configuration phase. Task is a class (we extend org.gradle.api.DefaultTask when developing them). So let's just call our task's execute method:
task myStandaloneTask(type: MyStandaloneTaskImpl){
println("myStandaloneTask works!")
}
// let's call our task
myStandaloneTask.execute()
task myInsideGradleTask {
println("myInsideGradleTask works!")
}
// let's call our task
myInsideGradleTask
where MyStandaloneTaskImpl is a Task developed in buildSrc or as Standalone project, details
P.S. No need to use parenthesis () after myInsideGradleTask because of Groovy
Related
We have an Android project which requires a certain Gradle Plugin Task to run before we build the APK. (The plugin is written by us)
We want to run the task automatically before every build.
If we use the deprecated task.execute() then we get a warning that it will be unavailable starting with version 5.0 or something like that.
If we use the dependsOn as recommended, then testTask1 is not before BUILD, but only after CLEAN. (all explained in the comments below)
I've been over the gradle docs, and many other SO threads, but I have yet to find a solution.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
flatDir { dirs 'libs' }
jcenter()
google()
}
dependencies {
classpath "com.android.tools.build:gradle:3.1.3"
// our platform-tools plugin, in charge of some gradle tasks
classpath 'sofakingforevre:test-plugin:1.0-SNAPSHOT'
}
}
apply plugin: 'test-plugin'
allprojects {
repositories {
jcenter()
google()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
// OPTION 1 - USING EXECUTE()
// this task works as expected when calling "clean", but also when calling "assemble".
// the problem here is that the "execute" method has been deprecated, and we want to prepare for Gradle 5.0
// CLEAN - testTask1 is called :)
// BUILD - testTask1 is called :)
// DEPRECATION WARNING :(
task buildPlatformExecute {
println("executing")
// this task is created by the plugin
tasks.getByName("testTask1").execute()
}
clean.dependsOn buildPlatformExecute
// OPTION 2 - USING DEPENDSON()
// this tasks works as expected when calling "clean", but DOES NOT WORK when calling "assemble".
// If we call we call assemble, the "executing" text does print, but "testTask1" would not run.
// CLEAN - testTask1 is called :)
// BUILD - testTask1 is NOT CALLED :(
task buildPlatformDependency {
println("executing")
// this task is created by the plugin
dependsOn 'testTask1'
}
clean.dependsOn buildPlatformDependency
Issues with your OPTION 1 solution
you are using deprecated task.execute() API (you are already aware of that)
you are mixing configuration and execution phases (a common Gradle mistake...):
Since you did not wrap tasks.getByName("testTask1").execute() in a doLast {} of doFirst {} block in buildPlatformExecute task : the testTask1 task will always be executed, whatever task you will invoke. You do not even need to create dependency between clean task and your custom task ( e.g.: try to execute simple "help" task with./gradlew help you will see that testTask1 is also executed: this is certainly not what you want)
More information here : https://docs.gradle.org/current/userguide/build_lifecycle.html
Issue with your OPTION2 solution
You created a dependency between clean task andbuildPlatformDependency task :
when executing clean tasks, testTask1 task will be executed as expected, but
there is no dependency between build (or assemble) task and clean task: that's why when you execute build task, clean task is not executed (so testTask1 won't be triggered)
Solution
The best approach would be to hook you custom task testTask1 in the correct place in your project build lifecycle, using Task.dependsOn API. The "correct" place depends on what your task is responsible for in the build process: for example if your task needs to be executed BEFORE assemble task, simply create dependency assemble.dependsOn testTask1
Hi, I'm almost new at Gradle in Android.
So.. I'm leanring the Gradle but there are many things that I don't understand.
task clean(type: Delete) {
println "task clean~~"
delete rootProject.buildDir
}
In project A when I input "gradlew" in Android Studio Terminal,
I can see below result.
C:\Users\xxxx\AndroidStudioProjects\ProjectA>gradlew
> Configure project :
task clean~~
(..snip..)
I have questions:
Why is the task clean executed?
Is it default task?
I couldn't find something like below code in the project's gradle file.
defaultTasks 'clean', 'run' .
What is type in task? (I saw type "Copy" too) .
Well task clean is a default task of gradle. It runs every time you start your android studio. To know the working of gradle you should read this gradle documentation which will surely help you in your questions:
Gradle Docs
And for the second point according to gradle documentation:
A Task represents a single atomic piece of work for a build, such as compiling classes or generating javadoc.
So actually it's a task not type, type is a general term and to be specific task type belongs to central types which are used in Gradle scripts. If you go through the documentation you will also find the type:Copy too.
After updating Android Studio to version 2.2 and the gradle plugin to 2.2.0, I get following error:
Error:(32, 1) A problem occurred evaluating project ':jobdispatcher'.
Could not get unknown property 'assembleRelease' for project ':jobdispatcher' of type org.gradle.api.Project.
The problem is in the build.gradle file of an imported jobdispatcher module:
task aar(dependsOn: assembleRelease)
What changes can I make to fix this?
Note, this issue is very similar to, but still a bit different to, that reported here.
Move your dependency dependsOn inside your gradle task like shown below:
task aar() << {
dependsOn 'assembleRelease'
}
Just add "" like this to fix your problem:
from:
task aar(dependsOn: assembleRelease)
to:
task aar(dependsOn: "assembleRelease")
I tried all the previous answers, all are not working. Here is the one working after gradle 2.2.
Starting from 2.2, those tasks also include "assembleDebug" and "assembleRelease". To access such tasks, the user will need to use an afterEvaluate closure:
afterEvaluate {
task aar(dependsOn: assembleRelease) {
//task
}
}
task aar {
....
}
aar.dependsOn('assembleRelease')
and task aar will run after task "assembleRelease" finished~
wish this will help you~ :-D
I had the same problem.
Disabling instant run under Android Studio/Preferences/Build, Execution, Deployment/Instant Run worked for me.
I have defined a gradle task that increments version code.
The thing is that it is in the app.gradle and it runs with every build.
My goal is to make it run on demand from our CI server.
Just make your task depends on a special task. In your case:
task incVersionCode(dependsOn: ["assembleRelease"]) << {
// your task code
}
you can also use this android gradle plugin for auto increasing version code:
https://github.com/moallemi/gradle-advanced-build-version/
I'm using Android Studio 1.3.2 on Mac.
Gradle version is listed as 2.2.1, Android Plugin version 1.3.1.
I have applied the FindBugs Gradle plugin, and I've created a task that successfully runs the analysis on the directory 'build/intermediates/classes'.
To trigger this task on Gradle Sync, I've added it as a dependency to the preBuild task, like this:
preBuild.dependsOn findBugs
The problem with this dependency is that, at time of preBuild, the generated class files are either non-existent (first sync) or stale (remaining from previous sync). Basically, I want my task to run immediately after the 'build/intermediates/classes' directory is created, or when the files there are refreshed as part of the 'Sync' operation.
Looking at the tasks available, I can see the 'clean' task has the following description:
clean - Deletes the build directory.
However, none of the other tasks I see describe the directory's creation. My first thought was "Well, it's got to be the build task, right?". Unfortunately, as usual, it's not that simple (pressing gradle 'sync' button, does not trigger my task when I've added it as a dependency to the 'build' task). Is no such task available? If so, which task would best suit what I'm trying to achieve?
You can see all tasks using ./gradlew tasks
The build task is the main one. It builds everything, generates APKs and runs all checks.
I recommend you adding the following to the build.gradle file:
check.dependsOn 'findbugs'
So when you run the check task, it will execute findbugs as well.
Then also set a dependency on findBugs for the compilation task:
task findbugs(type: FindBugs, dependsOn: 'compileDebugSources') {...}
This will compile the source in case you run findbugs without previously compiling it.