Android AAR artifactory publish executes before assemble

Hi all,

I have an Android library project and I am trying to publish the AAR files to JFrog artifactory using gradle.
Once I have the AAR files and when I execute the build task, the publish is working as expected, the problem is that I am not able to do it as part of my build process if the AAR files are not there.

I want to publish the AAR files when ever a new one is available. I tried to put assembleIntegrated.finalizedBy (artifactoryPublish), but that didn’t help.
The publish task gets triggered before the AARs are generated.
I also tried adding another task with the AAR files as output, to rerun the task by adding the --rerun-task. But I see the gradle tasks list as below, no deployments:

    executing tasks: [assembleIntegrated]

    AAR1 files not found in /myfolder/.../my_lib_project/app/build/outputs/aar/my_aar_file1.aar
    AAR2 files not found in /myfolder/.../my_lib_project/app/build/outputs/aar/my_aar_file2.aar
.
.
.

    > Task :app:preBuild UP-TO-DATE
    > Task :app:test UP-TO-DATE
    > Task :app:check
    > Task :app:build
    > Task :app:artifactoryPublish
    > Task :artifactoryDeploy

I am assuming that the publish task is executing at the end, but the files don’t get published!

How can I make sure that the AAR files publish task is triggered only after they are generated or what is the best practice ?

mygradle.gradle -->

apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'

    File AARFile1 = file("$buildDir/outputs/aar/my_aar_file1.aar")
    File AARFile2 = file("$buildDir/outputs/aar/my_aar_file2.aar")

    publishing {
    publications {

        AAR1(MavenPublication) {
            groupId repoFolder
            version libVersion

            // Tell maven to prepare the generated "*.aar" file for publishing
            if (AARFile1.exists()) {
                artifactId libRelease
                artifact(AARFile1)
            } else {
                println 'AAR1 files not found in' + AARFile1.absolutePath
            }
        }

        AAR2(MavenPublication) {
            groupId repoFolder
            version libVersion

            // Tell maven to prepare the generated "*.aar" file for publishing
            if (AARFile2.exists()) {
                artifactId libDebug
                artifact(AARFile2)
            } else {
                println 'AAR2 files not found in' + AARFile2.absolutePath
            }
        }
    }
}


artifactory {
    contextUrl = "https://bintray.com/jfrog/artifactory:8080"
    publish {
        repository {
            // The Artifactory repository key to publish to
            repoKey = 'my_key'
           username = 'my_username'
           password = 'my_encrypt_password'
        }
        defaults {
            // Tell the Artifactory Plugin which artifacts should be published to Artifactory.
            if (AARFile1.exists() || AARFile2.exists()) {
                publications('AAR1', 'AAR2')
                publishArtifacts = true

                // Properties to be attached to the published artifacts.
                properties = ['qa.level': 'basic', 'dev.team':'Me' ]
                // Publish generated POM files to Artifactory (true by default)
                publishPom = true
            }
        }
    }
}

Solved the issue by modifying the code as below:

build.finalizedBy(artifactoryPublish)
artifactoryPublish.dependsOn(build)
publishing {
    publications {
        AAR1(MavenPublication) {
            groupId repoFolder
            artifactId libRelease
            version libVersion
            artifact("$buildDir/outputs/aar/my_aar_file1.aar")
            
        }
        AAR2(MavenPublication) {
            groupId repoFolder
            artifactId libRelease
            version libVersion
            artifact("$buildDir/outputs/aar/my_aar_file2.aar")
         
        }
    }
}

artifactory {
    contextUrl = "https://bintray.com/jfrog/artifactory:8080"
    publish {
        repository {
           repoKey = 'my_key'
           username = 'my_username'
           password = 'my_encrypt_password'
        }
        defaults {
            publications('AAR1', 'AAR2')
            publishArtifacts = true
            publishPom = true
        }
    }
}