Home » Android » Android Library – Publish Multiple Variants to Local Maven Repository using Gradle

Android Library – Publish Multiple Variants to Local Maven Repository using Gradle

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am using Android Gradle plugin 0.13.2, Android Studio 0.8.11, Gradle 2.1 and maven plugin.

I would like to install multiple variants (flavour + build type) of my Android Library to local Maven repository all with one command (task).

Currently Android Gradle plugin 0.13.2 allows me to set publishNonDefault flag to publishing all variants, but as the documentation states it will publish the variants with a classifier which is not compatible with Maven Repository.

My workaround right now is to use defaultPublishConfig “myVariant” and change it for every variant I have.

apply plugin: 'com.android.library'
apply plugin: 'maven'

android {
    defaultPublishConfig "myVariant"
    .
    .
    .

}

task installArchives(type: Upload) {
    repositories.mavenInstaller {
        configuration = configurations.getByName(Dependency.DEFAULT_CONFIGURATION)
        pom.groupId = "com.company"
        pom.artifactId = "mylibrary"
        pom.version = "1.0.0-myVariant"
    }
}

I would like to have a single task that would properly publish all variants to local Maven repository.

How to&Answers:

To solve this I had to create one Upload task for each variant and make them depend on each other and on a master task that starts the process.

apply plugin: 'com.android.library'
apply plugin: 'maven'

android {
    .
    .
    .

}

// Master task that will publish all variants
def DefaultTask masterTask = project.tasks.create("installArchives", DefaultTask)

android.libraryVariants.all { variant ->
    variant.outputs.each { output ->

        // Configuration defines which artifacts will be published, create one configuration for each variant output (artifact)
        def Configuration variantConfiguration = project.configurations.create("${variant.name}Archives")
        project.artifacts.add(variantConfiguration.name, output.packageLibrary)

        // Create one Upload type task for each configuration
        def Upload variantTask = project.tasks.create("${variant.name}Install", Upload)
        variantTask.configuration = variantConfiguration
        variantTask.repositories.mavenInstaller {
            pom.groupId = "com.yourcompany"
            pom.artifactId = "yourLibrary"
            pom.version = "1.0.0-${variant.name}" //Give a different version for each variant
            pom.packaging = "aar"
        }

        // Make all tasks depend on each other and on master task
        masterTask.dependsOn variantTask
        masterTask = variantTask
    }
}

The task installArchives will publish all variants to the local Maven repository.

./gradlew installArchives