188c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar/*
288c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar * Copyright (C) 2017 The Android Open Source Project
388c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar *
488c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar * Licensed under the Apache License, Version 2.0 (the "License");
588c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar * you may not use this file except in compliance with the License.
688c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar * You may obtain a copy of the License at
788c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar *
888c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar *      http://www.apache.org/licenses/LICENSE-2.0
988c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar *
1088c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar * Unless required by applicable law or agreed to in writing, software
1188c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar * distributed under the License is distributed on an "AS IS" BASIS,
1288c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1388c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar * See the License for the specific language governing permissions and
1488c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar * limitations under the License.
1588c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar */
1688c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar
17b938b2eab0a90d4f9482067f3802f54c4834d2c1Aurimas Liutikasimport android.support.LibraryVersions
1888c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyarimport com.android.build.gradle.internal.coverage.JacocoPlugin
1988c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyarimport com.android.build.gradle.internal.coverage.JacocoReportTask
2088c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyarimport com.android.build.gradle.internal.tasks.DeviceProviderInstrumentTestTask
21a015004c3bb0c254e52a9fb61956fa1216eefbacAurimas Liutikasimport org.gradle.api.logging.configuration.ShowStacktrace
22a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit Boyarimport android.support.gmaven.GMavenVersionChecker
2388c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar
2488c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyardef supportRoot = ext.supportRootFolder
2588c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyarif (supportRoot == null) {
2688c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar    throw new RuntimeException("variable supportRootFolder is not set. you must set it before" +
2788c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar            " including this script")
2888c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar}
2988c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyardef init = new Properties()
3088c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyarext.init = init
311b862373b6693eb73cfcc88338dab94dbb788052Alan Viveretteext.init.debugKeystore = file("${supportRoot}/development/keystore/debug.keystore")
32a9ac19d133f6d727d979027aef8318d90fbd8e88Yigit BoyarrootProject.ext.versionChecker = new GMavenVersionChecker(rootProject)
339ab3b4c389856fa168165423297d468ccf108a90Aurimas Liutikasext.runningInBuildServer = System.env.DIST_DIR != null && System.env.OUT_DIR != null
3488c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar
3588c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyarapply from: "${supportRoot}/buildSrc/dependencies.gradle"
365b3946f819d6876ff46784ed1bfd16828b43448aAlan Viveretteext.docs = [:]
375b3946f819d6876ff46784ed1bfd16828b43448aAlan Viveretteext.docs.offline = rootProject.getProperties().containsKey("offlineDocs")
385b3946f819d6876ff46784ed1bfd16828b43448aAlan Viveretteext.docs.dac = [
395b3946f819d6876ff46784ed1bfd16828b43448aAlan Viverette        libraryroot: "android/support",
405b3946f819d6876ff46784ed1bfd16828b43448aAlan Viverette        dataname: "SUPPORT_DATA"
415b3946f819d6876ff46784ed1bfd16828b43448aAlan Viverette]
4288c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar
4388c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyardef enableDoclavaAndJDiff(p) {
4488c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar    p.configurations {
4588c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar        doclava
4688c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar        jdiff
4788c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar    }
4888c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar
4988c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar    p.dependencies {
50eca7a079412cee02f90ebd131ea646dda532e280Aurimas Liutikas        doclava project(':doclava')
51eca7a079412cee02f90ebd131ea646dda532e280Aurimas Liutikas        jdiff project(':jdiff')
5288c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar        jdiff libs.xml_parser_apis
5388c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar        jdiff libs.xerces_impl
5488c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar    }
5588c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar    apply from: "${ext.supportRootFolder}/buildSrc/diff_and_docs.gradle"
5688c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar}
5788c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar
58bb78f3f64b73d247a6e154b3170b410e47f6d73dAurimas Liutikasdef getFullSdkPath() {
59b06771d4778605ba8dc7af49d99a43a2750626a8Aurimas Liutikas    final String osName = System.getProperty("os.name").toLowerCase();
60b06771d4778605ba8dc7af49d99a43a2750626a8Aurimas Liutikas    final boolean isMacOsX =
61b06771d4778605ba8dc7af49d99a43a2750626a8Aurimas Liutikas            osName.contains("mac os x") || osName.contains("darwin") || osName.contains("osx");
62b06771d4778605ba8dc7af49d99a43a2750626a8Aurimas Liutikas    final String platform = isMacOsX ? 'darwin' : 'linux'
63bb78f3f64b73d247a6e154b3170b410e47f6d73dAurimas Liutikas    return "${repos.prebuiltsRoot}/fullsdk-${platform}"
64bb78f3f64b73d247a6e154b3170b410e47f6d73dAurimas Liutikas}
65bb78f3f64b73d247a6e154b3170b410e47f6d73dAurimas Liutikas
66bb78f3f64b73d247a6e154b3170b410e47f6d73dAurimas Liutikasdef setSdkInLocalPropertiesFile() {
67d2d9bda077323c3fb5b5918958f43108f89099a3Aurimas Liutikas    ext.buildToolsVersion = '26.0.0'
68bb78f3f64b73d247a6e154b3170b410e47f6d73dAurimas Liutikas    final String fullSdkPath = getFullSdkPath();
6988c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar    if (file(fullSdkPath).exists()) {
7088c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar        gradle.ext.currentSdk = 26
71eb9f3327cd7eb89326b34e9a63685d3a47e1243fAlan Viverette        project.ext.androidJar =
72eb9f3327cd7eb89326b34e9a63685d3a47e1243fAlan Viverette                files("${fullSdkPath}/platforms/android-${gradle.currentSdk}/android.jar")
73eb9f3327cd7eb89326b34e9a63685d3a47e1243fAlan Viverette        project.ext.androidSrcJar =
74eb9f3327cd7eb89326b34e9a63685d3a47e1243fAlan Viverette                file("${fullSdkPath}/platforms/android-${gradle.currentSdk}/android-stubs-src.jar")
75810a1483ccef0bb447fbe2e1056cea215ee725c1Alan Viverette        project.ext.androidApiTxt = null
7688c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar        File props = file("local.properties")
7788c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar        props.write "sdk.dir=${fullSdkPath}"
783c66600184c09ee5b8d089064cf569e448b0e3ebAurimas Liutikas        ext.usingFullSdk = true
7988c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar    } else {
8088c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar        gradle.ext.currentSdk = 'current'
819ab3b4c389856fa168165423297d468ccf108a90Aurimas Liutikas        project.ext.androidJar = files("${repos.prebuiltsRoot}/sdk/current/android.jar")
82810a1483ccef0bb447fbe2e1056cea215ee725c1Alan Viverette        project.ext.androidSrcJar = null
839ab3b4c389856fa168165423297d468ccf108a90Aurimas Liutikas        project.ext.androidApiTxt = file("${repos.prebuiltsRoot}/sdk/api/26.txt")
84bb78f3f64b73d247a6e154b3170b410e47f6d73dAurimas Liutikas        System.setProperty('android.dir', "${supportRootFolder}/../../")
8588c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar        File props = file("local.properties")
8688c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar        props.write "android.dir=../../"
873c66600184c09ee5b8d089064cf569e448b0e3ebAurimas Liutikas        ext.usingFullSdk = false
8888c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar    }
8988c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar}
9088c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar
9188c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyardef setupRepoOutAndBuildNumber() {
92ef300669d78824c4f9df6806ee9cd9f56969497cYigit Boyar    // common support repo folder which works well for prebuilts.
9388c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar    ext.supportRepoOut = ''
9476542da1882b14a6cbaf290d6f052cbcac048d4fAurimas Liutikas    ext.buildNumber = "0"
9588c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar    /*
9688c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar     * With the build server you are given two env variables.
9788c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar     * The OUT_DIR is a temporary directory you can use to put things during the build.
9888c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar     * The DIST_DIR is where you want to save things from the build.
9988c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar     *
10088c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar     * The build server will copy the contents of DIST_DIR to somewhere and make it available.
10188c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar     */
1027bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar    if (ext.runningInBuildServer) {
10388c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar        buildDir = new File(System.env.OUT_DIR + '/gradle/frameworks/support/build')
10488c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                .getCanonicalFile()
10588c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar        project.ext.distDir = new File(System.env.DIST_DIR).getCanonicalFile()
10688c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar
10788c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar        // the build server does not pass the build number so we infer it from the last folder of
10888c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar        // the dist path.
10988c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar        ext.buildNumber = project.ext.distDir.getName()
110a015004c3bb0c254e52a9fb61956fa1216eefbacAurimas Liutikas
111a015004c3bb0c254e52a9fb61956fa1216eefbacAurimas Liutikas        // the build server should always print out full stack traces for any failures.
112a015004c3bb0c254e52a9fb61956fa1216eefbacAurimas Liutikas        gradle.startParameter.showStacktrace = ShowStacktrace.ALWAYS
11388c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar    } else {
11488c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar        buildDir = file("${ext.supportRootFolder}/../../out/host/gradle/frameworks/support/build")
11588c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar        project.ext.distDir = new File("${ext.supportRootFolder}/../../out/dist")
11688c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar    }
11788c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar    subprojects {
11888c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar        // Change buildDir first so that all plugins pick up the new value.
11988c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar        project.buildDir = new File("$project.parent.buildDir/../$project.name/build")
12088c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar    }
12188c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar    ext.supportRepoOut = new File(buildDir, 'support_repo')
12288c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar    ext.testApkDistOut = ext.distDir
1237bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar    ext.testResultsDistDir = new File(distDir, "host-test-reports")
12488c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar    ext.docsDir = new File(buildDir, 'javadoc')
12588c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar}
12688c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar
12738e8f4d90824b11ba68ee733d82788cb90b35749Aurimas Liutikasdef configureBuildOnServer() {
12838e8f4d90824b11ba68ee733d82788cb90b35749Aurimas Liutikas    def buildOnServerTask = rootProject.tasks.create("buildOnServer")
12938e8f4d90824b11ba68ee733d82788cb90b35749Aurimas Liutikas    rootProject.tasks.whenTaskAdded { task ->
13038e8f4d90824b11ba68ee733d82788cb90b35749Aurimas Liutikas        if ("createArchive".equals(task.name)) {
13138e8f4d90824b11ba68ee733d82788cb90b35749Aurimas Liutikas            buildOnServerTask.dependsOn task
13238e8f4d90824b11ba68ee733d82788cb90b35749Aurimas Liutikas        }
13338e8f4d90824b11ba68ee733d82788cb90b35749Aurimas Liutikas    }
13438e8f4d90824b11ba68ee733d82788cb90b35749Aurimas Liutikas
13538e8f4d90824b11ba68ee733d82788cb90b35749Aurimas Liutikas    subprojects {
13638e8f4d90824b11ba68ee733d82788cb90b35749Aurimas Liutikas        project.tasks.whenTaskAdded { task ->
13738e8f4d90824b11ba68ee733d82788cb90b35749Aurimas Liutikas            if ("assembleErrorProne".equals(task.name) || "assembleAndroidTest".equals(task.name)) {
13838e8f4d90824b11ba68ee733d82788cb90b35749Aurimas Liutikas                buildOnServerTask.dependsOn task
13938e8f4d90824b11ba68ee733d82788cb90b35749Aurimas Liutikas            }
14038e8f4d90824b11ba68ee733d82788cb90b35749Aurimas Liutikas        }
14138e8f4d90824b11ba68ee733d82788cb90b35749Aurimas Liutikas    }
14238e8f4d90824b11ba68ee733d82788cb90b35749Aurimas Liutikas}
14338e8f4d90824b11ba68ee733d82788cb90b35749Aurimas Liutikas
14488c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyardef configureSubProjects() {
14588c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar    // lint every library
14688c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar    def lintTask = project.tasks.create("lint")
14788c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar    subprojects {
1486509a1ac4e78eb6c45526ee3434dec977ac22dc0Aurimas Liutikas        repos.addMavenRepositories(repositories)
1496509a1ac4e78eb6c45526ee3434dec977ac22dc0Aurimas Liutikas
15088c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar        // Only modify Android projects.
151eca7a079412cee02f90ebd131ea646dda532e280Aurimas Liutikas        if (project.name.equals('doclava')
152eca7a079412cee02f90ebd131ea646dda532e280Aurimas Liutikas                || project.name.equals('jdiff')
153da9ee6b28fd0c74f771be5c10b3407472a2d3cecHyundo Moon                || project.name.equals('noto-emoji-compat')
154da9ee6b28fd0c74f771be5c10b3407472a2d3cecHyundo Moon                || project.name.equals('support-media-compat-test-lib')) {
1557bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar            // disable tests and return
1567bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar            project.tasks.whenTaskAdded { task ->
1577bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar                if (task instanceof org.gradle.api.tasks.testing.Test) {
1587bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar                    task.enabled = false
1597bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar                }
1607bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar            }
1617bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar            return
1627bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar        }
16388c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar
16488c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar        project.ext.currentSdk = gradle.ext.currentSdk
16588c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar        apply plugin: 'maven'
16688c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar
167b938b2eab0a90d4f9482067f3802f54c4834d2c1Aurimas Liutikas        version = LibraryVersions.SUPPORT_LIBRARY.toString();
16888c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar        group = 'com.android.support'
16988c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar
17088c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar        project.plugins.whenPluginAdded { plugin ->
17188c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar            def isAndroidLibrary = "com.android.build.gradle.LibraryPlugin"
17288c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                    .equals(plugin.class.name)
17388c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar            def isAndroidApp = "com.android.build.gradle.AppPlugin".equals(plugin.class.name)
17488c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar            def isJavaLibrary = "org.gradle.api.plugins.JavaPlugin".equals(plugin.class.name)
17588c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar
17688c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar            if (isAndroidLibrary || isAndroidApp) {
17788c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                project.android.buildToolsVersion = rootProject.buildToolsVersion
17888c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar
17988c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                // Enable code coverage for debug builds only if we are not running inside the IDE,
18088c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                // since enabling coverage reports breaks the method parameter resolution in the IDE
18188c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                // debugger.
18288c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                project.android.buildTypes.debug.testCoverageEnabled =
183ac8e72bb65d917e7c3f6f0ad066a89296a67c479Sergey Vasilinets                        !project.hasProperty('android.injected.invoked.from.ide')
18488c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar
18588c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                // Copy the class files in a jar to be later used to generate code coverage report
18688c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                project.android.testVariants.all { v ->
18788c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                    // check if the variant has any source files
18888c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                    // and test coverage is enabled
18988c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                    if (v.buildType.testCoverageEnabled
19088c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                            && v.sourceSets.any { !it.java.sourceFiles.isEmpty() }) {
19188c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                        def jarifyTask = project.tasks.create(
19288c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                                name: "package${v.name.capitalize()}ClassFilesForCoverageReport",
19388c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                                type: Jar) {
19488c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                            from v.testedVariant.javaCompile.destinationDir
19588c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                            exclude "**/R.class"
19688c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                            exclude "**/R\$*.class"
19788c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                            exclude "**/BuildConfig.class"
19888c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                            destinationDir file(project.distDir)
19976542da1882b14a6cbaf290d6f052cbcac048d4fAurimas Liutikas                            archiveName "${project.name}-${v.baseName}-allclasses.jar"
20088c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                        }
20121b7fce00b5a98efde61158327cd461fe886428fAurimas Liutikas
20288c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                        def collectJacocoAntPackages = project.tasks.create(
20388c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                                name: "collectJacocoAntPackages",
20488c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                                type: Jar) {
20521b7fce00b5a98efde61158327cd461fe886428fAurimas Liutikas                            inputs.files project.configurations[JacocoPlugin.ANT_CONFIGURATION_NAME]
20621b7fce00b5a98efde61158327cd461fe886428fAurimas Liutikas                            from {
207585a934133fa85d80552488a915d3342e0d6e2c5Aurimas Liutikas                                project.configurations[JacocoPlugin.ANT_CONFIGURATION_NAME]
208585a934133fa85d80552488a915d3342e0d6e2c5Aurimas Liutikas                                        .resolvedConfiguration
209585a934133fa85d80552488a915d3342e0d6e2c5Aurimas Liutikas                                        .resolvedArtifacts.collect{ zipTree(it.getFile()) }} {
21021b7fce00b5a98efde61158327cd461fe886428fAurimas Liutikas                                    // exclude all the signatures the jar might have
21121b7fce00b5a98efde61158327cd461fe886428fAurimas Liutikas                                    exclude "META-INF/*.SF"
21221b7fce00b5a98efde61158327cd461fe886428fAurimas Liutikas                                    exclude "META-INF/*.DSA"
21321b7fce00b5a98efde61158327cd461fe886428fAurimas Liutikas                                    exclude "META-INF/*.RSA"
21421b7fce00b5a98efde61158327cd461fe886428fAurimas Liutikas                                }
21588c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                            destinationDir file(project.distDir)
21621b7fce00b5a98efde61158327cd461fe886428fAurimas Liutikas                            archiveName "jacocoant.jar"
21788c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                        }
21821b7fce00b5a98efde61158327cd461fe886428fAurimas Liutikas
21988c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                        jarifyTask.dependsOn v.getJavaCompiler()
22021b7fce00b5a98efde61158327cd461fe886428fAurimas Liutikas                        v.assemble.dependsOn jarifyTask , collectJacocoAntPackages
22188c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                    }
22288c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                }
22388c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar
22488c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                // Enforce NewApi lint check as fatal.
22588c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                project.android.lintOptions.fatal 'NewApi'
2266c6257048e8d06248106e0acc44602cdf82aef41Aurimas Liutikas                lintTask.dependsOn {project.lint}
22788c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar            }
22888c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar
22988c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar            if (isAndroidLibrary || isJavaLibrary) {
23088c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                // Add library to the aggregate dependency report.
23188c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                task allDeps(type: DependencyReportTask) {}
2326509a1ac4e78eb6c45526ee3434dec977ac22dc0Aurimas Liutikas
2336509a1ac4e78eb6c45526ee3434dec977ac22dc0Aurimas Liutikas                project.afterEvaluate {
2346509a1ac4e78eb6c45526ee3434dec977ac22dc0Aurimas Liutikas                    Upload uploadTask = (Upload) project.tasks.uploadArchives;
2356509a1ac4e78eb6c45526ee3434dec977ac22dc0Aurimas Liutikas                    uploadTask.repositories.mavenDeployer {
236279780d41883518f007c5b9669677a8c6dc7cbfeAurimas Liutikas                        repository(url: uri("$rootProject.ext.supportRepoOut"))
237279780d41883518f007c5b9669677a8c6dc7cbfeAurimas Liutikas                        setUniqueVersion(true)
23888c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                    }
23988c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar
2406509a1ac4e78eb6c45526ee3434dec977ac22dc0Aurimas Liutikas                    // Before the upload, make sure the repo is ready.
2416509a1ac4e78eb6c45526ee3434dec977ac22dc0Aurimas Liutikas                    uploadTask.dependsOn rootProject.tasks.prepareRepo
2426509a1ac4e78eb6c45526ee3434dec977ac22dc0Aurimas Liutikas
2436509a1ac4e78eb6c45526ee3434dec977ac22dc0Aurimas Liutikas                    // Make the mainupload depend on this one.
2446509a1ac4e78eb6c45526ee3434dec977ac22dc0Aurimas Liutikas                    mainUpload.dependsOn uploadTask
2456509a1ac4e78eb6c45526ee3434dec977ac22dc0Aurimas Liutikas                }
24688c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar            }
24788c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar        }
24888c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar
2497bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar        // Copy instrumentation test APKs and app APKs into the dist dir
2507bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar        // For test apks, they are uploaded only if we have java test sources.
2517bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar        // For regular app apks, they are uploaded only if they have java sources.
2527bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar        project.tasks.whenTaskAdded { task ->
2537bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar            if (task.name.startsWith("packageDebug")) {
2547bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar                def testApk = task.name.contains("AndroidTest")
2557bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar                task.doLast {
2567bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar                    def source = testApk ? project.android.sourceSets.androidTest
2577bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar                            : project.android.sourceSets.main
258ef3b492b5ecf5fab39d69d990c26f9ddad6dc4e7Aurimas Liutikas                    if (task.hasProperty("outputDirectory") && !source.java.sourceFiles.isEmpty()) {
2597bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar                        copy {
260ef3b492b5ecf5fab39d69d990c26f9ddad6dc4e7Aurimas Liutikas                            from(task.outputDirectory)
261ef3b492b5ecf5fab39d69d990c26f9ddad6dc4e7Aurimas Liutikas                            include '*.apk'
2627bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar                            into(rootProject.ext.testApkDistOut)
2637bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar                            rename { String fileName ->
2649938e17b8b278c82cf8011ccbdb657710742812eHyundo Moon                                // Exclude media-compat-test-* modules from existing support library
2659938e17b8b278c82cf8011ccbdb657710742812eHyundo Moon                                // presubmit tests.
2669938e17b8b278c82cf8011ccbdb657710742812eHyundo Moon                                if (fileName.contains("media-compat-test")) {
2679938e17b8b278c82cf8011ccbdb657710742812eHyundo Moon                                    fileName.replace("-debug-androidTest", "")
26896a0fcd9868e89a059c9e823fa117090b45a7e27Yigit Boyar                                } else {
26996a0fcd9868e89a059c9e823fa117090b45a7e27Yigit Boyar                                    // multiple modules may have the same name so prefix the name with
27096a0fcd9868e89a059c9e823fa117090b45a7e27Yigit Boyar                                    // the module's path to ensure it is unique.
27196a0fcd9868e89a059c9e823fa117090b45a7e27Yigit Boyar                                    // e.g. palette-v7-debug-androidTest.apk becomes
27296a0fcd9868e89a059c9e823fa117090b45a7e27Yigit Boyar                                    // support-palette-v7_palette-v7-debug-androidTest.apk
27396a0fcd9868e89a059c9e823fa117090b45a7e27Yigit Boyar                                    "${project.getPath().replace(':', '-').substring(1)}_${fileName}"
2749938e17b8b278c82cf8011ccbdb657710742812eHyundo Moon                                }
2757bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar                            }
2767bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar                        }
2777bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar                    }
2787bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar                }
2797bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar            }
2807bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar        }
2817bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar
2827bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar        // copy host side test results to DIST
2837bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar        project.tasks.whenTaskAdded { task ->
2847bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar            if (task instanceof org.gradle.api.tasks.testing.Test) {
2857bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar                def junitReport = task.reports.junitXml
2867bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar                if (junitReport.enabled) {
2877bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar                    def zipTask = project.tasks.create(name : "zipResultsOf${task.name.capitalize()}", type : Zip) {
2887bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar                        destinationDir(testResultsDistDir)
289278676dd187e3306c3e63f34f293f24a83dbc604Yigit Boyar                        // first one is always :, drop it.
290278676dd187e3306c3e63f34f293f24a83dbc604Yigit Boyar                        archiveName("${project.getPath().split(":").join("_").substring(1)}.zip")
2917bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar                    }
2927bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar                    if (project.rootProject.ext.runningInBuildServer) {
2937bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar                        task.ignoreFailures = true
2947bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar                    }
2957bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar                    task.finalizedBy zipTask
2967bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar                    task.doFirst {
2977bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar                        zipTask.from(junitReport.destination)
2987bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar                    }
2997bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar                }
3007bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar            }
3017bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar        }
3027bfacb70bcf6cc6f9307f994dfbb14588b5ce1b6Yigit Boyar
30388c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar        project.afterEvaluate { p ->
30488c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar            // remove dependency on the test so that we still get coverage even if some tests fail
30588c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar            p.tasks.findAll { it instanceof JacocoReportTask }.each { task ->
30688c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                def toBeRemoved = new ArrayList()
30788c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                def dependencyList = task.taskDependencies.values
30888c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                dependencyList.each { dep ->
30988c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                    if (dep instanceof String) {
31088c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                        def t = tasks.findByName(dep)
31188c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                        if (t instanceof DeviceProviderInstrumentTestTask) {
31288c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                            toBeRemoved.add(dep)
31388c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                            task.mustRunAfter(t)
31488c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                        }
31588c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                    }
31688c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                }
31788c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                toBeRemoved.each { dep ->
31888c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                    dependencyList.remove(dep)
31988c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar                }
32088c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar            }
32188c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar        }
32288c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar    }
32388c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar}
32488c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar
32588c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyardef setupRelease() {
32688c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar    apply from: "${ext.supportRootFolder}/buildSrc/release.gradle"
32788c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar}
32888c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyar
32988c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyarext.init.enableDoclavaAndJDiff = this.&enableDoclavaAndJDiff
33088c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyarext.init.setSdkInLocalPropertiesFile = this.&setSdkInLocalPropertiesFile
33188c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyarext.init.setupRepoOutAndBuildNumber = this.&setupRepoOutAndBuildNumber
33288c16ceecc0725f89d4b9c44cbf5ef51c1d7360dYigit Boyarext.init.setupRelease = this.&setupRelease
33338e8f4d90824b11ba68ee733d82788cb90b35749Aurimas Liutikasext.init.configureSubProjects = this.&configureSubProjects
334e1bbf71e07cdf216510faed6174e30e78b862bd8Yigit Boyarext.init.configureBuildOnServer = this.&configureBuildOnServer
335