Created
September 7, 2021 10:25
-
-
Save gmazzo/6233d891d28a4051f1ab6435ea5fd1f7 to your computer and use it in GitHub Desktop.
Android Library Maven Publication (with sources)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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