Android Jacoco Test with Gradle dependsOn order with compileDebugSources and compileDebugTestSources - android

Other places like This Other Question will have a jacocoTestReport task that depends on testDebug. Which works.
task jacocoTestReport(type: JacocoReport, dependsOn: "testDebug") {
...
}
However, in my case, I need to also depend on the android tasks compileDebugSources and compileDebugTestSources in addition to the testDebug. So I was hoping that the following would work
task jacocoTestReport(type: JacocoReport, dependsOn: ["compileDebugSources", "compileDebugTestSources", "testDebug"]) {
...
}
However, when I use the dependsOn property or method, the order of these dependencies aren't guaranteed as stated by Gradle. So I wanted to do something like the following outside of the task
testDebug.mustRunAfter compileDebugSources
testDebug.mustRunAfter compileDebugTestSources
but I get a compilation problem with the above is that testDebug or compileDebugSources or compileDebugTestSources gets marked as being "Could not find property 'testDebug/compileDebugSources/compileDebugTestSources'" on project :app
I'm wondering what I need to do in order to add the order of these tasks for my existing jacocoTestReport task in gradle.

Try this:
task jacocoTestReport(type: JacocoReport, dependsOn: ["compileDebugSources", "compileDebugTestSources", "testDebug"]) { ... }
tasks.testDebug.dependsOn(compileDebugTestSources)
tasks.compileDebugTestSources.dependsOn(compileDebugSources)

Related

Gradle Task dependsOn multiple parameters

I have 3 gradle tasks:first,second,third.
task first {
doLast {
println "+++++first+++++"
}
}
task second {
doLast {
println "+++++second+++++"
}
}
task third(dependsOn :[second,first]) {
doLast {
println "+++++third+++++"
}
}
execute 'gradle third' in cmd,the result is
E:\android\GradleTest>gradle third
> Task :app:first
+++++first+++++
> Task :app:second
+++++second+++++
> Task :app:third
+++++third+++++
dependsOn task order is [second,first],But why execute task order is "+++++first+++++" " +++++second+++++"?
When declaring Task Dependencies using dependsOn like that you're not specifying an order; you're just saying third depends on both one and two. Gradle can then look at the dependency graph of all the tasks in the build and decide in which order to run them all.
If you also have a dependency between one and two you should declare it explicitly.
You can do more subtle things with task ordering using mustRunAfter and shouldRunAfter but these are intendend for special cases only, and are rarely what you want. Just focus on the dependencies and let Gradle worry about the ordering.

Checkstyle Plugin does not add gradle tasks

i want to use checkstyle plugin in my gradle project, the gradle documentation says that it will add a few tasks:
https://docs.gradle.org/current/userguide/checkstyle_plugin.html
checkstyleMain, checkstyleTest, checkstyleSourceSet
I added this into my app build.gradle file:
apply plugin: 'checkstyle'
I want to run gradle task from cmd to perform code style check, but there are no one checkstyle task. I checked the whole list by typing:
./gradlew tasks
I also tried to add checkstyle jar as library dependency to app module.
Can anyone tell me what i am doing wrong and how can i get my checkstyle tasks?
Well, the checkstyle plugin adds its tasks in the Other tasks virtual task group. Those are really the tasks which have not been assigned to a task group. So, they are shown only when you run ./gradlew tasks --all (note the --all).
Complete working build.gradle:
apply plugin: 'java';
apply plugin: 'checkstyle';
Then run ./gradlew tasks --all, output:
<...snip...>
Other tasks
-----------
checkstyleMain - Run Checkstyle analysis for main classes
checkstyleTest - Run Checkstyle analysis for test classes
<...snip...>
If you want the Checkstyle tasks to appear without --all, then assign them to a task group, for example:
tasks.withType(Checkstyle).each {
it.group = 'verification'
}
Looking at other Android projects, built with Gradle, that run checkstyle (thanks Square) I found that I needed to do some setup for the task to appear. Without the task declaration I would still see my initial error.
build.gradle:
...
apply plugin: 'checkstyle'
...
checkstyle {
configFile rootProject.file('checkstyle.xml')
ignoreFailures false
showViolations true
toolVersion = "7.8.1"
}
task Checkstyle(type: Checkstyle) {
configFile rootProject.file('checkstyle.xml')
source 'src/main/java'
ignoreFailures false
showViolations true
include '**/*.java'
classpath = files()
}
// adds checkstyle task to existing check task
afterEvaluate {
if (project.tasks.getByName("check")) {
check.dependsOn('checkstyle')
}
}
You also need a checkstyle configuration file, either by placing one at the default location as documented or by configuring it explicitly.
For example:
checkstyle {
config = resources.text.fromFile('config/checkstyle.xml')
}

