Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 373
nieschumi Avatar asked Oct 01 '15 02:10

nieschumi


People also ask

Does Gradle build run all tasks?

You can execute multiple tasks from a single build file. Gradle can handle the build file using gradle command. This command will compile each task in such an order that they are listed and execute each task along with the dependencies using different options.

How do I run a default task in Gradle?

Gradle allows you to define one or more default tasks that are executed if no other tasks are specified. defaultTasks 'clean', 'run' tasks. register('clean') { doLast { println 'Default Cleaning! ' } } tasks.


2 Answers

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'
   }
}
like image 67
Opal Avatar answered Sep 25 '22 01:09

Opal


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.

like image 33
ShellDude Avatar answered Sep 25 '22 01:09

ShellDude