blob: b9f85d3249af169c05b7356c61f5229674eee220 [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 Liutikas9ab3b4c2017-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 Liutikas9ab3b4c2017-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 Liutikas9ab3b4c2017-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 = ''
Yigit Boyaref300662017-05-01 11:52:07 -070093 // files in artifactoryRepoOut can be safely copied into a real artifactory.
94 ext.artifactoryRepoOut = ''
Aurimas Liutikas76542da2017-06-29 17:00:29 -070095 ext.buildNumber = "0"
Yigit Boyar88c16ce2017-02-08 16:06:14 -080096 /*
97 * With the build server you are given two env variables.
98 * The OUT_DIR is a temporary directory you can use to put things during the build.
99 * The DIST_DIR is where you want to save things from the build.
100 *
101 * The build server will copy the contents of DIST_DIR to somewhere and make it available.
102 */
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800103 if (ext.runningInBuildServer) {
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800104 buildDir = new File(System.env.OUT_DIR + '/gradle/frameworks/support/build')
105 .getCanonicalFile()
106 project.ext.distDir = new File(System.env.DIST_DIR).getCanonicalFile()
107
108 // the build server does not pass the build number so we infer it from the last folder of
109 // the dist path.
110 ext.buildNumber = project.ext.distDir.getName()
Aurimas Liutikasa0150042017-04-21 09:46:12 -0700111
112 // the build server should always print out full stack traces for any failures.
113 gradle.startParameter.showStacktrace = ShowStacktrace.ALWAYS
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800114 } else {
115 buildDir = file("${ext.supportRootFolder}/../../out/host/gradle/frameworks/support/build")
116 project.ext.distDir = new File("${ext.supportRootFolder}/../../out/dist")
117 }
118 subprojects {
119 // Change buildDir first so that all plugins pick up the new value.
120 project.buildDir = new File("$project.parent.buildDir/../$project.name/build")
121 }
122 ext.supportRepoOut = new File(buildDir, 'support_repo')
Yigit Boyaref300662017-05-01 11:52:07 -0700123 ext.artifactoryRepoOut = new File(buildDir, 'artifactory_repo')
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800124 ext.testApkDistOut = ext.distDir
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800125 ext.testResultsDistDir = new File(distDir, "host-test-reports")
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800126 ext.docsDir = new File(buildDir, 'javadoc')
127}
128
Aurimas Liutikas38e8f4d2017-06-16 08:31:03 -0700129def configureBuildOnServer() {
130 def buildOnServerTask = rootProject.tasks.create("buildOnServer")
131 rootProject.tasks.whenTaskAdded { task ->
132 if ("createArchive".equals(task.name)) {
133 buildOnServerTask.dependsOn task
134 }
135 }
136
137 subprojects {
138 project.tasks.whenTaskAdded { task ->
139 if ("assembleErrorProne".equals(task.name) || "assembleAndroidTest".equals(task.name)) {
140 buildOnServerTask.dependsOn task
141 }
142 }
143 }
144}
145
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800146def configureSubProjects() {
147 // lint every library
148 def lintTask = project.tasks.create("lint")
149 subprojects {
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700150 repos.addMavenRepositories(repositories)
151
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800152 // Only modify Android projects.
Aurimas Liutikaseca7a072017-07-11 01:11:52 +0000153 if (project.name.equals('doclava')
154 || project.name.equals('jdiff')
155 || project.name.equals('support-testutils')
Hyundo Moonda9ee6b2017-07-21 14:32:12 +0900156 || project.name.equals('noto-emoji-compat')
157 || project.name.equals('support-media-compat-test-lib')) {
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800158 // disable tests and return
159 project.tasks.whenTaskAdded { task ->
160 if (task instanceof org.gradle.api.tasks.testing.Test) {
161 task.enabled = false
162 }
163 }
164 return
165 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800166
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800167 project.ext.currentSdk = gradle.ext.currentSdk
168 apply plugin: 'maven'
169
Aurimas Liutikasb938b2e2017-07-24 14:47:17 -0700170 version = LibraryVersions.SUPPORT_LIBRARY.toString();
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800171 group = 'com.android.support'
172
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800173 project.plugins.whenPluginAdded { plugin ->
174 def isAndroidLibrary = "com.android.build.gradle.LibraryPlugin"
175 .equals(plugin.class.name)
176 def isAndroidApp = "com.android.build.gradle.AppPlugin".equals(plugin.class.name)
177 def isJavaLibrary = "org.gradle.api.plugins.JavaPlugin".equals(plugin.class.name)
178
179 if (isAndroidLibrary || isAndroidApp) {
180 project.android.buildToolsVersion = rootProject.buildToolsVersion
181
182 // Enable code coverage for debug builds only if we are not running inside the IDE,
183 // since enabling coverage reports breaks the method parameter resolution in the IDE
184 // debugger.
185 project.android.buildTypes.debug.testCoverageEnabled =
Sergey Vasilinetsac8e72b2017-04-26 15:55:17 -0700186 !project.hasProperty('android.injected.invoked.from.ide')
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800187
188 // Copy the class files in a jar to be later used to generate code coverage report
189 project.android.testVariants.all { v ->
190 // check if the variant has any source files
191 // and test coverage is enabled
192 if (v.buildType.testCoverageEnabled
193 && v.sourceSets.any { !it.java.sourceFiles.isEmpty() }) {
194 def jarifyTask = project.tasks.create(
195 name: "package${v.name.capitalize()}ClassFilesForCoverageReport",
196 type: Jar) {
197 from v.testedVariant.javaCompile.destinationDir
198 exclude "**/R.class"
199 exclude "**/R\$*.class"
200 exclude "**/BuildConfig.class"
201 destinationDir file(project.distDir)
Aurimas Liutikas76542da2017-06-29 17:00:29 -0700202 archiveName "${project.name}-${v.baseName}-allclasses.jar"
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800203 }
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700204
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800205 def collectJacocoAntPackages = project.tasks.create(
206 name: "collectJacocoAntPackages",
207 type: Jar) {
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700208 inputs.files project.configurations[JacocoPlugin.ANT_CONFIGURATION_NAME]
209 from {
Aurimas Liutikas585a9342017-06-25 16:07:56 -0700210 project.configurations[JacocoPlugin.ANT_CONFIGURATION_NAME]
211 .resolvedConfiguration
212 .resolvedArtifacts.collect{ zipTree(it.getFile()) }} {
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700213 // exclude all the signatures the jar might have
214 exclude "META-INF/*.SF"
215 exclude "META-INF/*.DSA"
216 exclude "META-INF/*.RSA"
217 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800218 destinationDir file(project.distDir)
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700219 archiveName "jacocoant.jar"
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800220 }
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700221
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800222 jarifyTask.dependsOn v.getJavaCompiler()
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700223 v.assemble.dependsOn jarifyTask , collectJacocoAntPackages
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800224 }
225 }
226
227 // Enforce NewApi lint check as fatal.
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800228 project.android.lintOptions.fatal 'NewApi'
229 lintTask.dependsOn project.lint
230 }
231
232 if (isAndroidLibrary || isJavaLibrary) {
233 // Add library to the aggregate dependency report.
234 task allDeps(type: DependencyReportTask) {}
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800235
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700236 project.afterEvaluate {
237 Upload uploadTask = (Upload) project.tasks.uploadArchives;
238 uploadTask.repositories.mavenDeployer {
239
240 // Disable unique names for SNAPSHOTS so they can be updated in place.
241 setUniqueVersion(false)
Aurimas Liutikasfc243312017-05-12 14:21:13 -0700242 }
243 uploadTask.doLast {
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700244 // Remove any invalid maven-metadata.xml files that may have been
245 // created for SNAPSHOT versions that are *not* uniquely versioned.
Aurimas Liutikasfc243312017-05-12 14:21:13 -0700246 repositories.mavenDeployer.pom*.each { pom ->
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700247 if (pom.version.endsWith('-SNAPSHOT')) {
248 final File artifactDir = new File(
249 rootProject.ext.supportRepoOut,
250 pom.groupId.replace('.', '/')
251 + '/' + pom.artifactId
252 + '/' + pom.version)
253 delete fileTree(dir: artifactDir,
254 include: 'maven-metadata.xml*')
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800255 }
256 }
257 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800258
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700259 // create a release task that produces artifactory friends artifacts
260 // a.k.a. unique versions for snapshots with their maven-metadata files.
261 task artifactoryRelease(type : Upload) {
262 configuration = uploadTask.configuration
263 repositories {
264 mavenDeployer {
265 repository(url: uri("$rootProject.ext.artifactoryRepoOut"))
266 setUniqueVersion(true)
267 }
268 }
269 }
270
271 // Before the upload, make sure the repo is ready.
272 uploadTask.dependsOn rootProject.tasks.prepareRepo
273
274 artifactoryRelease.dependsOn uploadTask
275
276 // Make the mainupload depend on this one.
277 mainUpload.dependsOn uploadTask
278 mainUpload.dependsOn artifactoryRelease
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800279 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800280 }
281 }
282
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800283 // Copy instrumentation test APKs and app APKs into the dist dir
284 // For test apks, they are uploaded only if we have java test sources.
285 // For regular app apks, they are uploaded only if they have java sources.
286 project.tasks.whenTaskAdded { task ->
287 if (task.name.startsWith("packageDebug")) {
288 def testApk = task.name.contains("AndroidTest")
289 task.doLast {
290 def source = testApk ? project.android.sourceSets.androidTest
291 : project.android.sourceSets.main
Aurimas Liutikasef3b4922017-06-28 10:44:59 -0700292 if (task.hasProperty("outputDirectory") && !source.java.sourceFiles.isEmpty()) {
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800293 copy {
Aurimas Liutikasef3b4922017-06-28 10:44:59 -0700294 from(task.outputDirectory)
295 include '*.apk'
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800296 into(rootProject.ext.testApkDistOut)
297 rename { String fileName ->
298 // multiple modules may have the same name so prefix the name with
299 // the module's path to ensure it is unique.
300 // e.g. palette-v7-debug-androidTest.apk becomes
301 // support-palette-v7_palette-v7-debug-androidTest.apk
302 "${project.getPath().replace(':', '-').substring(1)}_${fileName}"
303 }
304 }
305 }
306 }
307 }
308 }
309
310 // copy host side test results to DIST
311 project.tasks.whenTaskAdded { task ->
312 if (task instanceof org.gradle.api.tasks.testing.Test) {
313 def junitReport = task.reports.junitXml
314 if (junitReport.enabled) {
315 def zipTask = project.tasks.create(name : "zipResultsOf${task.name.capitalize()}", type : Zip) {
316 destinationDir(testResultsDistDir)
Yigit Boyar278676d2017-03-10 15:29:11 -0800317 // first one is always :, drop it.
318 archiveName("${project.getPath().split(":").join("_").substring(1)}.zip")
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800319 }
320 if (project.rootProject.ext.runningInBuildServer) {
321 task.ignoreFailures = true
322 }
323 task.finalizedBy zipTask
324 task.doFirst {
325 zipTask.from(junitReport.destination)
326 }
327 }
328 }
329 }
330
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800331 project.afterEvaluate { p ->
332 // remove dependency on the test so that we still get coverage even if some tests fail
333 p.tasks.findAll { it instanceof JacocoReportTask }.each { task ->
334 def toBeRemoved = new ArrayList()
335 def dependencyList = task.taskDependencies.values
336 dependencyList.each { dep ->
337 if (dep instanceof String) {
338 def t = tasks.findByName(dep)
339 if (t instanceof DeviceProviderInstrumentTestTask) {
340 toBeRemoved.add(dep)
341 task.mustRunAfter(t)
342 }
343 }
344 }
345 toBeRemoved.each { dep ->
346 dependencyList.remove(dep)
347 }
348 }
349 }
350 }
351}
352
353def setupRelease() {
354 apply from: "${ext.supportRootFolder}/buildSrc/release.gradle"
355}
356
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800357ext.init.enableDoclavaAndJDiff = this.&enableDoclavaAndJDiff
358ext.init.setSdkInLocalPropertiesFile = this.&setSdkInLocalPropertiesFile
359ext.init.setupRepoOutAndBuildNumber = this.&setupRepoOutAndBuildNumber
360ext.init.setupRelease = this.&setupRelease
Aurimas Liutikas38e8f4d2017-06-16 08:31:03 -0700361ext.init.configureSubProjects = this.&configureSubProjects
Yigit Boyare1bbf712017-03-08 13:52:37 -0800362ext.init.configureBuildOnServer = this.&configureBuildOnServer