blob: cfd24681fbb3f61abca53e10417d24769a79062d [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 Liutikas9ab3b4c2017-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 Liutikasd2d9bda2017-03-03 16:06:39 -080067 ext.buildToolsVersion = '26.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 Liutikas9ab3b4c2017-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 Liutikas9ab3b4c2017-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 Liutikas38e8f4d2017-06-16 08:31:03 -0700127def configureBuildOnServer() {
128 def buildOnServerTask = rootProject.tasks.create("buildOnServer")
129 rootProject.tasks.whenTaskAdded { task ->
130 if ("createArchive".equals(task.name)) {
131 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')
153 || project.name.equals('support-testutils')
Hyundo Moonda9ee6b2017-07-21 14:32:12 +0900154 || project.name.equals('noto-emoji-compat')
155 || project.name.equals('support-media-compat-test-lib')) {
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800156 // disable tests and return
157 project.tasks.whenTaskAdded { task ->
158 if (task instanceof org.gradle.api.tasks.testing.Test) {
159 task.enabled = false
160 }
161 }
162 return
163 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800164
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800165 project.ext.currentSdk = gradle.ext.currentSdk
166 apply plugin: 'maven'
167
Aurimas Liutikasb938b2e2017-07-24 14:47:17 -0700168 version = LibraryVersions.SUPPORT_LIBRARY.toString();
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800169 group = 'com.android.support'
170
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800171 project.plugins.whenPluginAdded { plugin ->
172 def isAndroidLibrary = "com.android.build.gradle.LibraryPlugin"
173 .equals(plugin.class.name)
174 def isAndroidApp = "com.android.build.gradle.AppPlugin".equals(plugin.class.name)
175 def isJavaLibrary = "org.gradle.api.plugins.JavaPlugin".equals(plugin.class.name)
176
177 if (isAndroidLibrary || isAndroidApp) {
178 project.android.buildToolsVersion = rootProject.buildToolsVersion
179
180 // Enable code coverage for debug builds only if we are not running inside the IDE,
181 // since enabling coverage reports breaks the method parameter resolution in the IDE
182 // debugger.
183 project.android.buildTypes.debug.testCoverageEnabled =
Sergey Vasilinetsac8e72b2017-04-26 15:55:17 -0700184 !project.hasProperty('android.injected.invoked.from.ide')
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800185
186 // Copy the class files in a jar to be later used to generate code coverage report
187 project.android.testVariants.all { v ->
188 // check if the variant has any source files
189 // and test coverage is enabled
190 if (v.buildType.testCoverageEnabled
191 && v.sourceSets.any { !it.java.sourceFiles.isEmpty() }) {
192 def jarifyTask = project.tasks.create(
193 name: "package${v.name.capitalize()}ClassFilesForCoverageReport",
194 type: Jar) {
195 from v.testedVariant.javaCompile.destinationDir
196 exclude "**/R.class"
197 exclude "**/R\$*.class"
198 exclude "**/BuildConfig.class"
199 destinationDir file(project.distDir)
Aurimas Liutikas76542da2017-06-29 17:00:29 -0700200 archiveName "${project.name}-${v.baseName}-allclasses.jar"
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800201 }
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700202
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800203 def collectJacocoAntPackages = project.tasks.create(
204 name: "collectJacocoAntPackages",
205 type: Jar) {
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700206 inputs.files project.configurations[JacocoPlugin.ANT_CONFIGURATION_NAME]
207 from {
Aurimas Liutikas585a9342017-06-25 16:07:56 -0700208 project.configurations[JacocoPlugin.ANT_CONFIGURATION_NAME]
209 .resolvedConfiguration
210 .resolvedArtifacts.collect{ zipTree(it.getFile()) }} {
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700211 // exclude all the signatures the jar might have
212 exclude "META-INF/*.SF"
213 exclude "META-INF/*.DSA"
214 exclude "META-INF/*.RSA"
215 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800216 destinationDir file(project.distDir)
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700217 archiveName "jacocoant.jar"
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800218 }
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700219
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800220 jarifyTask.dependsOn v.getJavaCompiler()
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700221 v.assemble.dependsOn jarifyTask , collectJacocoAntPackages
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800222 }
223 }
224
225 // Enforce NewApi lint check as fatal.
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800226 project.android.lintOptions.fatal 'NewApi'
Aurimas Liutikas6c625702017-09-20 14:35:47 -0700227 lintTask.dependsOn {project.lint}
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800228 }
229
230 if (isAndroidLibrary || isJavaLibrary) {
231 // Add library to the aggregate dependency report.
232 task allDeps(type: DependencyReportTask) {}
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800233
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700234 project.afterEvaluate {
235 Upload uploadTask = (Upload) project.tasks.uploadArchives;
236 uploadTask.repositories.mavenDeployer {
Aurimas Liutikas279780d2017-08-09 14:03:59 -0700237 repository(url: uri("$rootProject.ext.supportRepoOut"))
238 setUniqueVersion(true)
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700239 }
240
241 // Before the upload, make sure the repo is ready.
242 uploadTask.dependsOn rootProject.tasks.prepareRepo
243
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700244 // Make the mainupload depend on this one.
245 mainUpload.dependsOn uploadTask
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800246 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800247 }
248 }
249
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800250 // Copy instrumentation test APKs and app APKs into the dist dir
251 // For test apks, they are uploaded only if we have java test sources.
252 // For regular app apks, they are uploaded only if they have java sources.
253 project.tasks.whenTaskAdded { task ->
254 if (task.name.startsWith("packageDebug")) {
255 def testApk = task.name.contains("AndroidTest")
256 task.doLast {
257 def source = testApk ? project.android.sourceSets.androidTest
258 : project.android.sourceSets.main
Aurimas Liutikasef3b4922017-06-28 10:44:59 -0700259 if (task.hasProperty("outputDirectory") && !source.java.sourceFiles.isEmpty()) {
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800260 copy {
Aurimas Liutikasef3b4922017-06-28 10:44:59 -0700261 from(task.outputDirectory)
262 include '*.apk'
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800263 into(rootProject.ext.testApkDistOut)
264 rename { String fileName ->
Hyundo Moon9938e172017-07-26 20:51:11 +0900265 // Exclude media-compat-test-* modules from existing support library
266 // presubmit tests.
267 if (fileName.contains("media-compat-test")) {
268 fileName.replace("-debug-androidTest", "")
Yigit Boyar96a0fcd2017-08-25 18:18:28 -0700269 } else {
270 // multiple modules may have the same name so prefix the name with
271 // the module's path to ensure it is unique.
272 // e.g. palette-v7-debug-androidTest.apk becomes
273 // support-palette-v7_palette-v7-debug-androidTest.apk
274 "${project.getPath().replace(':', '-').substring(1)}_${fileName}"
Hyundo Moon9938e172017-07-26 20:51:11 +0900275 }
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800276 }
277 }
278 }
279 }
280 }
281 }
282
283 // copy host side test results to DIST
284 project.tasks.whenTaskAdded { task ->
285 if (task instanceof org.gradle.api.tasks.testing.Test) {
286 def junitReport = task.reports.junitXml
287 if (junitReport.enabled) {
288 def zipTask = project.tasks.create(name : "zipResultsOf${task.name.capitalize()}", type : Zip) {
289 destinationDir(testResultsDistDir)
Yigit Boyar278676d2017-03-10 15:29:11 -0800290 // first one is always :, drop it.
291 archiveName("${project.getPath().split(":").join("_").substring(1)}.zip")
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800292 }
293 if (project.rootProject.ext.runningInBuildServer) {
294 task.ignoreFailures = true
295 }
296 task.finalizedBy zipTask
297 task.doFirst {
298 zipTask.from(junitReport.destination)
299 }
300 }
301 }
302 }
303
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800304 project.afterEvaluate { p ->
305 // remove dependency on the test so that we still get coverage even if some tests fail
306 p.tasks.findAll { it instanceof JacocoReportTask }.each { task ->
307 def toBeRemoved = new ArrayList()
308 def dependencyList = task.taskDependencies.values
309 dependencyList.each { dep ->
310 if (dep instanceof String) {
311 def t = tasks.findByName(dep)
312 if (t instanceof DeviceProviderInstrumentTestTask) {
313 toBeRemoved.add(dep)
314 task.mustRunAfter(t)
315 }
316 }
317 }
318 toBeRemoved.each { dep ->
319 dependencyList.remove(dep)
320 }
321 }
322 }
323 }
324}
325
326def setupRelease() {
327 apply from: "${ext.supportRootFolder}/buildSrc/release.gradle"
328}
329
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800330ext.init.enableDoclavaAndJDiff = this.&enableDoclavaAndJDiff
331ext.init.setSdkInLocalPropertiesFile = this.&setSdkInLocalPropertiesFile
332ext.init.setupRepoOutAndBuildNumber = this.&setupRepoOutAndBuildNumber
333ext.init.setupRelease = this.&setupRelease
Aurimas Liutikas38e8f4d2017-06-16 08:31:03 -0700334ext.init.configureSubProjects = this.&configureSubProjects
Yigit Boyare1bbf712017-03-08 13:52:37 -0800335ext.init.configureBuildOnServer = this.&configureBuildOnServer