How to publish all flavor variants to maven with Android Gradle plugin 3.0.0?
Asked Answered
E

2

8

Earlier I have used android gradle plugin v2.2.2, now I am migrating to 3.0. The below code publishes(uploadArchives task) all my variants to maven without any issues with 2.14.1. But as soon I upgrade to 3.0 nothing is been published. If I remove the prodcutFlavors, it publishes the release version of the library to maven properly. Am I missing something? Is something changed in 3.0?

I have read that by default now all variants will be published and publishNonDefault is no more needed. After removing publishNonDefault also it does not work.

I am using Gradle 4.1 with plugin 3.0 & Gradle 2.14.1 with plugin 2.2.2

apply plugin: 'com.android.library'
apply plugin: 'maven'
android {
    compileSdkVersion 26

    defaultConfig {
        minSdkVersion 17
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }


    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    publishNonDefault true

    flavorDimensions "app"
    productFlavors{
        apple{

        }
        orange{

        }
    }
}


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:26.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: mavenLocal().url)
            pom.version = "1.3.7"
            pom.artifactId = "android"
            pom.groupId = "com.example"
        }
    }
}
Erythrism answered 10/3, 2018 at 9:6 Comment(0)
I
15

I am facing a similar problem. Although I don't use productFlavors, I am expecting mavenDeployer to publish three configurations (corresponding to my three build types "debug", "release", and "staging"). But it no longerd does, it only publishes an AAR to Maven for the "release" configuration.

Prior to Android Plugin for Gradle 3.0.1, the following mavenDeployer code did publish all three AAR files:

uploadArchives {
    repositories.mavenDeployer {
        repository(url:"file://" + "${project.buildDir}/maven/releases")

        pom.groupId = "com.example"
        pom.artifactId = "sdk"
        pom.version = sdkName
    }
}

The Gradle code above used to produce the following 3 files published to Maven:

sdk/build/maven/releases/com/locuslabs/sdk/develop/sdk-develop-debug.aar 
sdk/build/maven/releases/com/locuslabs/sdk/develop/sdk-develop-staging.aar 
sdk/build/maven/releases/com/locuslabs/sdk/develop/sdk-develop-release.aar

However, after upgrading to Android Plugin for Gradle 3.0.1, it only produces:

sdk/build/maven/releases/com/locuslabs/sdk/develop/sdk-develop.aar

Notice that there isn't even a suffix of -release on the file name even though I'm pretty sure it's the release one.

So far the closest I've come to a solution is documentation for a newer way to publish AAR files to Maven: android-maven-publish.

Background research:

From Android Libraries: maven vs maven-publish gradle project I learned that there are two Gradle mechanisms for publishing to Gradle. Plus there are unofficial Android implementations of those.

The Maven Plugin: Multiple Artifacts per Project

An old one which I think you're using because you've specified apply plugin: 'maven' which is documented here The Maven Plugin. To get multiple AAR files output, presumably you would "need need to declare a filter for each artifact you want to publish." but the drawback is "you need to generate multiple POMs" (Multiple Artifacts per Project).

Maven Publishing (new): Publishing multiple modules

The apparent replacement for The Maven Plugin is a new one, Maven Publishing (new), which seems to have the ability to publish multiple artifacts as described in Publishing multiple modules. Basically it seems like you just need to have multiple method calls in the publications block (sorry if I'm using the wrong Gradle/Groovy syntax--it's a confusing language that I wish was never created).

publishing {
    publications {
        impl(MavenPublication) {               // for first variant?
            groupId 'org.gradle.sample.impl'
            artifactId 'project2-impl'
            version '2.3'

            from components.java
        }
        api(MavenPublication) {                // for second variant?
            groupId 'org.gradle.sample'
            artifactId 'project2-api'
            version '2'

            artifact apiJar
        }
    }
}

task apiJar(type: Jar) {
    baseName "publishing-api"
    from sourceSets.main.output
    exclude '**/impl/**'
}

If that weren't complicated enough, according to Android Libraries: maven vs maven-publish gradle project, "The above two official plugins don't fully support Android, so there are two community ports."

Gradle Android Maven plugin (unofficial)

The documentation at Gradle Android Maven plugin doesn't explain how we would output multiple AARs.

android-maven-publish (unofficial)

As mentioned above, the documentation at android-maven-publish looks the most promising because it provides an example to "publish custom variants" which implies that it would solve your problem of publishing all flavor variants. Here's the example from android-maven-publish:

publishing {
    publications {
        android.libraryVariants.all { variant ->

            "maven${variant.name.capitalize()}Aar"(MavenPublication) {
                from components.findByName("android${variant.name.capitalize()}")
                groupId 'digital.wup.test-publish'
                artifactId 'test-publish'
                version "1.0.0-${variant.name}"
            }
        }
    }
}
Inion answered 16/3, 2018 at 23:50 Comment(1)
Nice one! the android-maven-publish as mentioned in its site is no longer needed if your project uses AGP (Android Gradle Plugin) 3.6.0 or newer, as it already supports maven-publish.Springe
B
2

The Maven Publish plugin for Android is indeed the way to do this, as mentioned by htofoya.

You can either enter all the variants one by one, as shown in the docs, or you can do something like this:

afterEvaluate {
    android.libraryVariants.each { variant ->
        // Only consider release
        if (variant.buildType.name != "release") { return }
        publishing.publications.create(variant.name, MavenPublication) {
            from components.findByName(variant.name)
            groupId libraryGroupId
            artifactId "${libraryArtifactId}-${variant.flavorName}"
            version libraryVersion
        }
    }
}

The example from android-maven-publish, shown in Michael Osofsky answer, is flawed and doesn't produce correct POM files

Berry answered 24/2, 2021 at 10:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.