How to execute a task after assembleRelease?

3.4k views Asked by At

Firebase-Crash has a new feature. You can upload your mapping.txt using a Gradle command: ./gradlew :app:firebaseUploadReleaseProguardMapping.

I want to automate this process. I want to upload that mapping.txt file to Firebase as soon as I create a release apk.

How can I force Gradle to execute firebaseUploadReleaseProguardMapping after a succesful assembleRelease? Is there an easier way to do this?

5

There are 5 answers

3
Gabriele Mariotti On

In general you can define a dependencies for the task using the dependsOn method.

For example:

task A << {
    println 'Hello from A'
}

task B << {
    println 'Hello from B'
}

B.dependsOn A

You will obtain

> gradle -q B
Hello from A
Hello from B

In your case you can specify:

firebaseUploadReleaseProguardMapping.dependsOn assembleRelease

Also you can use the finalizedBy method.

A.finalizedBy B

Note that :

  • this will run B even if A failed.
  • finalizedBy is marked as "incubating" which means that this is an experimental feature and its behavior can be changed in future releases.
0
Doug Stevenson On

Look carefully at the chain of tasks that get executed when you target firebaseUploadReleaseProguardMapping:

...
:app:transformClassesWithDexForRelease
:app:mergeReleaseJniLibFolders UP-TO-DATE
:app:transformNative_libsWithMergeJniLibsForRelease
:app:transformNative_libsWithStripDebugSymbolForRelease
:app:packageRelease
:app:assembleRelease
:app:firebaseUploadReleaseProguardMapping

See that firebaseUploadReleaseProguardMapping already depends on assembleRelease. You don't need to force any additional dependencies for what you're trying to do - the dependency is already set up by the plugin, so that when you tell gradle to run that task, it will have already completed a release build. If you always want to upload after a successful release build, simply target firebaseUploadReleaseProguardMapping instead of assembleRelease.

0
Martin Zeitler On

One can define an external tool "generate signed APK and upload the ProGuard mapping file" - instead of the "generate signed APK" button, because the task firebaseUploadReleaseProguardMapping depends on assembleRelease and therefore will always execute it. the Firebase Plugins recently was updated to 1.1.0 ...

Screenshot

the result:

:mobile:assembleRelease
:mobile:firebaseUploadReleaseProguardMapping
Attempting to upload Proguard mapping file...
2
zhichang chen On
project.tasks.whenTaskAdded { Task task ->
if (task.name == 'assembleRelease') {
    task.doLast {
        println("makeSystemSignature")
    }
    task.finalizedBy(makeSystemSignature)
}}
0
SudoPlz On

In build.gradle do:

afterEvaluate {
    tasks.assembleRelease.finalizedBy {
        task postReleaseTask(type: Exec) {
            // Work
        }
    }
}

or in your case:

afterEvaluate {
    tasks.assembleRelease.finalizedBy firebaseUploadReleaseProguardMapping
}