Skip to content

Instantly share code, notes, and snippets.

@Audhil
Last active November 10, 2022 17:32
Show Gist options
  • Save Audhil/1774ccb7e5472c721b107205c12fe181 to your computer and use it in GitHub Desktop.
Save Audhil/1774ccb7e5472c721b107205c12fe181 to your computer and use it in GitHub Desktop.
// exploring funcs & props usage in gradle file
// below code is placed in app/build.gradle
// executed tasks as `$ ./gradlew showMagics`
// https://medium.com/@dmitriileonov/5-gradle-things-that-get-android-developers-confused-ed606b8e8c92
class Foo {
def name = ""
void name(String newString) {
name = newString
println "Foo.name() triggered: name: $newString"
}
}
tasks.create("showMagics") {
group "Magic Group"
description "Check getter and setter capabilities"
doFirst {
def foo = new Foo()
foo.name = "Audhil"
println "showMagic() 1: printed: ${foo.name}"
foo.name("Humaiyya")
println "showMagic() 2: printed: ${foo.name}"
foo.name "Hafsa" // function executed
println "showMagic() 3: printed: ${foo.name}"
foo.setName("Mohammed")
println "showMagic() 4: printed: ${foo.name}"
foo.setName "Another Name"
println "showMagic() 5: printed: ${foo.name}"
String propertyOrFunctionName = "name"
foo."$propertyOrFunctionName" = "Final name"
println "showMagic() 6: printed: ${foo.name}"
foo."$propertyOrFunctionName"("UnExpected!")
println "showMagic() 7: printed: ${foo.name}"
/*
o/p
showMagic() 1: printed: Audhil
Foo.name() triggered: name: Humaiyya
showMagic() 2: printed: Humaiyya
Foo.name() triggered: name: Hafsa
showMagic() 3: printed: Hafsa
showMagic() 4: printed: Mohammed
showMagic() 5: printed: Another Name
showMagic() 6: printed: Final name
Foo.name() triggered: name: UnExpected!
showMagic() 7: printed: UnExpected!
*/
}
}
void foo(String url, String parameter) {
println url
println parameter
}
tasks.create("callFoo") {
doFirst {
foo "google.com", 'consistency'
println "New string"
}
/*
o/p:
google.com
consistency
New string
*/
}
// real world tasks - customized
//task copyApk(type: Copy) {
task copyApk(type: Copy, dependsOn: "test") { // runs unit test, then this custom task
def sourceDir = layout.buildDirectory.dir("intermediates/apk/debug/app-debug.apk")
def destDir = "$rootDir/apk"
from sourceDir
into destDir
rename "app-debug.apk", "gradle-experiment.apk"
// checksum
doLast {
def file = new File(destDir, "gradle-experiment.apk")
ant.checksum file: file.path
}
}
tasks.whenTaskAdded { task ->
// "assembleDebug" is the task executed just before .apk is assembled, it is auto triggered when we "Rebuild Project"
if (task.name == "assembleDebug") {
task.finalizedBy "copyApk" // "copyApk" task is executed once "assembleDebug" task is ran
}
}
// uncomment tasks to get outputs
// custom gradle tasks @ https://www.youtube.com/watch?v=2SWgl-OdxDY&t=2s&ab_channel=PhilippLackner
/*
o/p
> Configure project :app
Jack and jill!
Hi all! then! how!
> Task :app:helloWorld UP-TO-DATE
NOTE: these lines executed when the task is configured
*/
task helloWorld() {
println "Jack and jill!"
println "Hi all! then! how!"
}
// life cycle call backs of task
/*
o/p
> Configure project :app
Hi hello!!
> Task :app:helloWorld2
executed first!
executed last!
*/
task helloWorld2 {
doFirst {
println "executed first!"
}
println "Hi hello!!"
doLast {
println "executed last!"
}
}
// register task inside of a task(following code registers the task, it doesn't runs it),
// to run registered task use ./gradlew in terminal
task helloWorld3 {
10.times { i ->
tasks.register("task$i") {
doLast {
println "hello from task $i"
}
}
}
}
// dependsOn - running tasks inside another task
task helloWorld4 {
10.times { i ->
tasks.register("task_$i") {
doLast {
println "hello from task_$i"
}
}
}
println "I'm from helloWorld 4"
dependsOn "task_2"
println "I'm from helloWorld 4, I'm last line"
}
// more about dependsOn
task helloWorld5 {
10.times { i ->
tasks.register("task__$i") {
doLast {
println "hello from task__$i"
}
}
}
tasks.named("task__2") {
dependsOn "task__4", "task__6", "task__8"
}
dependsOn "task__2"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment