1

I would like to get a notification when Gadle finish to build my app,

Please note that I'm not very familliar with Gradle except for the usual statements used in Android Studio but I'm glad I could learn something.

The best way I've seen so far seems to use announce and build-announcements plugins, as it is stated in this link. Unfortunately it is meant for Gradle in general and I cannot see how to adapt it to Android Studio's builds system. BTW, I'm on windows and have Snarl installed but no clue about how to make it work with Android Studio.

As in the tutorial, I applied both plugins to my app/build.gradle.

I first tried to adapt the code from this SO question. Since there was no real infromation about it, I wrote these lines at the root of the gradle file.

assembleRelease.doLast {
    announce.local.send "Gradle Info Task", 'Running'
    println gradle.gradleVersion
    announce.announce("helloWorld completed!", "local")
}

However, Gradle won't even sync, throwing this error:

Could not get unknown property 'assembleRelease' for project ':myApplication' of type org.gradle.api.Project.

I then tried to create a task like seen in this other SO question:

task notification() {
        announce.local.send "Gradle Info Task", 'Running'
        println gradle.gradleVersion
        announce.announce("helloWorld completed!", "local")
}
build.finalizedBy(notification) //

This doesn't throw any error but no notification is showing.

Why did my attempts failed ? How could I achieve my goal ?

If possible, information about how I should have searched to find this information myself is very welcome.

Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92
  • Anyone downvoting this question should at least explain why... How am I supposed to improve it if I don't know what is wrong ? – Dan Chaltiel Nov 12 '18 at 08:19

3 Answers3

1

The easiest way to achieve what you need :

In Android Studio, go into Preferences > Appearance & Behavior > Notifications, go down to Gradle Build (Logging) and check the Read aloud box.

This will speak Gradle build finished in x minutes and x seconds when your build is complete.

Pouya Heydari
  • 2,496
  • 26
  • 39
0

you need to add plugins announce and build announcements. the following destinations are supported: Twitter, notify-send (Ubuntu), Snarl (Windows), Growl (macOS). those are the plugins required:

rootProject {
    apply plugin: "announce"
    apply plugin: 'build-announcements'
}

and to finalize the build process (see supported notification services):

// it finalizes :assemble
task finalizeBuild {
    doLast {
        println(":finalizeBuild > doLast")
        announce.announce("task :assemble completed!", "snarl")
    }
}

tasks.whenTaskAdded { task ->
    if (task.name == "assembleDebug") {
        task.finalizedBy finalizeBuild
    } else if (task.name == "assembleRelease") {
        task.finalizedBy finalizeBuild
    }
}
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • Thank you for your answer but I already have added these 2 plugins, as stated in the question. I also know all these things as I obviously read all links I provided. I deited my question so my platform is known. If you want to help me, could you please provide som answers to my questions ? Or maybe some code ? – Dan Chaltiel Nov 10 '18 at 19:32
  • @DanChaltiel if the project does not have any `assembleRelease` task, it might be lacking a `buildType` with name `release`. there also is no task `build`. `assembleDebug` & `assembleRelease` are the last tasks. – Martin Zeitler Nov 11 '18 at 15:27
  • As I said, I only know basics about gradle and even never had to use it outside of AS, hence I am here for help. I think your answer, even after edits, is more thought for gradle-aware people so I'm afraid I'm still as lost as at start. I think I use the default config of Android Studio with both `debug` and `release` buildTypes and I don't even know where to look at to add a task without blasting everything out (and I couldn't find any simple tutorial). Your code doesn't work when I insert it at the bottom of app/build.gradle, was it the right place ? Could you elaborate a little (ELI5) ? – Dan Chaltiel Nov 12 '18 at 08:39
  • @DanChaltiel the names of the tasks may vary. just run `./gradlew tasks` to get the correct names. or check the build log, what the task at the very bottom is. without build-types, it might be `assemble`. – Martin Zeitler Nov 12 '18 at 10:59
  • Your code is finally working, but only the `println` part. The command of your last comment returned all expected tasks included `assemble`, `assembleDebug` & `assembleRelease`. Still no clue why Gradle won't recognise them anyway. I think my very problem is that the `announce` plugin doesn't work and the Snarl doc is not much of help. Is there any config I should know about to connect Gradle to Snarl ? I couldn't find any doc about it... – Dan Chaltiel Nov 12 '18 at 20:52
0

Have you considered using sound for notification?

You can add this to your app/.gradle file inside android{ } tag :

afterEvaluate {
    gradle.buildFinished{ BuildResult buildResult ->
        if (buildResult.failure) {
            ['powershell', """(New-Object Media.SoundPlayer "C:\\failed_notif.wav").PlaySync();"""].execute()
            println("failed doing task")
        } else {
            ['powershell', """(New-Object Media.SoundPlayer "C:\\succeed_notif.wav").PlaySync();"""].execute()
            println("build finished")
        }
    }
}

If you want the notification only run after specific buildType finish (success build only), you can use :

afterEvaluate {
   assembleDebug.doLast{ //play sound for debug buildType }
   assembleRelease.doLast{ //play sound for release buildType }
}

Please note that this method can only run with .wav file. If you want to use an .mp3 you can try this.