Revert "Use the maven-publish plugin to publish AAR files. (#101276)" (#101827)

This reverts commit 2b0255f0d9.
This commit is contained in:
Zachary Anderson
2022-04-13 08:12:07 -07:00
committed by GitHub
parent 5fb74db1a9
commit 6190c5df09
8 changed files with 115 additions and 292 deletions

View File

@@ -6,7 +6,9 @@
import java.nio.file.Paths
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.artifacts.maven.MavenDeployer
import org.gradle.api.plugins.MavenPlugin
import org.gradle.api.tasks.Upload
void configureProject(Project project, String outputDir) {
if (!project.hasProperty("android")) {
@@ -16,6 +18,8 @@ void configureProject(Project project, String outputDir) {
throw new GradleException("Can't generate AAR on a non Android library project.");
}
project.apply plugin: "maven"
// Snapshot versions include the timestamp in the artifact name.
// Therefore, remove the snapshot part, so new runs of `flutter build aar` overrides existing artifacts.
// This version isn't relevant in Flutter since the pub version is used
@@ -30,51 +34,13 @@ void configureProject(Project project, String outputDir) {
addAarTask(project, variant)
}
project.publishing {
project.uploadArchives {
repositories {
maven {
url = uri("file://${outputDir}/outputs/repo")
mavenDeployer {
repository(url: "file://${outputDir}/outputs/repo")
}
}
}
// Some extra work to have alternative publications with the same format as the old maven plugin.
// Instead of using classifiers for the variants, the old maven plugin appended `_{variant}` to the artifactId
// First, create a default MavenPublication for each variant (except "all" since that is used to publish artifacts in the new way)
project.components.forEach { component ->
if (component.name != "all") {
project.publishing.publications.create(component.name, MavenPublication) {
from component
}
}
}
// then, rename the artifactId to include the variant and make sure to remove any classifier
// data tha gradle has set, as well as adding a <relocation> tag pointing to the new coordinates
project.publishing.publications.forEach { pub ->
def relocationArtifactId = pub.artifactId
pub.artifactId = "${relocationArtifactId}_${pub.name}"
pub.alias = true
pub.pom.distributionManagement {
relocation {
// New artifact coordinates
groupId = "${pub.groupId}"
artifactId = "${relocationArtifactId}"
version = "${pub.version}"
message = "Use classifiers rather than _variant for new publish plugin"
}
}
}
// also publish the artifacts in the new way, using one set of coordinates with classifiers
project.publishing.publications.create("all", MavenPublication) {
from project.components.all
alias false
}
if (!project.property("is-plugin").toBoolean()) {
return
}
@@ -118,21 +84,55 @@ void addAarTask(Project project, variant) {
String variantName = variant.name.capitalize()
String taskName = "assembleAar$variantName"
project.tasks.create(name: taskName) {
// This check is required to be able to configure the archives before `publish` runs.
// This check is required to be able to configure the archives before `uploadArchives` runs.
if (!project.gradle.startParameter.taskNames.contains(taskName)) {
return
}
project.uploadArchives.repositories.mavenDeployer {
pom {
artifactId = "${project.name}_${variant.name.toLowerCase()}"
}
}
overrideDefaultPublishConfig(project, variant)
// Generate the Maven artifacts.
finalizedBy "publish"
finalizedBy "uploadArchives"
}
}
// maven-publish has to be applied _before_ the project gets evaluated, but some of the code in
// `configureProject` requires the project to be evaluated. Apply the maven plugin to all projects, but
// only configure it if it matches the conditions in `projectsEvaluated`
// This method mimics the logic in AGP when `android.defaultPublishConfig` is set in `build.gradle`:
// https://android.googlesource.com/platform/tools/base/+/studio-master-dev/build-system/gradle-core/src/main/java/com/android/build/gradle/internal/variant/VariantHelper.java
//
// Unfortunately, `android.defaultPublishConfig` cannot be overridden at this point since
// AGP already run this code.
void overrideDefaultPublishConfig(Project project, variant) {
String variantName = variant.name.capitalize()
Task bundle = project.tasks.findByName("bundle${variantName}Aar") // gradle:3.2.0
if (bundle == null) {
bundle = project.tasks.findByName("bundle${variantName}") // gradle:3.1.0
}
if (bundle == null) {
throw new GradleException("Can't generate AAR for variant ${variantName}.");
}
// Clear the current archive artifacts since the artifacts are based on `android.defaultPublishConfig`.
project.configurations["archives"].artifacts.clear()
// Add the artifact that will be published.
project.artifacts.add("archives", bundle)
allprojects {
apply plugin: "maven-publish"
def scopeMappings = project.uploadArchives.repositories.mavenDeployer.pom.scopeMappings
// Clear the scope mappings added by AGP since they are based on the current `android.defaultPublishConfig`.
scopeMappings.mappings.clear()
// Add the new mappings.
for (Configuration configuration : flattenConfiguration(variant.runtimeConfiguration)) {
scopeMappings.addMapping(/* priority = */ 300, configuration, "compile")
}
}
Set<Configuration> flattenConfiguration(Configuration configuration) {
Set<Configuration> configs = [configuration]
for (Configuration extend : configuration.extendsFrom) {
configs.addAll(flattenConfiguration(extend))
}
return configs
}
projectsEvaluated {

View File

@@ -726,7 +726,7 @@ void printHowToConsumeAar({
for (final String buildMode in buildModes) {
logger.printStatus("""
${buildMode}Implementation '$androidPackage:flutter:$buildNumber:$buildMode'""");
${buildMode}Implementation '$androidPackage:flutter_$buildMode:$buildNumber'""");
}
logger.printStatus('''

View File

@@ -29,7 +29,8 @@ import 'android_sdk.dart';
// https://kotlinlang.org/docs/gradle.html#plugin-and-versions
const String templateDefaultGradleVersion = '7.4';
const String templateAndroidGradlePluginVersion = '7.1.2';
const String templateDefaultGradleVersionForModule = '7.1.2';
// TODO(egarciad): Gradle 7 breaks AARs builds: https://github.com/flutter/flutter/issues/101083
const String templateDefaultGradleVersionForModule = '4.1.0';
const String templateKotlinGradlePluginVersion = '1.6.10';
// These versions should match the values in flutter.gradle (FlutterExtension).

View File

@@ -543,9 +543,9 @@ flutter:
' 3. Make the host app depend on the Flutter module:\n'
'\n'
' dependencies {\n'
" releaseImplementation 'com.mycompany:flutter:2.2:release'\n"
" debugImplementation 'com.mycompany:flutter:2.2:debug'\n"
" profileImplementation 'com.mycompany:flutter:2.2:profile'\n"
" releaseImplementation 'com.mycompany:flutter_release:2.2'\n"
" debugImplementation 'com.mycompany:flutter_debug:2.2'\n"
" profileImplementation 'com.mycompany:flutter_profile:2.2'\n"
' }\n'
'\n'
'\n'
@@ -594,7 +594,7 @@ flutter:
' 3. Make the host app depend on the Flutter module:\n'
'\n'
' dependencies {\n'
" releaseImplementation 'com.mycompany:flutter:1.0:release'\n"
" releaseImplementation 'com.mycompany:flutter_release:1.0'\n"
' }\n'
'\n'
'To learn more, visit https://flutter.dev/go/build-aar\n'
@@ -632,7 +632,7 @@ flutter:
' 3. Make the host app depend on the Flutter module:\n'
'\n'
' dependencies {\n'
" debugImplementation 'com.mycompany:flutter:1.0:debug'\n"
" debugImplementation 'com.mycompany:flutter_debug:1.0'\n"
' }\n'
'\n'
'To learn more, visit https://flutter.dev/go/build-aar\n'
@@ -671,7 +671,7 @@ flutter:
' 3. Make the host app depend on the Flutter module:\n'
'\n'
' dependencies {\n'
" profileImplementation 'com.mycompany:flutter:1.0:profile'\n"
" profileImplementation 'com.mycompany:flutter_profile:1.0'\n"
' }\n'
'\n'
'\n'