简体   繁体   中英

Gradle: How to run custom task after an Android Library is built?

I have an Android Library, it's generating a debug.aar and a release.aar, I need to copy the release.aar to another folder as a reference to other part of the project.

What I've done now is in this Android Library build.gradle I defined a task:

task copyAARToCommonLibs(type: Copy) {
    from('../build/outputs/aar') {
        include '*-release.arr'
    }
    into '../SomeSampleApps/libs'
}

I'm trying to run this task after the arr is generated, which I assume is assembleRelease stage, so I tried do this in this build.gradle

assembleRelease.doLast{
   copyAARToCommonLibs
}

I build the overall project using

 gradle build

But this task is running at the very beginning of the whole process.

I also tried this:

 applicationVariants.all { variant ->
     variant.assemble.doLast {
         copyAARToCommonLibs
     }
 }

inside android{} property(I guess that's what it's called?) Running gradle build, got this error: Could not find property 'applicationVariants'

I then came across this snippet:

tasks.withType(JavaCompile) { compileTask -> compileTask.dependsOn copyAARToCommonLibs }

But it seems this makes the task to run after compiling, I don't know exactly how to modify this to run after assemble.

Could someone please correct me where I did wrong and how can I get this copy task work after the .arr file is generated?

It seems that finalizedBy might be helpful.

assembleRelease.finalizedBy(copyAARToCommonLibs)

Mind the fact that in the following way you won't define a dependency:

assembleRelease.doLast {
   copyAARToCommonLibs
}

actually.. it does exactly nothing. You need to execute the task:

assembleRelease.doLast {
   copyAARToCommonLibs.execute()
}

but running task in the following way is discouraged and very bad practice.

You can also try:

assembleRelease.doLast {
   copy {
      from('../build/outputs/aar') {
        include '*-release.aar'
      }
      into '../AscendonSDKSamples/libs'
   }
}

I went with finalizedBy() but had to include it within an afterEvaluate...

afterEvaluate {
    if (gradle.startParameter.taskNames.contains(":app:assembleFatReleaseInternal")) {
        play.enabled = true
        play.commit = true
        play.track = "internal"
        play.releaseStatus = "completed"
        play.releaseName = versionName

        generateFatReleaseInternalBuildConfig.dependsOn set_build_date
        assembleFatReleaseInternal.finalizedBy(uploadCrashlyticsSymbolFileFatReleaseInternal)
        uploadCrashlyticsSymbolFileFatReleaseInternal.finalizedBy(publishFatReleaseInternal)
    }
}

This worked well for automating the upload of native symbols to Fabric / Crashlytics and other things such as automated play store publishing.

Because android studio add task by dynamic,so assembleRelease will not be recognized.
Just add hook after task added event happens.

    tasks.whenTaskAdded {
        theTask ->
            if (theTask.name.contains('externalNativeBuild')) {
                theTask.doLast{
                    println "[*] begin to copy file."
                }
            }
//            println theTask.name
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM