Hong
Hong

Reputation: 18501

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?

Upvotes: 7

Views: 10132

Answers (4)

midFang
midFang

Reputation: 135

try add this in you app/build.gradle, Then after assembleRelease is completed, you can copy the file to the directory you specified.

project.tasks.configureEach { Task theTask ->
if (theTask.name == 'assembleRelease') {

    theTask.doLast {

        copy {
            from youInputPath // This can be the apk file path or mapping file path
            into youOutputPath
        }
        
    }
}

Upvotes: 0

Ollie C
Ollie C

Reputation: 28509

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

Upvotes: 12

Pol
Pol

Reputation: 481

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

Upvotes: 18

alijandro
alijandro

Reputation: 12147

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

Upvotes: 10

Related Questions