Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a Gradle task after apks are produced in Android Studio?

The following task (in build.gradle of an app's module) seems to run always before the apk is produced:

android.applicationVariants.all { variant ->
    if (variant.buildType.name == 'release') {
            def releaseBuildTask = tasks.create(name: "debug") {
            println("....................  test   ..............................")
        }
        releaseBuildTask.mustRunAfter variant.assemble
    }
}

Could anyone offer a tip on how to run a task after the apks are produced?

like image 327
Hong Avatar asked Dec 31 '14 00:12

Hong


3 Answers

Android tasks are typically created in the "afterEvaluate" phase. Starting from gradle 2.2, those tasks also include "assembleDebug" and "assembleRelease". To access such tasks, the user will need to use an afterEvaluate closure:

afterEvaluate { assembleDebug.dependsOn someTask }

source: https://code.google.com/p/android/issues/detail?id=219732#c32

like image 83
Pol Avatar answered Oct 24 '22 05:10

Pol


try add this in you app/build.gradle

assembleDebug.doLast {
    android.applicationVariants.all { variant ->
        if (variant.buildType.name == 'release') {
            def releaseBuildTask = tasks.create(name: "debug") {
                println("....................  test   ..............................")
            }
            releaseBuildTask.mustRunAfter variant.assemble
        }
    }
    println "build finished"
}

invoke the build command and specify the task assembleDebug

./gradlew assembleDebug

like image 22
alijandro Avatar answered Oct 24 '22 04:10

alijandro


I found a solution that works, to copy the release APK into the project root automatically on build completion.

    android {
        ...
        task copyReleaseApk(type: Copy) {
            from 'build/outputs/apk'
            into '..' // Into the project root, one level above the app folder
            include '**/*release.apk'
        }

        afterEvaluate {
            packageRelease.finalizedBy(copyReleaseApk)
        }
}
like image 45
Ollie C Avatar answered Oct 24 '22 05:10

Ollie C