My problem is simple but I don't find the solution. In my app, I have added crashlytics and I want to use beta to upload builds.
For one productFlavor I use the task called crashlyticsUploadDistribution<ProductFlavor>Release and before I have to assemble build with assemble<ProductFlavor>Release otherwise it can upload a wrong apk.
Now I want to merge this two tasks in only one so in my gradle.file
task prepareFabric(group: 'aat') {
println 'HereSTART'
dependsOn 'assemble<ProductFlavor>Release'
}
task publishRealeaseOnFabric(group: 'aat') {
mustRunAfter prepareFabric
println 'HereFINAL'
dependsOn 'crashlyticsUploadDistribution<ProductFlavor>Release'
}
Now when I execute my task (publishRealeaseOnFabric), print appears but the following line is not executed
dependsOn 'assemble<ProductFlavor>Release'
My question is : How can I make two tasks assemble<ProductFlavor>Release and then crashlyticsUploadDistribution<ProductFlavor>Release in one task ?
I use that it create all the flavors
//Creates the app for all the productFlavors
File crashlyticsProperties = new File("${project.projectDir.absolutePath}/fabric.properties")
applicationVariants.all { variant ->
variant.productFlavors.each { flavor ->
println("flavor : " + variant.name)
def variantSuffix = variant.name.capitalize()
def generatePropertiesTask = task("fabricGenerateProperties${variantSuffix}") << {
Properties properties = new Properties()
properties.put("apiKey", flavor.fabricApiKey)
properties.put("apiSecret", flavor.fabricApiSecret)
properties.store(new FileWriter(crashlyticsProperties), "")
}
def generateResourcesTask = project.tasks.getByName("fabricGenerateResources${variantSuffix}")
generateResourcesTask.dependsOn generatePropertiesTask
generateResourcesTask.doLast {
println "Removing fabric.properties"
crashlyticsProperties.delete()
}
}
}
Related
A gradle project contains several similar task type:Copy and some of them require additional checks.
task copyPackage1(type: Copy) {
from buildPackage1
into "/pkgs/"
eachFile {
if (it.relativePath.getFile(destinationDir).exists()) {
throw new GradleException("Probably version was no updated. File exists: " + it)
}
}
}
...
task copyPackage2(type: Copy) {
from buildPackage2
into "/pkgs/"
eachFile {
if (it.relativePath.getFile(destinationDir).exists()) {
throw new GradleException("Probably version was no updated. File exists: " + it)
}
}
}
How it is possible to refactor duplicated checks and specify same target directory for all similar tasks (but not all Copy tasks)?
You could either implement this with a custom Gradle plugin (as suggested in this similar question on Gradle forum), or use simple Groovy method to create and configure your tasks, as follows:
// define a "task creator" method
ext.createCopyToPkgTask = { String taskName , String fromDir ->
return project.tasks.create(taskName, Copy.class){
from fromDir
into "/pkgs"
eachFile {
if (it.relativePath.getFile(destinationDir).exists()) {
throw new GradleException("Probably version was no updated. File exists: " + it)
}
}
}
}
/* declare your similar tasks using the creator method above */
createCopyToPkgTask("copyPackage1","buildPackage1")
createCopyToPkgTask("copyPackage2","buildPackage2")
// ...
My custom task at gradle-4.10.1:
task fabricUploadApkDevelop(group: "fabric", dependsOn: ['assembleDevDebug']) {
doLast {
//fabric gradle task `assembleRelease crashlyticsUploadDistributionDevDevelop` and options:
def task = tasks.getByName("crashlyticsUploadDistributionDevDebug")
task.ext.betaDistributionGroupAliases = "develop"
task.ext.betaDistributionNotifications = true
task.ext.betaDistributionReleaseNotesFilePath = "${project.rootDir}/app/build/outputs/apk/dev/debug/releaseNotes.txt"
task.execute()
}
}
But at gradle-5.1.1:
TaskInternal.execute() is removed, so now I can't run my task.execute(), how to change my custom task? Or how to run task crashlyticsUploadDistributionDevDebug with params via terminal?
I tried to use:
//gradle 5.x
task fabricUploadApkDevelop(group: "fabric") {
ext.betaDistributionGroupAliases = "develop"
ext.betaDistributionNotifications = true
ext.betaDistributionReleaseNotesFilePath = "${project.rootDir}/app/build/outputs/apk/dev/debug/releaseNotes.txt"
doLast {
ext.betaDistributionGroupAliases = "develop"
ext.betaDistributionNotifications = true
ext.betaDistributionReleaseNotesFilePath = "${project.rootDir}/app/build/outputs/apk/dev/debug/releaseNotes.txt"
}
finalizedBy 'crashlyticsUploadDistributionDevDebug'
}
But crashlyticsUploadDistributionDevDebug task doesn't get params...
The sources for the Fabric/Crashlytics Gradle plugin don’t seem to be publicly available (and I have never used it myself), otherwise I would have checked there. But given your working example for Gradle 4 and looking at the issue through my vanilla Gradle glasses, I’d expect that the following should/could work with Gradle 5:
project.afterEvaluate {
crashlyticsUploadDistributionDevDebug.doFirst {
ext.betaDistributionGroupAliases = "develop"
ext.betaDistributionNotifications = true
ext.betaDistributionReleaseNotesFilePath = "${project.rootDir}/app/build/outputs/apk/dev/debug/releaseNotes.txt"
}
}
task fabricUploadApkDevelop(group: "fabric") {
dependsOn 'crashlyticsUploadDistributionDevDebug'
}
I’d even expect there to be a nicer way to do it but since I can’t test this myself, I wanted to play it safe. Let me know if it worked or what didn’t!
Our build has some generic logic to create tasks from configuration data. There are also several Gradle files on whose execution order I do not want to depend.
Now I need to add a task dependency to a task without knowing if that task will be defined before my part of the build script runs (at configuration time) or after my part.
[Edit: as #canillas writes I can say myTask.dependsOn "otherTask" and "lazily" depend on yet undefined "otherTask", but I need it the other way around.]
Since I can't write tasks["otherTask"].dependsOn myNewTask before "otherTask" is defined, I made the following helper function:
void safelyDoWithTask(String taskName, Closure func) {
def task = tasks.findByName(taskName)
if (task != null) {
func(task)
} else {
project.tasks.whenTaskAdded { task_ ->
if (taskName.equals(task_.getName())) {
func(task_)
}
}
}
}
safelyDoWithTask('otherTask', { it.dependsOn myNewTask })
Now I wonder if there is more canonical way to achieve this?
Consider the following:
// section 1
def dynamicDependencies = [:]
dynamicDependencies['otherTask'] = 'myNewTask'
// section 2
afterEvaluate { project ->
// taskA dependsOn taskB
dynamicDependencies.each { taskA, taskB ->
def task = project.tasks.findByName(taskA)
if (task) { task.dependsOn "${taskB}" }
}
}
// section 3
task myNewTask() {
doLast {
println 'myNewTask !'
}
}
task otherTask() {
doLast {
println 'otherTask !'
}
}
The gist is:
section 1 defines our dependency info in a custom map
section 2 uses the afterEvaluate hook to process the map
because the above is decoupled from the task definitions, we can simply put them in section 3 (or wherever)
The following task (in build.gradle of an app's module) seems to run always before the apk is produced:
android.applicationVariants.all { variant ->
if (variant.buildType.name == 'release') {
def releaseBuildTask = tasks.create(name: "debug") {
println(".................... test ..............................")
}
releaseBuildTask.mustRunAfter variant.assemble
}
}
Could anyone offer a tip on how to run a task after the apks are produced?
Android tasks are typically created in the "afterEvaluate" phase.
Starting from gradle 2.2, those tasks also include "assembleDebug" and
"assembleRelease". To access such tasks, the user will need to use an
afterEvaluate closure:
afterEvaluate {
assembleDebug.dependsOn someTask
}
source: https://code.google.com/p/android/issues/detail?id=219732#c32
try add this in you app/build.gradle
assembleDebug.doLast {
android.applicationVariants.all { variant ->
if (variant.buildType.name == 'release') {
def releaseBuildTask = tasks.create(name: "debug") {
println(".................... test ..............................")
}
releaseBuildTask.mustRunAfter variant.assemble
}
}
println "build finished"
}
invoke the build command and specify the task assembleDebug
./gradlew assembleDebug
I found a solution that works, to copy the release APK into the project root automatically on build completion.
android {
...
task copyReleaseApk(type: Copy) {
from 'build/outputs/apk'
into '..' // Into the project root, one level above the app folder
include '**/*release.apk'
}
afterEvaluate {
packageRelease.finalizedBy(copyReleaseApk)
}
}
I have a simple custom task that cleans up some local properties files to work for the given dev environment. I'd like Gradle to run this task every time I run any command, e.g., test, build, clean, etc.
Is there a way to do so?
The standard way is to declare the dependency on every task such as:
task myclean() << {
println "myclean"
}
task mytask1(dependsOn: myclean) << {
println "mytask1"
}
task mytask2(dependsOn: myclean) << {
println "mytask2"
}
Alternatively you can add a dependency to every task after they have been declared:
task myclean() << {
println "myclean"
}
task mytask1() << {
println "mytask1"
}
task mytask2() << {
println "mytask2"
}
projects.tasks.findAll { it != myclean }.each { it.dependsOn << myclean }