blob: cc1c11ca45c4d741f0f5a41bb3c0499e3cc37694 [file] [log] [blame]
Yigit Boyar88c16ce2017-02-08 16:06:14 -08001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Sergey Vasilinets22b62142017-11-22 20:32:27 -080017
18import android.support.DacOptions
19import android.support.DiffAndDocs
Aurimas Liutikas1187af12017-11-28 15:28:01 -080020import android.support.gmaven.GMavenVersionChecker
Yigit Boyar88c16ce2017-02-08 16:06:14 -080021import com.android.build.gradle.internal.coverage.JacocoPlugin
22import com.android.build.gradle.internal.coverage.JacocoReportTask
23import com.android.build.gradle.internal.tasks.DeviceProviderInstrumentTestTask
Aurimas Liutikasa0150042017-04-21 09:46:12 -070024import org.gradle.api.logging.configuration.ShowStacktrace
Yigit Boyar88c16ce2017-02-08 16:06:14 -080025
26def supportRoot = ext.supportRootFolder
27if (supportRoot == null) {
28 throw new RuntimeException("variable supportRootFolder is not set. you must set it before" +
29 " including this script")
30}
31def init = new Properties()
32ext.init = init
Alan Viverette1b862372017-03-22 11:32:28 -040033ext.init.debugKeystore = file("${supportRoot}/development/keystore/debug.keystore")
Yigit Boyara9ac19d2017-09-20 16:11:49 -070034rootProject.ext.versionChecker = new GMavenVersionChecker(rootProject)
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -070035ext.runningInBuildServer = System.env.DIST_DIR != null && System.env.OUT_DIR != null
Yigit Boyar88c16ce2017-02-08 16:06:14 -080036
37apply from: "${supportRoot}/buildSrc/dependencies.gradle"
Aurimas Liutikas1187af12017-11-28 15:28:01 -080038apply from: "${supportRoot}/buildSrc/build_dependencies.gradle"
Sergey Vasilinets22b62142017-11-22 20:32:27 -080039ext.docsDac = new DacOptions("android/support", "SUPPORT_DATA")
Yigit Boyar88c16ce2017-02-08 16:06:14 -080040
Yigit Boyar88c16ce2017-02-08 16:06:14 -080041def enableDoclavaAndJDiff(p) {
42 p.configurations {
43 doclava
44 jdiff
45 }
46
47 p.dependencies {
Aurimas Liutikaseca7a072017-07-11 01:11:52 +000048 doclava project(':doclava')
49 jdiff project(':jdiff')
Aurimas Liutikas1187af12017-11-28 15:28:01 -080050 jdiff build_libs.xml_parser_apis
51 jdiff build_libs.xerces_impl
Yigit Boyar88c16ce2017-02-08 16:06:14 -080052 }
Sergey Vasilinets22b62142017-11-22 20:32:27 -080053 DiffAndDocs.configureDiffAndDocs(rootProject, createArchive, supportRootFolder)
Yigit Boyar88c16ce2017-02-08 16:06:14 -080054}
55
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070056def getFullSdkPath() {
Aurimas Liutikasb06771d2017-04-20 09:46:49 -070057 final String osName = System.getProperty("os.name").toLowerCase();
58 final boolean isMacOsX =
59 osName.contains("mac os x") || osName.contains("darwin") || osName.contains("osx");
60 final String platform = isMacOsX ? 'darwin' : 'linux'
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070061 return "${repos.prebuiltsRoot}/fullsdk-${platform}"
62}
63
64def setSdkInLocalPropertiesFile() {
Aurimas Liutikas97c75372017-09-28 17:44:34 -070065 ext.buildToolsVersion = '27.0.0'
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070066 final String fullSdkPath = getFullSdkPath();
Yigit Boyar88c16ce2017-02-08 16:06:14 -080067 if (file(fullSdkPath).exists()) {
68 gradle.ext.currentSdk = 26
Alan Viveretteeb9f3322017-03-09 16:29:57 -050069 project.ext.androidJar =
70 files("${fullSdkPath}/platforms/android-${gradle.currentSdk}/android.jar")
71 project.ext.androidSrcJar =
72 file("${fullSdkPath}/platforms/android-${gradle.currentSdk}/android-stubs-src.jar")
Alan Viverette810a1482017-03-20 12:43:43 -040073 project.ext.androidApiTxt = null
Yigit Boyar88c16ce2017-02-08 16:06:14 -080074 File props = file("local.properties")
75 props.write "sdk.dir=${fullSdkPath}"
Aurimas Liutikas3c666002017-03-08 19:30:28 -080076 ext.usingFullSdk = true
Yigit Boyar88c16ce2017-02-08 16:06:14 -080077 } else {
78 gradle.ext.currentSdk = 'current'
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -070079 project.ext.androidJar = files("${repos.prebuiltsRoot}/sdk/current/android.jar")
Alan Viverette810a1482017-03-20 12:43:43 -040080 project.ext.androidSrcJar = null
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -070081 project.ext.androidApiTxt = file("${repos.prebuiltsRoot}/sdk/api/26.txt")
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070082 System.setProperty('android.dir', "${supportRootFolder}/../../")
Yigit Boyar88c16ce2017-02-08 16:06:14 -080083 File props = file("local.properties")
84 props.write "android.dir=../../"
Aurimas Liutikas3c666002017-03-08 19:30:28 -080085 ext.usingFullSdk = false
Yigit Boyar88c16ce2017-02-08 16:06:14 -080086 }
87}
88
89def setupRepoOutAndBuildNumber() {
Yigit Boyaref300662017-05-01 11:52:07 -070090 // common support repo folder which works well for prebuilts.
Yigit Boyar88c16ce2017-02-08 16:06:14 -080091 ext.supportRepoOut = ''
Aurimas Liutikas76542da2017-06-29 17:00:29 -070092 ext.buildNumber = "0"
Yigit Boyar88c16ce2017-02-08 16:06:14 -080093 /*
94 * With the build server you are given two env variables.
95 * The OUT_DIR is a temporary directory you can use to put things during the build.
96 * The DIST_DIR is where you want to save things from the build.
97 *
98 * The build server will copy the contents of DIST_DIR to somewhere and make it available.
99 */
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800100 if (ext.runningInBuildServer) {
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800101 buildDir = new File(System.env.OUT_DIR + '/gradle/frameworks/support/build')
102 .getCanonicalFile()
103 project.ext.distDir = new File(System.env.DIST_DIR).getCanonicalFile()
104
105 // the build server does not pass the build number so we infer it from the last folder of
106 // the dist path.
107 ext.buildNumber = project.ext.distDir.getName()
Aurimas Liutikasa0150042017-04-21 09:46:12 -0700108
109 // the build server should always print out full stack traces for any failures.
110 gradle.startParameter.showStacktrace = ShowStacktrace.ALWAYS
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800111 } else {
112 buildDir = file("${ext.supportRootFolder}/../../out/host/gradle/frameworks/support/build")
113 project.ext.distDir = new File("${ext.supportRootFolder}/../../out/dist")
114 }
115 subprojects {
116 // Change buildDir first so that all plugins pick up the new value.
117 project.buildDir = new File("$project.parent.buildDir/../$project.name/build")
118 }
119 ext.supportRepoOut = new File(buildDir, 'support_repo')
120 ext.testApkDistOut = ext.distDir
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800121 ext.testResultsDistDir = new File(distDir, "host-test-reports")
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800122 ext.docsDir = new File(buildDir, 'javadoc')
123}
124
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700125def configureBuildOnServer() {
126 def buildOnServerTask = rootProject.tasks.create("buildOnServer")
127 rootProject.tasks.whenTaskAdded { task ->
Aurimas Liutikas212905f2017-10-09 15:28:28 -0700128 if ("createArchive".equals(task.name) || "distDocs".equals(task.name)) {
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700129 buildOnServerTask.dependsOn task
130 }
131 }
132
133 subprojects {
134 project.tasks.whenTaskAdded { task ->
135 if ("assembleErrorProne".equals(task.name) || "assembleAndroidTest".equals(task.name)) {
136 buildOnServerTask.dependsOn task
137 }
138 }
139 }
140}
141
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800142def configureSubProjects() {
143 // lint every library
144 def lintTask = project.tasks.create("lint")
145 subprojects {
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700146 repos.addMavenRepositories(repositories)
147
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800148 // Only modify Android projects.
Aurimas Liutikaseca7a072017-07-11 01:11:52 +0000149 if (project.name.equals('doclava')
150 || project.name.equals('jdiff')
Hyundo Moonda9ee6b22017-07-21 14:32:12 +0900151 || project.name.equals('noto-emoji-compat')
152 || project.name.equals('support-media-compat-test-lib')) {
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800153 // disable tests and return
154 project.tasks.whenTaskAdded { task ->
155 if (task instanceof org.gradle.api.tasks.testing.Test) {
156 task.enabled = false
157 }
158 }
159 return
160 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800161
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800162 project.ext.currentSdk = gradle.ext.currentSdk
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800163
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800164 project.plugins.whenPluginAdded { plugin ->
165 def isAndroidLibrary = "com.android.build.gradle.LibraryPlugin"
166 .equals(plugin.class.name)
167 def isAndroidApp = "com.android.build.gradle.AppPlugin".equals(plugin.class.name)
168 def isJavaLibrary = "org.gradle.api.plugins.JavaPlugin".equals(plugin.class.name)
169
170 if (isAndroidLibrary || isAndroidApp) {
171 project.android.buildToolsVersion = rootProject.buildToolsVersion
172
173 // Enable code coverage for debug builds only if we are not running inside the IDE,
174 // since enabling coverage reports breaks the method parameter resolution in the IDE
175 // debugger.
176 project.android.buildTypes.debug.testCoverageEnabled =
Sergey Vasilinetsac8e72b2017-04-26 15:55:17 -0700177 !project.hasProperty('android.injected.invoked.from.ide')
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800178
179 // Copy the class files in a jar to be later used to generate code coverage report
180 project.android.testVariants.all { v ->
181 // check if the variant has any source files
182 // and test coverage is enabled
183 if (v.buildType.testCoverageEnabled
184 && v.sourceSets.any { !it.java.sourceFiles.isEmpty() }) {
185 def jarifyTask = project.tasks.create(
186 name: "package${v.name.capitalize()}ClassFilesForCoverageReport",
187 type: Jar) {
188 from v.testedVariant.javaCompile.destinationDir
189 exclude "**/R.class"
190 exclude "**/R\$*.class"
191 exclude "**/BuildConfig.class"
192 destinationDir file(project.distDir)
Aurimas Liutikas76542da2017-06-29 17:00:29 -0700193 archiveName "${project.name}-${v.baseName}-allclasses.jar"
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800194 }
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700195
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800196 def collectJacocoAntPackages = project.tasks.create(
197 name: "collectJacocoAntPackages",
198 type: Jar) {
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700199 inputs.files project.configurations[JacocoPlugin.ANT_CONFIGURATION_NAME]
200 from {
Aurimas Liutikas585a9342017-06-25 16:07:56 -0700201 project.configurations[JacocoPlugin.ANT_CONFIGURATION_NAME]
202 .resolvedConfiguration
203 .resolvedArtifacts.collect{ zipTree(it.getFile()) }} {
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700204 // exclude all the signatures the jar might have
205 exclude "META-INF/*.SF"
206 exclude "META-INF/*.DSA"
207 exclude "META-INF/*.RSA"
208 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800209 destinationDir file(project.distDir)
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700210 archiveName "jacocoant.jar"
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800211 }
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700212
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800213 jarifyTask.dependsOn v.getJavaCompiler()
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700214 v.assemble.dependsOn jarifyTask , collectJacocoAntPackages
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800215 }
216 }
217
218 // Enforce NewApi lint check as fatal.
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800219 project.android.lintOptions.fatal 'NewApi'
Aurimas Liutikas6c625702017-09-20 14:35:47 -0700220 lintTask.dependsOn {project.lint}
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800221 }
222
223 if (isAndroidLibrary || isJavaLibrary) {
224 // Add library to the aggregate dependency report.
225 task allDeps(type: DependencyReportTask) {}
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800226
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700227 project.afterEvaluate {
228 Upload uploadTask = (Upload) project.tasks.uploadArchives;
229 uploadTask.repositories.mavenDeployer {
Aurimas Liutikas279780d2017-08-09 14:03:59 -0700230 repository(url: uri("$rootProject.ext.supportRepoOut"))
231 setUniqueVersion(true)
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700232 }
233
234 // Before the upload, make sure the repo is ready.
235 uploadTask.dependsOn rootProject.tasks.prepareRepo
236
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700237 // Make the mainupload depend on this one.
238 mainUpload.dependsOn uploadTask
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800239 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800240 }
241 }
242
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800243 // Copy instrumentation test APKs and app APKs into the dist dir
244 // For test apks, they are uploaded only if we have java test sources.
245 // For regular app apks, they are uploaded only if they have java sources.
246 project.tasks.whenTaskAdded { task ->
247 if (task.name.startsWith("packageDebug")) {
248 def testApk = task.name.contains("AndroidTest")
249 task.doLast {
250 def source = testApk ? project.android.sourceSets.androidTest
251 : project.android.sourceSets.main
Yigit Boyare606d6d2017-09-07 12:37:36 -0700252 def hasKotlinSources = false
253 if (source.hasProperty('kotlin')) {
254 if (!source.kotlin.files.isEmpty()) {
255 hasKotlinSources = true
256 } else {
257 // kotlin files does not show in java sources due to the *.java filter
258 // so we need to check them manually
259 hasKotlinSources = source.java.sourceDirectoryTrees.any {
260 !fileTree(dir: it.dir, include:'**/*.kt').files.isEmpty()
261 }
262 }
263 }
264 def hasSourceCode = !source.java.sourceFiles.isEmpty() || hasKotlinSources
265 if (task.hasProperty("outputDirectory") && (hasSourceCode || !testApk)) {
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800266 copy {
Aurimas Liutikasef3b4922017-06-28 10:44:59 -0700267 from(task.outputDirectory)
268 include '*.apk'
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800269 into(rootProject.ext.testApkDistOut)
270 rename { String fileName ->
Hyundo Moon9938e172017-07-26 20:51:11 +0900271 // Exclude media-compat-test-* modules from existing support library
272 // presubmit tests.
273 if (fileName.contains("media-compat-test")) {
274 fileName.replace("-debug-androidTest", "")
Yigit Boyar96a0fcd2017-08-25 18:18:28 -0700275 } else {
276 // multiple modules may have the same name so prefix the name with
277 // the module's path to ensure it is unique.
278 // e.g. palette-v7-debug-androidTest.apk becomes
279 // support-palette-v7_palette-v7-debug-androidTest.apk
280 "${project.getPath().replace(':', '-').substring(1)}_${fileName}"
Hyundo Moon9938e172017-07-26 20:51:11 +0900281 }
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800282 }
283 }
284 }
285 }
286 }
287 }
288
289 // copy host side test results to DIST
290 project.tasks.whenTaskAdded { task ->
291 if (task instanceof org.gradle.api.tasks.testing.Test) {
292 def junitReport = task.reports.junitXml
293 if (junitReport.enabled) {
294 def zipTask = project.tasks.create(name : "zipResultsOf${task.name.capitalize()}", type : Zip) {
295 destinationDir(testResultsDistDir)
Yigit Boyar278676d2017-03-10 15:29:11 -0800296 // first one is always :, drop it.
297 archiveName("${project.getPath().split(":").join("_").substring(1)}.zip")
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800298 }
299 if (project.rootProject.ext.runningInBuildServer) {
300 task.ignoreFailures = true
301 }
302 task.finalizedBy zipTask
303 task.doFirst {
304 zipTask.from(junitReport.destination)
305 }
306 }
307 }
308 }
309
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800310 project.afterEvaluate { p ->
311 // remove dependency on the test so that we still get coverage even if some tests fail
312 p.tasks.findAll { it instanceof JacocoReportTask }.each { task ->
313 def toBeRemoved = new ArrayList()
314 def dependencyList = task.taskDependencies.values
315 dependencyList.each { dep ->
316 if (dep instanceof String) {
317 def t = tasks.findByName(dep)
318 if (t instanceof DeviceProviderInstrumentTestTask) {
319 toBeRemoved.add(dep)
320 task.mustRunAfter(t)
321 }
322 }
323 }
324 toBeRemoved.each { dep ->
325 dependencyList.remove(dep)
326 }
327 }
328 }
329 }
330}
331
332def setupRelease() {
333 apply from: "${ext.supportRootFolder}/buildSrc/release.gradle"
334}
335
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800336ext.init.enableDoclavaAndJDiff = this.&enableDoclavaAndJDiff
337ext.init.setSdkInLocalPropertiesFile = this.&setSdkInLocalPropertiesFile
338ext.init.setupRepoOutAndBuildNumber = this.&setupRepoOutAndBuildNumber
339ext.init.setupRelease = this.&setupRelease
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700340ext.init.configureSubProjects = this.&configureSubProjects
Yigit Boyare1bbf712017-03-08 13:52:37 -0800341ext.init.configureBuildOnServer = this.&configureBuildOnServer