The background,

A key part of a recent project requirement was to publish different Module variants to Maven libraries. I didn’t realize this at first. Using the common apply Plugin: ‘maven’ plugin configuration, I found that the uploadArchives task was successfully executed, but there was actually no product generated. Tasks on which uploadArchives depends are not executed, and no other valid log information is displayed during this period. Later, IT occurred to me that the build variant was the reason. After communicating with my colleagues, I finally found that maven-publish could be used.


Maven-publish plugin

In fact, the Android website already has guidelines for various Module variants to the Maven library, using the Maven-publish plugin.

Android Gradle plugin 3.6.0 and later supports the Maven Publish Gradle plugin, which allows you to Publish build artifacts to the Apache Maven code base. The Android Gradle plug-in creates a component for each build variant artifact in the application or library module that you can use to customize the publication to the Maven code base.

The Maven-Publish plugin is powerful enough to not only distinguish the variant publication module AAR, but also publish AAB and even APK files.

Address: specific document developer.android.com/studio/buil… Docs.gradle.org/current/use…


Third, the sample

1. Examples of AAR published on the official website:

// Because the components are created only during the afterEvaluate phase, you must // configure your publications using the afterEvaluate() lifecycle method. afterEvaluate { publishing { publications { // Creates a Maven publication called "release". release(MavenPublication) { // Applies the component for  the release build variant. from components.release // You can then customize attributes of the publication as shown GroupId = 'com.example.MyLibrary' artifactId = 'final' version = '1.0'} // Creates a Maven publication called Debug (MavenPublication) {// Applies the component for the debug build variable. from components. Debug groupId = 'com.example.MyLibrary' artifactId = 'final-debug' version = '1.0'}}Copy the code


2, the configuration used in the project, where part of the script example:

apply plugin: 'com.android.library' apply plugin: 'maven-publish' task androidJavadocs(type: Javadoc) {/ / set the source location source = android. SourceSets. Main. Java srcDirs exclude '* * / r.h. HTML', '/ R * *. *. HTML. Task androidJavadocsJar(type: Jar) {/ / the specified document name archiveClassifier. Set (' javadoc) from androidJavadocs. DestinationDir} / / generated sources. The Jar task androidSourcesJar(type: Jar) { archiveClassifier.set('sources') from android.sourceSets.main.java.sourceFiles } afterEvaluate { publishing { publications { thGoogleSdk(MavenPublication) { groupId = 'com.corn.pay' artifactId = project.getName() version = '1.1.3-SNAPSHOT' pop.withxml {def dependenciesNode = asNode().appendnode ("dependencies") configurations.implementation.allDependencies.forEach(){ Dependency dependency -> if (dependency.version ! = "unspecified" && dependency.name ! = "unspecified"){ def dependencyNode = dependenciesNode.appendNode('dependency') dependencyNode.appendNode('groupId', dependency.group) dependencyNode.appendNode('artifactId', dependency.name) dependencyNode.appendNode('version', dependency.version) } } } artifact androidJavadocsJar artifact androidSourcesJar artifact(tasks.getByName("bundleThGoogleSdkReleaseAar")) } } repositories { maven { def devUrl = "https://maven.xxxx.com/nexus" def user = 'xxxx' def pass = 'yyyy' String target = pom_target if (target == "release") {  url = "${devUrl}/content/repositories/releases/" credentials { username = user password = pass } } else if (target == "local") { // test, upload local maven repository url = "file:" + new File(project.rootProject.rootDir, "mavenlocal").path } else { url = "${devUrl}/content/repositories/snapshots" credentials { username = user password = pass } } } } } }Copy the code