Android-Components-Plugin
Variant aware artifact publishing for android projects.
Project Status
Icebox for now. Life and work are needing my time and it seems that the tools team at google was just unblocked by gradle with changes to the publishing api in 5.3.
Preface
Gradle and the Android Gradle Plugin(s) (AGP) provide/consume dependencies in a variant aware way when depending on projects directly. The Maven POM format does not support exposing gradle metadata to achieve the same level of itegration when consuming a published artifact.
The goal of this plugin is to provide support for the Gradle module metadata specification as well as reduce the amount of work required to setup publishing android projects.
Installation and Configuration
The plugin is available via the Gradle Plugin Portal
//build.gradle.kts
plugins {
`maven-publish`
id("com.android.library")
id("com.trevjonez.android-components") version "0.1.0"
}
publishing {
repositories {
maven {
name = "CompanyMaven"
url = uri("https://maven.company.xyz")
credentials {...}
}
}
}
android-components
binds the configuration of the applied AGP into the
Maven-Publish plugin. This is done by creating a SoftwareComponent instance
for each variant which is registered as part of a single ComponentWithVariants
that represents the entire module. By implementing ComponentWithVariants gradle
will automatically produce the appropriate .module metadata file when attached to
a publication.
The published artifact coordinates are inferred from the project group, name, and version properties.
The primary artifact id can be overridden via the AndroidComponentsExtension. Project naming should be preferred over using this override.
//build.gradle(.kts)
androidComponents {
artifactId = "CustomId"
}
Android Gradle Plugin Behaviors
AGP Plugin ID | Published Artifacts |
---|---|
com.android.library |
AAR, Sources JAR |
com.android.application |
See Issue #2 |
com.android.test |
See Issue #3 |
com.android.feature |
See Issue #4 |
com.android.instantapp |
See Issue #5 |
com.android.dynamic-feature |
See Issue #8 |
Artifact Consumption
.module metadata
In order to consume the .module metadata
in downstream projects GRADLE_METADATA
must be enabled via the
Settings.enableFeaturePreview method.
//settings.gradle(.kts)
enableFeaturePreview("GRADLE_METADATA")
Then add the dependency as usual.
dependencies {
implementation("com.example:lib:1.2.3")
}
.pom metadata
The android-components
plugin will also generate a redirecting POM file so the
main artifact coordinates point to the variant that was set as the
default publishing configuration on the AGP DSL.
dependencies {
//POM artifact that depends on "com.example:lib_release:1.2.3"
implementation("com.example:lib:1.2.3")
}
or manually select variants per configuration:
dependencies {
debugImplementation("com.example:lib_debug:1.2.3")
releaseImplementation("com.example:lib_release:1.2.3")
}
AGP/Gradle Compatibility
The following version combinations have been tested to work:
AGP Version | Gradle Version | Android Components Version |
---|---|---|
3.1.4 | 4.10 | 0.1.0 |
3.2.0-rc02 | 4.10 | 0.1.0 |
3.3.0-alpha08 | 4.10 | 0.1.0 |
Troubleshooting
If you are met with issues surrounding variant mismatching review the AGP DSL documentation for defaultConfig.missingDimensionStrategy as the same rules apply.
At that point if you are still unable to resolve the problem feel free to open a new issue to discuss further.
License
Copyright 2018 Trevor Jones
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.