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

It seems that the following task (in build.gradle of the application module) always works before creating apk:

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

Can anyone offer advice on how to run a task after creating apks?

+4
source share
3 answers

try adding this to 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"
}

call the build command and specify the task assembleDebug

./gradlew assembleDebug

+8
source

Android tasks are usually created in the afterEvaluate phase. Starting with gradle 2.2, these tasks also include assembleDebug and AssembleRelease. To access such tasks, the user will need to use close afterEvaluate:

afterEvaluate { assembleDebug.dependsOn someTask }

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

+4

I found a solution that works to copy the release APK to the project root automatically when the build is complete.

    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)
        }
}
+3
source

Source: https://habr.com/ru/post/1569489/


All Articles