Skip to content

Instantly share code, notes, and snippets.

@gmazzo
Created September 7, 2021 10:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmazzo/6233d891d28a4051f1ab6435ea5fd1f7 to your computer and use it in GitHub Desktop.
Save gmazzo/6233d891d28a4051f1ab6435ea5fd1f7 to your computer and use it in GitHub Desktop.
Android Library Maven Publication (with sources)
plugins {
id("com.android.library")
`maven-publish`
}
publishing {
repositories {
// TODO define your publish repos here
}
publications {
val component = components["all"] as AdhocComponentWithVariants
create<MavenPublication>("default") {
from(component)
}
afterEvaluate {
android.libraryVariants.all {
val variantName = name
val allSources = objects.fileCollection()
sourceSets.forEach {
allSources.from(
it.kotlinDirectories,
it.javaDirectories,
it.resourcesDirectories
)
}
val sourcesTask =
tasks.register<Jar>("${variantName}Sources") {
from(allSources)
archiveClassifier.set("${variantName}-sources")
duplicatesStrategy = DuplicatesStrategy.WARN
}
fun createVariant(type: String, mavenScope: String) {
val attrs = configurations["${variantName}All${type}Publication"].attributes
val configuration = configurations.create("${variantName}${type}SourceElements") {
isVisible = false
isCanBeConsumed = false
isCanBeResolved = false
outgoing.artifact(sourcesTask)
attributes {
(attrs.keySet() - LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE).forEach {
@Suppress("UNCHECKED_CAST")
attribute(it as Attribute<Any>, attrs.getAttribute(it)!!)
}
attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.DOCUMENTATION))
attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType.SOURCES))
attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.EXTERNAL))
}
}
component.addVariantsFromConfiguration(configuration) {
mapToMavenScope(mavenScope)
mapToOptional()
}
}
createVariant("Api", "compile")
createVariant("Runtime", "runtime")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment