Skip to content

Instantly share code, notes, and snippets.

@Robyer
Last active June 15, 2023 04:22
Show Gist options
  • Save Robyer/a6578e60127418b380ca133a1291f017 to your computer and use it in GitHub Desktop.
Save Robyer/a6578e60127418b380ca133a1291f017 to your computer and use it in GitHub Desktop.
Gradle script for publishing Android library with sources and javadoc to Maven repository using maven-publish plugin.
// You can use maven-publish-helper.gradle script without changes and even share it between multiple
// modules. Just place the maven-publish-helper.gradle file in the root directory of your project,
// then apply it at the bottom of your module's build.gradle file like this:
// ...content of module's build.gradle file...
apply from: '../maven-publish-helper.gradle'
publishing {
publications {
release(MavenPublication) {
// Specify own groupId as package name of your library,
// otherwise it would just use project's name (=name of the root directory) by default.
groupId 'com.example'
// Specify custom artifactId if needed,
// otherwise it would use module's name by default.
//artifactId 'custom-artifact'
// You can specify custom version,
// otherwise it would use version from `android { defaultConfig { ... } }` by default.
//version = '1.0'
}
}
}
/**
* Maven Publish Helper
*
* Requires Android Gradle plugin 3.6.0 or higher (available since Android Studio 3.6).
* See also: https://developer.android.com/studio/build/maven-publish-plugin
*
* @Author Robert Pösel
* @Version 1.5
* @Date 3.3.2020
*/
apply plugin: 'maven-publish'
task androidJavadocs(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
android.libraryVariants.all { variant ->
if (variant.name == 'release') {
owner.classpath += variant.javaCompileProvider.get().classpath
}
}
exclude '**/R.html', '**/R.*.html', '**/index.html'
}
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
archiveClassifier.set('javadoc')
from androidJavadocs.destinationDir
}
task androidSourcesJar(type: Jar) {
archiveClassifier.set('sources')
from android.sourceSets.main.java.srcDirs
}
// 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
// Adds javadocs and sources as separate jars.
artifact androidJavadocsJar
artifact androidSourcesJar
// You can customize attributes of the publication here or in module's build.gradle file.
//groupId = 'com.example'
//artifactId = 'custom-artifact'
version = android.defaultConfig.versionName // or just '1.0'
}
}
}
}
@Robyer
Copy link
Author

Robyer commented May 5, 2023

@nikunj-unifynd It would be better for you to ask this question on StackOverflow, providing also additional info as content of your build.gradle file, because maybe you have some conflicting configuration there.

Or just start by creating new empty Android Studio project, and add this publishing code there. Then if it works, compare how your project where it doesn't work differs and try to find the issue.

@RahulSDeshpande
Copy link

RahulSDeshpande commented Jun 15, 2023

Thanks @Robyer

Guys FYI
Official Google Documentation:
https://developer.android.com/build/publish-library

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment