1// Copyright 2019 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4// 5// This script is used to initialize the build in a module or plugin project. 6// During this phase, the script applies the Maven plugin and configures the 7// destination of the local repository. 8// The local repository will contain the AAR and POM files. 9 10import org.gradle.api.Project 11import org.gradle.api.artifacts.Configuration 12import org.gradle.api.artifacts.maven.MavenDeployer 13import org.gradle.api.plugins.MavenPlugin 14import org.gradle.api.tasks.Upload 15 16void configureProject(Project project, File outputDir) { 17 if (!project.hasProperty("android")) { 18 throw new GradleException("Android property not found.") 19 } 20 if (!project.android.hasProperty("libraryVariants")) { 21 throw new GradleException("Can't generate AAR on a non Android library project."); 22 } 23 24 project.apply plugin: "maven" 25 26 project.android.libraryVariants.all { variant -> 27 addAarTask(project, variant) 28 } 29 // Snapshot versions include the timestamp in the artifact name. 30 // Therefore, remove the snapshot part, so new runs of `flutter build aar` overrides existing artifacts. 31 // This version isn't relevant in Flutter since the pub version is used 32 // to resolve dependencies. 33 project.version = project.version.replace("-SNAPSHOT", "") 34 35 project.uploadArchives { 36 repositories { 37 mavenDeployer { 38 repository(url: "file://${outputDir}/outputs/repo") 39 } 40 } 41 } 42 // Check if the project uses the Flutter plugin (defined in flutter.gradle). 43 Boolean usesFlutterPlugin = project.plugins.find { it.class.name == "FlutterPlugin" } != null 44 if (!usesFlutterPlugin) { 45 project.dependencies { 46 // Some plugins don't include `annotations` and they don't set 47 // `android.useAndroidX=true` in `gradle.properties`. 48 compileOnly "androidx.annotation:annotation:+" 49 compileOnly "com.android.support:support-annotations:+" 50 // The Flutter plugin already adds `flutter.jar`. 51 compileOnly project.files("${getFlutterRoot(project)}/bin/cache/artifacts/engine/android-arm-release/flutter.jar") 52 } 53 } 54} 55 56String getFlutterRoot(Project project) { 57 if (!project.hasProperty("flutter-root")) { 58 throw new GradleException("The `-Pflutter-root` flag must be specified.") 59 } 60 return project.property("flutter-root") 61} 62 63void addAarTask(Project project, variant) { 64 String variantName = variant.name.capitalize() 65 String taskName = "assembleAar${variantName}" 66 project.tasks.create(name: taskName) { 67 // This check is required to be able to configure the archives before `uploadArchives` runs. 68 if (!project.gradle.startParameter.taskNames.contains(taskName)) { 69 return 70 } 71 // NOTE(blasten): `android.defaultPublishConfig` must equal the variant name to build. 72 // Where variant name is `<product-flavor><Build-Type>`. However, it's too late to configure 73 // `defaultPublishConfig` at this point. Therefore, the code below ensures that the 74 // default build config uses the artifacts produced for the specific build variant. 75 Task bundle = project.tasks.findByName("bundle${variantName}Aar") // gradle:3.2.0 76 if (bundle == null) { 77 bundle = project.tasks.findByName("bundle${variantName}") // gradle:3.1.0 78 } 79 if (bundle == null) { 80 throw new GradleException("Can't generate AAR for variant ${variantName}."); 81 } 82 project.uploadArchives.repositories.mavenDeployer { 83 pom { 84 artifactId = "${project.name}_${variant.name.toLowerCase()}" 85 } 86 } 87 // Clear the current archives since the current one is assigned based on 88 // `android.defaultPublishConfig` which defaults to `release`. 89 project.configurations["archives"].artifacts.clear() 90 // Add the artifact that will be published. 91 project.artifacts.add("archives", bundle) 92 // Generate the Maven artifacts. 93 finalizedBy "uploadArchives" 94 } 95} 96 97projectsEvaluated { 98 if (rootProject.property("is-plugin").toBoolean()) { 99 if (rootProject.hasProperty("output-dir")) { 100 rootProject.buildDir = rootProject.property("output-dir") 101 } else { 102 rootProject.buildDir = "../build"; 103 } 104 // In plugin projects, the Android library is the root project. 105 configureProject(rootProject, rootProject.buildDir) 106 return 107 } 108 // In module projects, the Android library project is the `:flutter` subproject. 109 Project androidLibrarySubproject = rootProject.subprojects.find { it.name == "flutter" } 110 // In module projects, the `buildDir` is defined in the `:app` subproject. 111 Project appSubproject = rootProject.subprojects.find { it.name == "app" } 112 113 assert appSubproject != null 114 assert androidLibrarySubproject != null 115 116 if (appSubproject.hasProperty("output-dir")) { 117 appSubproject.buildDir = appSubproject.property("output-dir") 118 } else { 119 appSubproject.buildDir = "../build/host" 120 } 121 configureProject(androidLibrarySubproject, appSubproject.buildDir) 122} 123