Task not found in root project

I am confused by gradle build lifecycle for a few days. Refer tells me
A Gradle build has three distinct phases.
Initialization
Configuration
Execution
Is task creation in the third step? If so, how does gradle find all tasks in a project object?
Here is an example.
rootporject
|----app
|----build.gradle
|----checkstyle.gradle
The build.gradle is as simple as normal.checkstyle.gradle file has a task. Here is its content.
apply plugin: 'checkstyle'
task checkstyle(type:Checkstyle) {
description 'Runs Checkstyle inspection against girl sourcesets.'
group = 'Code Quality'
configFile rootProject.file('checkstyle.xml')
ignoreFailures = false
showViolations true
classpath = files()
source 'src/main/java'
}
After ./gradlew -q tasks, there is no task checkstyle.
But if I remove the definition into build.gradle, I get it.
Is there anything wrong?Thanks in advance.
Edit
From doc
There is a one-to-one relationship between a Project and a build.gradle file.
You haven't applied your other script.
In your build.gradle you should add an apply from: 'checkstyle.gradle'.

Error:Could not find property 'assembleDebug' on project ':app'

I am using 'com.android.tools.build:gradle:2.2.0-alpha6' and Android Studio 2.2 Preview 6. The build runs perfectly fine on Gradle 2.1.0, but to enable instant run it asks me to update Gradle plugin.
On updating Gradle plugin, the build shows "Error:Could not find property 'assembleDebug' on project ':app'". I already tried cleaning .gradle and .idea and reloading the project, but nothing works.
Please help.
find which task is depending on assembleDebug task
changing the following did the trick for me at least:
from:
task findbugs(type: FindBugs, dependsOn: assembleDebug)
to:
task findbugs(type: FindBugs, dependsOn: "assembleDebug")
so just surrounding the task with quotes was enough.
It's from In that case, a workaround is this way:
//assembleDebug.doFirst {
// println '=============assembleDebug============='
//}
//assembleRelease.doFirst {
// println '=============assembleRelease============='
//}
//
// =======>
tasks.whenTaskAdded { task ->
if (task.name == 'assembleDebug') {
//task.dependsOn 'checkstyle', 'findbugs', 'pmd', 'lint'
println '=============assembleDebug============='
} else if (task.name == 'assembleRelease') {
//task.dependsOn 'checkstyle', 'findbugs', 'pmd', 'lint'
println '=============assembleRelease============='
}
}
If you have no any "assemble" in your project, so check an answer from this post:
Could not get unknown property 'assemble'

use packageReleaseJar in depends on

I have a Gradle task that depends on other tasks. For instance:
//Dependent tasks will be executed first before executing requested task
makeJar.dependsOn(clearJar)
makeJar and clearJar are the task which I defined as follow:
task clearJar(type: Delete) {
delete 'build/outputs/myProject.jar'
}
task makeJar(type: Copy) {
def someString = 'build/intermediates/bundles/release/'
from(someString)
into('build/outputs/')
include('classes.jar')
rename ('classes.jar', 'myProject.jar')
}
I want to add another dependency to makeJar task. Gradle has a task called packageReleaseJar which I want to use.
Following script fails:
makeJar.dependsOn(clearJar,packageReleaseJar)
Do you know how can I use packageReleaseJar using dependsOn ?
This worked for me:
makeJar.dependsOn(clearJar, ':ModuleName:packageReleaseJar')
We can also add dependsOn when we define the task such as :
task makeJar(type: Copy, dependsOn: ':ModuleName:packageReleaseJar') {
...
}

Categories

Resources