How to get a notification when Android Studio fini

2019-12-16 19:21发布

问题:

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.

回答1:

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
    }
}


回答2:

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.