blob: 6b613d051cabfe77f52bed8c88401bc66b72b068 [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
Aurimas Liutikasb938b2e2017-07-24 14:47:17 -070017import android.support.LibraryVersions
Yigit Boyar88c16ce2017-02-08 16:06:14 -080018import com.android.build.gradle.internal.coverage.JacocoPlugin
19import com.android.build.gradle.internal.coverage.JacocoReportTask
20import com.android.build.gradle.internal.tasks.DeviceProviderInstrumentTestTask
Aurimas Liutikasa0150042017-04-21 09:46:12 -070021import org.gradle.api.logging.configuration.ShowStacktrace
Yigit Boyara9ac19d2017-09-20 16:11:49 -070022import android.support.gmaven.GMavenVersionChecker
Yigit Boyar88c16ce2017-02-08 16:06:14 -080023
24def supportRoot = ext.supportRootFolder
25if (supportRoot == null) {
26 throw new RuntimeException("variable supportRootFolder is not set. you must set it before" +
27 " including this script")
28}
29def init = new Properties()
30ext.init = init
Alan Viverette1b862372017-03-22 11:32:28 -040031ext.init.debugKeystore = file("${supportRoot}/development/keystore/debug.keystore")
Yigit Boyara9ac19d2017-09-20 16:11:49 -070032rootProject.ext.versionChecker = new GMavenVersionChecker(rootProject)
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -070033ext.runningInBuildServer = System.env.DIST_DIR != null && System.env.OUT_DIR != null
Yigit Boyar88c16ce2017-02-08 16:06:14 -080034
35apply from: "${supportRoot}/buildSrc/dependencies.gradle"
Alan Viverette5b3946f2017-04-27 09:59:22 -040036ext.docs = [:]
37ext.docs.offline = rootProject.getProperties().containsKey("offlineDocs")
38ext.docs.dac = [
39 libraryroot: "android/support",
40 dataname: "SUPPORT_DATA"
41]
Yigit Boyar88c16ce2017-02-08 16:06:14 -080042
Yigit Boyar88c16ce2017-02-08 16:06:14 -080043def enableDoclavaAndJDiff(p) {
44 p.configurations {
45 doclava
46 jdiff
47 }
48
49 p.dependencies {
Aurimas Liutikaseca7a072017-07-11 01:11:52 +000050 doclava project(':doclava')
51 jdiff project(':jdiff')
Yigit Boyar88c16ce2017-02-08 16:06:14 -080052 jdiff libs.xml_parser_apis
53 jdiff libs.xerces_impl
54 }
55 apply from: "${ext.supportRootFolder}/buildSrc/diff_and_docs.gradle"
56}
57
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070058def getFullSdkPath() {
Aurimas Liutikasb06771d2017-04-20 09:46:49 -070059 final String osName = System.getProperty("os.name").toLowerCase();
60 final boolean isMacOsX =
61 osName.contains("mac os x") || osName.contains("darwin") || osName.contains("osx");
62 final String platform = isMacOsX ? 'darwin' : 'linux'
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070063 return "${repos.prebuiltsRoot}/fullsdk-${platform}"
64}
65
66def setSdkInLocalPropertiesFile() {
Aurimas Liutikas97c75372017-09-28 17:44:34 -070067 ext.buildToolsVersion = '27.0.0'
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070068 final String fullSdkPath = getFullSdkPath();
Yigit Boyar88c16ce2017-02-08 16:06:14 -080069 if (file(fullSdkPath).exists()) {
70 gradle.ext.currentSdk = 26
Alan Viveretteeb9f3322017-03-09 16:29:57 -050071 project.ext.androidJar =
72 files("${fullSdkPath}/platforms/android-${gradle.currentSdk}/android.jar")
73 project.ext.androidSrcJar =
74 file("${fullSdkPath}/platforms/android-${gradle.currentSdk}/android-stubs-src.jar")
Alan Viverette810a1482017-03-20 12:43:43 -040075 project.ext.androidApiTxt = null
Yigit Boyar88c16ce2017-02-08 16:06:14 -080076 File props = file("local.properties")
77 props.write "sdk.dir=${fullSdkPath}"
Aurimas Liutikas3c666002017-03-08 19:30:28 -080078 ext.usingFullSdk = true
Yigit Boyar88c16ce2017-02-08 16:06:14 -080079 } else {
80 gradle.ext.currentSdk = 'current'
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -070081 project.ext.androidJar = files("${repos.prebuiltsRoot}/sdk/current/android.jar")
Alan Viverette810a1482017-03-20 12:43:43 -040082 project.ext.androidSrcJar = null
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -070083 project.ext.androidApiTxt = file("${repos.prebuiltsRoot}/sdk/api/26.txt")
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070084 System.setProperty('android.dir', "${supportRootFolder}/../../")
Yigit Boyar88c16ce2017-02-08 16:06:14 -080085 File props = file("local.properties")
86 props.write "android.dir=../../"
Aurimas Liutikas3c666002017-03-08 19:30:28 -080087 ext.usingFullSdk = false
Yigit Boyar88c16ce2017-02-08 16:06:14 -080088 }
89}
90
91def setupRepoOutAndBuildNumber() {
Yigit Boyaref300662017-05-01 11:52:07 -070092 // common support repo folder which works well for prebuilts.
Yigit Boyar88c16ce2017-02-08 16:06:14 -080093 ext.supportRepoOut = ''
Aurimas Liutikas76542da2017-06-29 17:00:29 -070094 ext.buildNumber = "0"
Yigit Boyar88c16ce2017-02-08 16:06:14 -080095 /*
96 * With the build server you are given two env variables.
97 * The OUT_DIR is a temporary directory you can use to put things during the build.
98 * The DIST_DIR is where you want to save things from the build.
99 *
100 * The build server will copy the contents of DIST_DIR to somewhere and make it available.
101 */
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800102 if (ext.runningInBuildServer) {
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800103 buildDir = new File(System.env.OUT_DIR + '/gradle/frameworks/support/build')
104 .getCanonicalFile()
105 project.ext.distDir = new File(System.env.DIST_DIR).getCanonicalFile()
106
107 // the build server does not pass the build number so we infer it from the last folder of
108 // the dist path.
109 ext.buildNumber = project.ext.distDir.getName()
Aurimas Liutikasa0150042017-04-21 09:46:12 -0700110
111 // the build server should always print out full stack traces for any failures.
112 gradle.startParameter.showStacktrace = ShowStacktrace.ALWAYS
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800113 } else {
114 buildDir = file("${ext.supportRootFolder}/../../out/host/gradle/frameworks/support/build")
115 project.ext.distDir = new File("${ext.supportRootFolder}/../../out/dist")
116 }
117 subprojects {
118 // Change buildDir first so that all plugins pick up the new value.
119 project.buildDir = new File("$project.parent.buildDir/../$project.name/build")
120 }
121 ext.supportRepoOut = new File(buildDir, 'support_repo')
122 ext.testApkDistOut = ext.distDir
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800123 ext.testResultsDistDir = new File(distDir, "host-test-reports")
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800124 ext.docsDir = new File(buildDir, 'javadoc')
125}
126
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700127def configureBuildOnServer() {
128 def buildOnServerTask = rootProject.tasks.create("buildOnServer")
129 rootProject.tasks.whenTaskAdded { task ->
Aurimas Liutikas212905f2017-10-09 15:28:28 -0700130 if ("createArchive".equals(task.name) || "distDocs".equals(task.name)) {
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700131 buildOnServerTask.dependsOn task
132 }
133 }
134
135 subprojects {
136 project.tasks.whenTaskAdded { task ->
137 if ("assembleErrorProne".equals(task.name) || "assembleAndroidTest".equals(task.name)) {
138 buildOnServerTask.dependsOn task
139 }
140 }
141 }
142}
143
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800144def configureSubProjects() {
145 // lint every library
146 def lintTask = project.tasks.create("lint")
147 subprojects {
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700148 repos.addMavenRepositories(repositories)
149
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800150 // Only modify Android projects.
Aurimas Liutikaseca7a072017-07-11 01:11:52 +0000151 if (project.name.equals('doclava')
152 || project.name.equals('jdiff')
Hyundo Moonda9ee6b22017-07-21 14:32:12 +0900153 || project.name.equals('noto-emoji-compat')
154 || project.name.equals('support-media-compat-test-lib')) {
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800155 // disable tests and return
156 project.tasks.whenTaskAdded { task ->
157 if (task instanceof org.gradle.api.tasks.testing.Test) {
158 task.enabled = false
159 }
160 }
161 return
162 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800163
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800164 project.ext.currentSdk = gradle.ext.currentSdk
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800165
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800166 project.plugins.whenPluginAdded { plugin ->
167 def isAndroidLibrary = "com.android.build.gradle.LibraryPlugin"
168 .equals(plugin.class.name)
169 def isAndroidApp = "com.android.build.gradle.AppPlugin".equals(plugin.class.name)
170 def isJavaLibrary = "org.gradle.api.plugins.JavaPlugin".equals(plugin.class.name)
171
172 if (isAndroidLibrary || isAndroidApp) {
173 project.android.buildToolsVersion = rootProject.buildToolsVersion
174
175 // Enable code coverage for debug builds only if we are not running inside the IDE,
176 // since enabling coverage reports breaks the method parameter resolution in the IDE
177 // debugger.
178 project.android.buildTypes.debug.testCoverageEnabled =
Sergey Vasilinetsac8e72b2017-04-26 15:55:17 -0700179 !project.hasProperty('android.injected.invoked.from.ide')
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800180
181 // Copy the class files in a jar to be later used to generate code coverage report
182 project.android.testVariants.all { v ->
183 // check if the variant has any source files
184 // and test coverage is enabled
185 if (v.buildType.testCoverageEnabled
186 && v.sourceSets.any { !it.java.sourceFiles.isEmpty() }) {
187 def jarifyTask = project.tasks.create(
188 name: "package${v.name.capitalize()}ClassFilesForCoverageReport",
189 type: Jar) {
190 from v.testedVariant.javaCompile.destinationDir
191 exclude "**/R.class"
192 exclude "**/R\$*.class"
193 exclude "**/BuildConfig.class"
194 destinationDir file(project.distDir)
Aurimas Liutikas76542da2017-06-29 17:00:29 -0700195 archiveName "${project.name}-${v.baseName}-allclasses.jar"
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800196 }
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700197
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800198 def collectJacocoAntPackages = project.tasks.create(
199 name: "collectJacocoAntPackages",
200 type: Jar) {
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700201 inputs.files project.configurations[JacocoPlugin.ANT_CONFIGURATION_NAME]
202 from {
Aurimas Liutikas585a9342017-06-25 16:07:56 -0700203 project.configurations[JacocoPlugin.ANT_CONFIGURATION_NAME]
204 .resolvedConfiguration
205 .resolvedArtifacts.collect{ zipTree(it.getFile()) }} {
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700206 // exclude all the signatures the jar might have
207 exclude "META-INF/*.SF"
208 exclude "META-INF/*.DSA"
209 exclude "META-INF/*.RSA"
210 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800211 destinationDir file(project.distDir)
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700212 archiveName "jacocoant.jar"
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800213 }
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700214
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800215 jarifyTask.dependsOn v.getJavaCompiler()
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700216 v.assemble.dependsOn jarifyTask , collectJacocoAntPackages
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800217 }
218 }
219
220 // Enforce NewApi lint check as fatal.
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800221 project.android.lintOptions.fatal 'NewApi'
Aurimas Liutikas6c625702017-09-20 14:35:47 -0700222 lintTask.dependsOn {project.lint}
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800223 }
224
225 if (isAndroidLibrary || isJavaLibrary) {
226 // Add library to the aggregate dependency report.
227 task allDeps(type: DependencyReportTask) {}
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800228
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700229 project.afterEvaluate {
230 Upload uploadTask = (Upload) project.tasks.uploadArchives;
231 uploadTask.repositories.mavenDeployer {
Aurimas Liutikas279780d2017-08-09 14:03:59 -0700232 repository(url: uri("$rootProject.ext.supportRepoOut"))
233 setUniqueVersion(true)
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700234 }
235
236 // Before the upload, make sure the repo is ready.
237 uploadTask.dependsOn rootProject.tasks.prepareRepo
238
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700239 // Make the mainupload depend on this one.
240 mainUpload.dependsOn uploadTask
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800241 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800242 }
243 }
244
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800245 // Copy instrumentation test APKs and app APKs into the dist dir
246 // For test apks, they are uploaded only if we have java test sources.
247 // For regular app apks, they are uploaded only if they have java sources.
248 project.tasks.whenTaskAdded { task ->
249 if (task.name.startsWith("packageDebug")) {
250 def testApk = task.name.contains("AndroidTest")
251 task.doLast {
252 def source = testApk ? project.android.sourceSets.androidTest
253 : project.android.sourceSets.main
Yigit Boyare606d6d2017-09-07 12:37:36 -0700254 def hasKotlinSources = false
255 if (source.hasProperty('kotlin')) {
256 if (!source.kotlin.files.isEmpty()) {
257 hasKotlinSources = true
258 } else {
259 // kotlin files does not show in java sources due to the *.java filter
260 // so we need to check them manually
261 hasKotlinSources = source.java.sourceDirectoryTrees.any {
262 !fileTree(dir: it.dir, include:'**/*.kt').files.isEmpty()
263 }
264 }
265 }
266 def hasSourceCode = !source.java.sourceFiles.isEmpty() || hasKotlinSources
267 if (task.hasProperty("outputDirectory") && (hasSourceCode || !testApk)) {
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800268 copy {
Aurimas Liutikasef3b4922017-06-28 10:44:59 -0700269 from(task.outputDirectory)
270 include '*.apk'
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800271 into(rootProject.ext.testApkDistOut)
272 rename { String fileName ->
Hyundo Moon9938e172017-07-26 20:51:11 +0900273 // Exclude media-compat-test-* modules from existing support library
274 // presubmit tests.
275 if (fileName.contains("media-compat-test")) {
276 fileName.replace("-debug-androidTest", "")
Yigit Boyar96a0fcd2017-08-25 18:18:28 -0700277 } else {
278 // multiple modules may have the same name so prefix the name with
279 // the module's path to ensure it is unique.
280 // e.g. palette-v7-debug-androidTest.apk becomes
281 // support-palette-v7_palette-v7-debug-androidTest.apk
282 "${project.getPath().replace(':', '-').substring(1)}_${fileName}"
Hyundo Moon9938e172017-07-26 20:51:11 +0900283 }
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800284 }
285 }
286 }
287 }
288 }
289 }
290
291 // copy host side test results to DIST
292 project.tasks.whenTaskAdded { task ->
293 if (task instanceof org.gradle.api.tasks.testing.Test) {
294 def junitReport = task.reports.junitXml
295 if (junitReport.enabled) {
296 def zipTask = project.tasks.create(name : "zipResultsOf${task.name.capitalize()}", type : Zip) {
297 destinationDir(testResultsDistDir)
Yigit Boyar278676d2017-03-10 15:29:11 -0800298 // first one is always :, drop it.
299 archiveName("${project.getPath().split(":").join("_").substring(1)}.zip")
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800300 }
301 if (project.rootProject.ext.runningInBuildServer) {
302 task.ignoreFailures = true
303 }
304 task.finalizedBy zipTask
305 task.doFirst {
306 zipTask.from(junitReport.destination)
307 }
308 }
309 }
310 }
311
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800312 project.afterEvaluate { p ->
313 // remove dependency on the test so that we still get coverage even if some tests fail
314 p.tasks.findAll { it instanceof JacocoReportTask }.each { task ->
315 def toBeRemoved = new ArrayList()
316 def dependencyList = task.taskDependencies.values
317 dependencyList.each { dep ->
318 if (dep instanceof String) {
319 def t = tasks.findByName(dep)
320 if (t instanceof DeviceProviderInstrumentTestTask) {
321 toBeRemoved.add(dep)
322 task.mustRunAfter(t)
323 }
324 }
325 }
326 toBeRemoved.each { dep ->
327 dependencyList.remove(dep)
328 }
329 }
330 }
331 }
332}
333
334def setupRelease() {
335 apply from: "${ext.supportRootFolder}/buildSrc/release.gradle"
336}
337
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800338ext.init.enableDoclavaAndJDiff = this.&enableDoclavaAndJDiff
339ext.init.setSdkInLocalPropertiesFile = this.&setSdkInLocalPropertiesFile
340ext.init.setupRepoOutAndBuildNumber = this.&setupRepoOutAndBuildNumber
341ext.init.setupRelease = this.&setupRelease
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700342ext.init.configureSubProjects = this.&configureSubProjects
Yigit Boyare1bbf712017-03-08 13:52:37 -0800343ext.init.configureBuildOnServer = this.&configureBuildOnServer