aar_init_script.gradle 5.7 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// This script is used to initialize the build in a module or plugin project.
// During this phase, the script applies the Maven plugin and configures the
// destination of the local repository.
// The local repository will contain the AAR and POM files.

10
import java.nio.file.Paths
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.maven.MavenDeployer
import org.gradle.api.plugins.MavenPlugin
import org.gradle.api.tasks.Upload

void configureProject(Project project, File outputDir) {
    if (!project.hasProperty("android")) {
        throw new GradleException("Android property not found.")
    }
    if (!project.android.hasProperty("libraryVariants")) {
        throw new GradleException("Can't generate AAR on a non Android library project.");
    }

    project.apply plugin: "maven"

    project.android.libraryVariants.all { variant ->
        addAarTask(project, variant)
    }
    // 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
    // to resolve dependencies.
    project.version = project.version.replace("-SNAPSHOT", "")

    project.uploadArchives {
        repositories {
            mavenDeployer {
                repository(url: "file://${outputDir}/outputs/repo")
            }
        }
    }
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    if (!project.property("is-plugin").toBoolean()) {
        return
    }
    if (project.hasProperty('localEngineOut')) {
        // TODO(egarciad): Support local engine.
        // This most likely requires refactoring `flutter.gradle`, so the logic can be reused.
        throw new GradleException(
            "Local engine isn't supported when building the plugins as AAR. " +
            "See: https://github.com/flutter/flutter/issues/40866")
    }

    // This is a Flutter plugin project. Plugin projects don't apply the Flutter Gradle plugin,
    // as a result, add the dependency on the embedding.
    project.repositories {
        maven {
            url "http://download.flutter.io"
        }
    }
    String engineVersion = Paths.get(getFlutterRoot(project), "bin", "internal", "engine.version")
        .toFile().text.trim()
    project.dependencies {
        // Add the embedding dependency.
        compileOnly ("io.flutter:flutter_embedding_release:1.0.0-$engineVersion") {
            // We only need to expose io.flutter.plugin.*
            // No need for the embedding transitive dependencies.
            transitive = false
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
        }
    }
}

String getFlutterRoot(Project project) {
    if (!project.hasProperty("flutter-root")) {
        throw new GradleException("The `-Pflutter-root` flag must be specified.")
    }
    return project.property("flutter-root")
}

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 `uploadArchives` runs.
        if (!project.gradle.startParameter.taskNames.contains(taskName)) {
            return
        }
        // NOTE(blasten): `android.defaultPublishConfig` must equal the variant name to build.
        // Where variant name is `<product-flavor><Build-Type>`. However, it's too late to configure
        // `defaultPublishConfig` at this point. Therefore, the code below ensures that the
        // default build config uses the artifacts produced for the specific build variant.
        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}.");
        }
        project.uploadArchives.repositories.mavenDeployer {
            pom {
                artifactId = "${project.name}_${variant.name.toLowerCase()}"
            }
        }
        // Clear the current archives since the current one is assigned based on
        // `android.defaultPublishConfig` which defaults to `release`.
        project.configurations["archives"].artifacts.clear()
        // Add the artifact that will be published.
        project.artifacts.add("archives", bundle)
        // Generate the Maven artifacts.
        finalizedBy "uploadArchives"
    }
}

projectsEvaluated {
    if (rootProject.property("is-plugin").toBoolean()) {
        if (rootProject.hasProperty("output-dir")) {
            rootProject.buildDir = rootProject.property("output-dir")
        } else {
            rootProject.buildDir = "../build";
        }
        // In plugin projects, the Android library is the root project.
        configureProject(rootProject, rootProject.buildDir)
        return
    }
    // In module projects, the Android library project is the `:flutter` subproject.
    Project androidLibrarySubproject = rootProject.subprojects.find { it.name == "flutter" }
    // In module projects, the `buildDir` is defined in the `:app` subproject.
    Project appSubproject = rootProject.subprojects.find { it.name == "app" }

    assert appSubproject != null
    assert androidLibrarySubproject != null

    if (appSubproject.hasProperty("output-dir")) {
        appSubproject.buildDir = appSubproject.property("output-dir")
    } else {
        appSubproject.buildDir = "../build/host"
    }
    configureProject(androidLibrarySubproject, appSubproject.buildDir)
}