blob: 700bd76c7b026e8e61022d688699b59a0862cba0 [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 Boyar88c16ce2017-02-08 16:06:14 -080022
23def supportRoot = ext.supportRootFolder
24if (supportRoot == null) {
25 throw new RuntimeException("variable supportRootFolder is not set. you must set it before" +
26 " including this script")
27}
28def init = new Properties()
29ext.init = init
Alan Viverette1b862372017-03-22 11:32:28 -040030ext.init.debugKeystore = file("${supportRoot}/development/keystore/debug.keystore")
Yigit Boyar88c16ce2017-02-08 16:06:14 -080031
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -070032ext.runningInBuildServer = System.env.DIST_DIR != null && System.env.OUT_DIR != null
Yigit Boyar88c16ce2017-02-08 16:06:14 -080033
34apply from: "${supportRoot}/buildSrc/dependencies.gradle"
Alan Viverette5b3946f2017-04-27 09:59:22 -040035ext.docs = [:]
36ext.docs.offline = rootProject.getProperties().containsKey("offlineDocs")
37ext.docs.dac = [
38 libraryroot: "android/support",
39 dataname: "SUPPORT_DATA"
40]
Yigit Boyar88c16ce2017-02-08 16:06:14 -080041
Yigit Boyar88c16ce2017-02-08 16:06:14 -080042def enableDoclavaAndJDiff(p) {
43 p.configurations {
44 doclava
45 jdiff
46 }
47
48 p.dependencies {
Aurimas Liutikaseca7a072017-07-11 01:11:52 +000049 doclava project(':doclava')
50 jdiff project(':jdiff')
Yigit Boyar88c16ce2017-02-08 16:06:14 -080051 jdiff libs.xml_parser_apis
52 jdiff libs.xerces_impl
53 }
54 apply from: "${ext.supportRootFolder}/buildSrc/diff_and_docs.gradle"
55}
56
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070057def getFullSdkPath() {
Aurimas Liutikasb06771d2017-04-20 09:46:49 -070058 final String osName = System.getProperty("os.name").toLowerCase();
59 final boolean isMacOsX =
60 osName.contains("mac os x") || osName.contains("darwin") || osName.contains("osx");
61 final String platform = isMacOsX ? 'darwin' : 'linux'
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070062 return "${repos.prebuiltsRoot}/fullsdk-${platform}"
63}
64
65def setSdkInLocalPropertiesFile() {
Aurimas Liutikasd2d9bda2017-03-03 16:06:39 -080066 ext.buildToolsVersion = '26.0.0'
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070067 final String fullSdkPath = getFullSdkPath();
Yigit Boyar88c16ce2017-02-08 16:06:14 -080068 if (file(fullSdkPath).exists()) {
69 gradle.ext.currentSdk = 26
Alan Viveretteeb9f3322017-03-09 16:29:57 -050070 project.ext.androidJar =
71 files("${fullSdkPath}/platforms/android-${gradle.currentSdk}/android.jar")
72 project.ext.androidSrcJar =
73 file("${fullSdkPath}/platforms/android-${gradle.currentSdk}/android-stubs-src.jar")
Alan Viverette810a1482017-03-20 12:43:43 -040074 project.ext.androidApiTxt = null
Yigit Boyar88c16ce2017-02-08 16:06:14 -080075 File props = file("local.properties")
76 props.write "sdk.dir=${fullSdkPath}"
Aurimas Liutikas3c666002017-03-08 19:30:28 -080077 ext.usingFullSdk = true
Yigit Boyar88c16ce2017-02-08 16:06:14 -080078 } else {
79 gradle.ext.currentSdk = 'current'
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -070080 project.ext.androidJar = files("${repos.prebuiltsRoot}/sdk/current/android.jar")
Alan Viverette810a1482017-03-20 12:43:43 -040081 project.ext.androidSrcJar = null
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -070082 project.ext.androidApiTxt = file("${repos.prebuiltsRoot}/sdk/api/26.txt")
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070083 System.setProperty('android.dir', "${supportRootFolder}/../../")
Yigit Boyar88c16ce2017-02-08 16:06:14 -080084 File props = file("local.properties")
85 props.write "android.dir=../../"
Aurimas Liutikas3c666002017-03-08 19:30:28 -080086 ext.usingFullSdk = false
Yigit Boyar88c16ce2017-02-08 16:06:14 -080087 }
88}
89
90def setupRepoOutAndBuildNumber() {
Yigit Boyaref300662017-05-01 11:52:07 -070091 // common support repo folder which works well for prebuilts.
Yigit Boyar88c16ce2017-02-08 16:06:14 -080092 ext.supportRepoOut = ''
Aurimas Liutikas76542da2017-06-29 17:00:29 -070093 ext.buildNumber = "0"
Yigit Boyar88c16ce2017-02-08 16:06:14 -080094 /*
95 * With the build server you are given two env variables.
96 * The OUT_DIR is a temporary directory you can use to put things during the build.
97 * The DIST_DIR is where you want to save things from the build.
98 *
99 * The build server will copy the contents of DIST_DIR to somewhere and make it available.
100 */
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800101 if (ext.runningInBuildServer) {
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800102 buildDir = new File(System.env.OUT_DIR + '/gradle/frameworks/support/build')
103 .getCanonicalFile()
104 project.ext.distDir = new File(System.env.DIST_DIR).getCanonicalFile()
105
106 // the build server does not pass the build number so we infer it from the last folder of
107 // the dist path.
108 ext.buildNumber = project.ext.distDir.getName()
Aurimas Liutikasa0150042017-04-21 09:46:12 -0700109
110 // the build server should always print out full stack traces for any failures.
111 gradle.startParameter.showStacktrace = ShowStacktrace.ALWAYS
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800112 } else {
113 buildDir = file("${ext.supportRootFolder}/../../out/host/gradle/frameworks/support/build")
114 project.ext.distDir = new File("${ext.supportRootFolder}/../../out/dist")
115 }
116 subprojects {
117 // Change buildDir first so that all plugins pick up the new value.
118 project.buildDir = new File("$project.parent.buildDir/../$project.name/build")
119 }
120 ext.supportRepoOut = new File(buildDir, 'support_repo')
121 ext.testApkDistOut = ext.distDir
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800122 ext.testResultsDistDir = new File(distDir, "host-test-reports")
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800123 ext.docsDir = new File(buildDir, 'javadoc')
124}
125
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700126def configureBuildOnServer() {
127 def buildOnServerTask = rootProject.tasks.create("buildOnServer")
128 rootProject.tasks.whenTaskAdded { task ->
129 if ("createArchive".equals(task.name)) {
130 buildOnServerTask.dependsOn task
131 }
132 }
133
134 subprojects {
135 project.tasks.whenTaskAdded { task ->
136 if ("assembleErrorProne".equals(task.name) || "assembleAndroidTest".equals(task.name)) {
137 buildOnServerTask.dependsOn task
138 }
139 }
140 }
141}
142
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800143def configureSubProjects() {
144 // lint every library
145 def lintTask = project.tasks.create("lint")
146 subprojects {
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700147 repos.addMavenRepositories(repositories)
148
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800149 // Only modify Android projects.
Aurimas Liutikaseca7a072017-07-11 01:11:52 +0000150 if (project.name.equals('doclava')
151 || project.name.equals('jdiff')
152 || project.name.equals('support-testutils')
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
165 apply plugin: 'maven'
166
Aurimas Liutikasb938b2e2017-07-24 14:47:17 -0700167 version = LibraryVersions.SUPPORT_LIBRARY.toString();
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800168 group = 'com.android.support'
169
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800170 project.plugins.whenPluginAdded { plugin ->
171 def isAndroidLibrary = "com.android.build.gradle.LibraryPlugin"
172 .equals(plugin.class.name)
173 def isAndroidApp = "com.android.build.gradle.AppPlugin".equals(plugin.class.name)
174 def isJavaLibrary = "org.gradle.api.plugins.JavaPlugin".equals(plugin.class.name)
175
176 if (isAndroidLibrary || isAndroidApp) {
177 project.android.buildToolsVersion = rootProject.buildToolsVersion
178
179 // Enable code coverage for debug builds only if we are not running inside the IDE,
180 // since enabling coverage reports breaks the method parameter resolution in the IDE
181 // debugger.
182 project.android.buildTypes.debug.testCoverageEnabled =
Sergey Vasilinetsac8e72b2017-04-26 15:55:17 -0700183 !project.hasProperty('android.injected.invoked.from.ide')
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800184
185 // Copy the class files in a jar to be later used to generate code coverage report
186 project.android.testVariants.all { v ->
187 // check if the variant has any source files
188 // and test coverage is enabled
189 if (v.buildType.testCoverageEnabled
190 && v.sourceSets.any { !it.java.sourceFiles.isEmpty() }) {
191 def jarifyTask = project.tasks.create(
192 name: "package${v.name.capitalize()}ClassFilesForCoverageReport",
193 type: Jar) {
194 from v.testedVariant.javaCompile.destinationDir
195 exclude "**/R.class"
196 exclude "**/R\$*.class"
197 exclude "**/BuildConfig.class"
198 destinationDir file(project.distDir)
Aurimas Liutikas76542da2017-06-29 17:00:29 -0700199 archiveName "${project.name}-${v.baseName}-allclasses.jar"
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800200 }
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700201
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800202 def collectJacocoAntPackages = project.tasks.create(
203 name: "collectJacocoAntPackages",
204 type: Jar) {
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700205 inputs.files project.configurations[JacocoPlugin.ANT_CONFIGURATION_NAME]
206 from {
Aurimas Liutikas585a9342017-06-25 16:07:56 -0700207 project.configurations[JacocoPlugin.ANT_CONFIGURATION_NAME]
208 .resolvedConfiguration
209 .resolvedArtifacts.collect{ zipTree(it.getFile()) }} {
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700210 // exclude all the signatures the jar might have
211 exclude "META-INF/*.SF"
212 exclude "META-INF/*.DSA"
213 exclude "META-INF/*.RSA"
214 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800215 destinationDir file(project.distDir)
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700216 archiveName "jacocoant.jar"
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800217 }
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700218
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800219 jarifyTask.dependsOn v.getJavaCompiler()
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700220 v.assemble.dependsOn jarifyTask , collectJacocoAntPackages
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800221 }
222 }
223
224 // Enforce NewApi lint check as fatal.
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800225 project.android.lintOptions.fatal 'NewApi'
Aurimas Liutikas6c625702017-09-20 14:35:47 -0700226 lintTask.dependsOn {project.lint}
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800227 }
228
229 if (isAndroidLibrary || isJavaLibrary) {
230 // Add library to the aggregate dependency report.
231 task allDeps(type: DependencyReportTask) {}
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800232
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700233 project.afterEvaluate {
234 Upload uploadTask = (Upload) project.tasks.uploadArchives;
235 uploadTask.repositories.mavenDeployer {
Aurimas Liutikas279780d2017-08-09 14:03:59 -0700236 repository(url: uri("$rootProject.ext.supportRepoOut"))
237 setUniqueVersion(true)
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700238 }
239
240 // Before the upload, make sure the repo is ready.
241 uploadTask.dependsOn rootProject.tasks.prepareRepo
242
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700243 // Make the mainupload depend on this one.
244 mainUpload.dependsOn uploadTask
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800245 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800246 }
247 }
248
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800249 // Copy instrumentation test APKs and app APKs into the dist dir
250 // For test apks, they are uploaded only if we have java test sources.
251 // For regular app apks, they are uploaded only if they have java sources.
252 project.tasks.whenTaskAdded { task ->
253 if (task.name.startsWith("packageDebug")) {
254 def testApk = task.name.contains("AndroidTest")
255 task.doLast {
256 def source = testApk ? project.android.sourceSets.androidTest
257 : project.android.sourceSets.main
Aurimas Liutikasef3b4922017-06-28 10:44:59 -0700258 if (task.hasProperty("outputDirectory") && !source.java.sourceFiles.isEmpty()) {
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800259 copy {
Aurimas Liutikasef3b4922017-06-28 10:44:59 -0700260 from(task.outputDirectory)
261 include '*.apk'
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800262 into(rootProject.ext.testApkDistOut)
263 rename { String fileName ->
Hyundo Moon9938e172017-07-26 20:51:11 +0900264 // Exclude media-compat-test-* modules from existing support library
265 // presubmit tests.
266 if (fileName.contains("media-compat-test")) {
267 fileName.replace("-debug-androidTest", "")
Yigit Boyar96a0fcd2017-08-25 18:18:28 -0700268 } else {
269 // multiple modules may have the same name so prefix the name with
270 // the module's path to ensure it is unique.
271 // e.g. palette-v7-debug-androidTest.apk becomes
272 // support-palette-v7_palette-v7-debug-androidTest.apk
273 "${project.getPath().replace(':', '-').substring(1)}_${fileName}"
Hyundo Moon9938e172017-07-26 20:51:11 +0900274 }
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800275 }
276 }
277 }
278 }
279 }
280 }
281
282 // copy host side test results to DIST
283 project.tasks.whenTaskAdded { task ->
284 if (task instanceof org.gradle.api.tasks.testing.Test) {
285 def junitReport = task.reports.junitXml
286 if (junitReport.enabled) {
287 def zipTask = project.tasks.create(name : "zipResultsOf${task.name.capitalize()}", type : Zip) {
288 destinationDir(testResultsDistDir)
Yigit Boyar278676d2017-03-10 15:29:11 -0800289 // first one is always :, drop it.
290 archiveName("${project.getPath().split(":").join("_").substring(1)}.zip")
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800291 }
292 if (project.rootProject.ext.runningInBuildServer) {
293 task.ignoreFailures = true
294 }
295 task.finalizedBy zipTask
296 task.doFirst {
297 zipTask.from(junitReport.destination)
298 }
299 }
300 }
301 }
302
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800303 project.afterEvaluate { p ->
304 // remove dependency on the test so that we still get coverage even if some tests fail
305 p.tasks.findAll { it instanceof JacocoReportTask }.each { task ->
306 def toBeRemoved = new ArrayList()
307 def dependencyList = task.taskDependencies.values
308 dependencyList.each { dep ->
309 if (dep instanceof String) {
310 def t = tasks.findByName(dep)
311 if (t instanceof DeviceProviderInstrumentTestTask) {
312 toBeRemoved.add(dep)
313 task.mustRunAfter(t)
314 }
315 }
316 }
317 toBeRemoved.each { dep ->
318 dependencyList.remove(dep)
319 }
320 }
321 }
322 }
323}
324
325def setupRelease() {
326 apply from: "${ext.supportRootFolder}/buildSrc/release.gradle"
327}
328
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800329ext.init.enableDoclavaAndJDiff = this.&enableDoclavaAndJDiff
330ext.init.setSdkInLocalPropertiesFile = this.&setSdkInLocalPropertiesFile
331ext.init.setupRepoOutAndBuildNumber = this.&setupRepoOutAndBuildNumber
332ext.init.setupRelease = this.&setupRelease
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700333ext.init.configureSubProjects = this.&configureSubProjects
Yigit Boyare1bbf712017-03-08 13:52:37 -0800334ext.init.configureBuildOnServer = this.&configureBuildOnServer