Create custom gradle task to build and send apk to firebase - android

I want to create a gradle task that will build the apk and send it to firebase. I tried something like this:
task sendBuildsForSomething() {
doLast {
assembleSomething//create APK
appDistributionUploadSomething//distribute it to firebase
}}
But it won't find the property. Am I missing something, is this even possible?
Every piece of documentation is helpin me.
This task in in the projects gradle, and the tasks im trying to call are located in module gradle

Related

What is type in Gradle task?

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.

Run a gradle task before resolving dependencies

I want to run a gradle task that fetches additional sources and sets them up before gradle tries to resolve dependencies.
In build.gradle there is a task that fetches a sub project's source code. The task needs to be run before Gradle tries to resolve dependencies, because the sub project is part of the dependencies. The task involves fetching sources from a remote repository and replacing a few build.gradle files to make the build possible.
What happens now is that:
I run the task.
Gradle tries to resolve dependencies before actually running the task.
It fails because one of the dependencies requires the sub project (the sources that my task is supposed to fetch).
Of course, resolving dependencies is part of the "Configuration" build phase, so it's pretty clear why the task is run after. The question is how to make it run before.
Of course I can make it work if I replace my gradle task with a separate bash script and run it manually before gradle does anything. However, that would mean that I duplicate some variables in the gradle and the bash scripts (like version names and git tag names). Those variables are used for other purposes in gradle, and having them in two places is bad. There are other reasons I want to avoid that, one of them being - using a bash script would mean that gradle fails at doing our build from start to finish...
Firstly you are incorrect that resolving dependencies is part of the "Configuration" phase. If you make use of the lazy evaluation of FileCollection then it will actually be resolved in the execution phase. A configuration will be resolved the first time that resolve() is invoked. Please see the javadoc for the methods which cause a Configuration to be resolved. AFAIK the core gradle code won't resolve a configuration in the "Configuration" phase but your custom code may cause this (I suggest you refactor if this is the case)
You can do something like this:
dependencies {
// this is lazy evaluated
compile fileTree(dir: "$buildDir/dynamicJars", include: "*.jar")
}
task getDynamicJars(type: Copy) {
from zipTree('path/to/somefile.zip')
into "$buildDir/dynamicJars"
}
compileJava.dependsOn getDynamicJars

Where to define gradle task so that it doesn't run with every build

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/

Gradle - Which task creates the 'build' directory?

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.

Android Studio - gradle task to execute after SYNC

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

Categories

Resources