Skip to content

Instantly share code, notes, and snippets.

@Robyer
Last active July 31, 2024 13:18
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'
}
}
}
}
@AndroidDeveloperLB
Copy link

@Robyer It was Java based, and had gradle and not kts.
I tried kts just to see how to do it, but got back to gradle after seeing it might be the reason to the issues.

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