blob: 4e66dc8734c4d3dd182c0b7e621d1f7be8f89770 [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
17import com.android.build.gradle.internal.coverage.JacocoPlugin
18import com.android.build.gradle.internal.coverage.JacocoReportTask
19import com.android.build.gradle.internal.tasks.DeviceProviderInstrumentTestTask
Aurimas Liutikasa0150042017-04-21 09:46:12 -070020import org.gradle.api.logging.configuration.ShowStacktrace
Yigit Boyar88c16ce2017-02-08 16:06:14 -080021
22def supportRoot = ext.supportRootFolder
23if (supportRoot == null) {
24 throw new RuntimeException("variable supportRootFolder is not set. you must set it before" +
25 " including this script")
26}
27def init = new Properties()
28ext.init = init
Alan Viverette1b862372017-03-22 11:32:28 -040029ext.init.debugKeystore = file("${supportRoot}/development/keystore/debug.keystore")
Yigit Boyar88c16ce2017-02-08 16:06:14 -080030
Aurimas Liutikas9ab3b4c2017-04-19 09:33:27 -070031ext.runningInBuildServer = System.env.DIST_DIR != null && System.env.OUT_DIR != null
Yigit Boyar88c16ce2017-02-08 16:06:14 -080032
33apply from: "${supportRoot}/buildSrc/dependencies.gradle"
Alan Viverette5b3946f2017-04-27 09:59:22 -040034ext.docs = [:]
35ext.docs.offline = rootProject.getProperties().containsKey("offlineDocs")
36ext.docs.dac = [
37 libraryroot: "android/support",
38 dataname: "SUPPORT_DATA"
39]
Yigit Boyar88c16ce2017-02-08 16:06:14 -080040
41def loadDefaultVersions() {
42 apply from: "${supportRootFolder}/buildSrc/versions.gradle"
43}
44
Yigit Boyar88c16ce2017-02-08 16:06:14 -080045def enableDoclavaAndJDiff(p) {
46 p.configurations {
47 doclava
48 jdiff
49 }
50
51 p.dependencies {
52 doclava project(':doclava')
53 jdiff project(':jdiff')
54 jdiff libs.xml_parser_apis
55 jdiff libs.xerces_impl
56 }
57 apply from: "${ext.supportRootFolder}/buildSrc/diff_and_docs.gradle"
58}
59
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070060def getFullSdkPath() {
Aurimas Liutikasb06771d2017-04-20 09:46:49 -070061 final String osName = System.getProperty("os.name").toLowerCase();
62 final boolean isMacOsX =
63 osName.contains("mac os x") || osName.contains("darwin") || osName.contains("osx");
64 final String platform = isMacOsX ? 'darwin' : 'linux'
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070065 return "${repos.prebuiltsRoot}/fullsdk-${platform}"
66}
67
68def setSdkInLocalPropertiesFile() {
Aurimas Liutikasd2d9bda2017-03-03 16:06:39 -080069 ext.buildToolsVersion = '26.0.0'
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070070 final String fullSdkPath = getFullSdkPath();
Yigit Boyar88c16ce2017-02-08 16:06:14 -080071 if (file(fullSdkPath).exists()) {
72 gradle.ext.currentSdk = 26
Alan Viveretteeb9f3322017-03-09 16:29:57 -050073 project.ext.androidJar =
74 files("${fullSdkPath}/platforms/android-${gradle.currentSdk}/android.jar")
75 project.ext.androidSrcJar =
76 file("${fullSdkPath}/platforms/android-${gradle.currentSdk}/android-stubs-src.jar")
Alan Viverette810a1482017-03-20 12:43:43 -040077 project.ext.androidApiTxt = null
Yigit Boyar88c16ce2017-02-08 16:06:14 -080078 File props = file("local.properties")
79 props.write "sdk.dir=${fullSdkPath}"
Aurimas Liutikas3c666002017-03-08 19:30:28 -080080 ext.usingFullSdk = true
Yigit Boyar88c16ce2017-02-08 16:06:14 -080081 } else {
82 gradle.ext.currentSdk = 'current'
Aurimas Liutikas9ab3b4c2017-04-19 09:33:27 -070083 project.ext.androidJar = files("${repos.prebuiltsRoot}/sdk/current/android.jar")
Alan Viverette810a1482017-03-20 12:43:43 -040084 project.ext.androidSrcJar = null
Aurimas Liutikas9ab3b4c2017-04-19 09:33:27 -070085 project.ext.androidApiTxt = file("${repos.prebuiltsRoot}/sdk/api/26.txt")
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070086 System.setProperty('android.dir', "${supportRootFolder}/../../")
Yigit Boyar88c16ce2017-02-08 16:06:14 -080087 File props = file("local.properties")
88 props.write "android.dir=../../"
Aurimas Liutikas3c666002017-03-08 19:30:28 -080089 ext.usingFullSdk = false
Yigit Boyar88c16ce2017-02-08 16:06:14 -080090 }
91}
92
93def setupRepoOutAndBuildNumber() {
94 ext.supportRepoOut = ''
95 ext.buildNumber = Integer.toString(ext.extraVersion)
96
97 /*
98 * With the build server you are given two env variables.
99 * The OUT_DIR is a temporary directory you can use to put things during the build.
100 * The DIST_DIR is where you want to save things from the build.
101 *
102 * The build server will copy the contents of DIST_DIR to somewhere and make it available.
103 */
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800104 if (ext.runningInBuildServer) {
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800105 buildDir = new File(System.env.OUT_DIR + '/gradle/frameworks/support/build')
106 .getCanonicalFile()
107 project.ext.distDir = new File(System.env.DIST_DIR).getCanonicalFile()
108
109 // the build server does not pass the build number so we infer it from the last folder of
110 // the dist path.
111 ext.buildNumber = project.ext.distDir.getName()
Aurimas Liutikasa0150042017-04-21 09:46:12 -0700112
113 // the build server should always print out full stack traces for any failures.
114 gradle.startParameter.showStacktrace = ShowStacktrace.ALWAYS
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800115 } else {
116 buildDir = file("${ext.supportRootFolder}/../../out/host/gradle/frameworks/support/build")
117 project.ext.distDir = new File("${ext.supportRootFolder}/../../out/dist")
118 }
119 subprojects {
120 // Change buildDir first so that all plugins pick up the new value.
121 project.buildDir = new File("$project.parent.buildDir/../$project.name/build")
122 }
123 ext.supportRepoOut = new File(buildDir, 'support_repo')
124 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 Liutikasabb471f2017-05-09 16:25:43 -0700150 repos.addMavenRepositories(repositories)
151
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800152 // Only modify Android projects.
Aurimas Liutikasabb471f2017-05-09 16:25:43 -0700153 if (project.name.equals('doclava')
154 || project.name.equals('jdiff')
155 || project.name.equals('support-testutils')
156 || project.name.equals('noto-emoji-compat')) {
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800157 // disable tests and return
158 project.tasks.whenTaskAdded { task ->
159 if (task instanceof org.gradle.api.tasks.testing.Test) {
160 task.enabled = false
161 }
162 }
163 return
164 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800165
166 // Current SDK is set in studioCompat.gradle.
167 project.ext.currentSdk = gradle.ext.currentSdk
168 apply plugin: 'maven'
169
170 version = rootProject.ext.supportVersion
171 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)
202 archiveName "${project.archivesBaseName}-${v.baseName}-allclasses.jar"
203 }
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) {}
235
Aurimas Liutikasabb471f2017-05-09 16:25:43 -0700236 project.afterEvaluate {
237 Upload uploadTask = (Upload) project.tasks.uploadArchives;
238 uploadTask.repositories.mavenDeployer {
239 // Disable unique names for SNAPSHOTS so they can be updated in place.
240 setUniqueVersion(false)
Aurimas Liutikasfc243312017-05-12 14:21:13 -0700241 }
242 uploadTask.doLast {
Aurimas Liutikasabb471f2017-05-09 16:25:43 -0700243 // Remove any invalid maven-metadata.xml files that may have been
244 // created for SNAPSHOT versions that are *not* uniquely versioned.
Aurimas Liutikasfc243312017-05-12 14:21:13 -0700245 repositories.mavenDeployer.pom*.each { pom ->
Aurimas Liutikasabb471f2017-05-09 16:25:43 -0700246 if (pom.version.endsWith('-SNAPSHOT')) {
247 final File artifactDir = new File(
248 rootProject.ext.supportRepoOut,
249 pom.groupId.replace('.', '/')
250 + '/' + pom.artifactId
251 + '/' + pom.version)
252 delete fileTree(dir: artifactDir,
253 include: 'maven-metadata.xml*')
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800254 }
255 }
256 }
Aurimas Liutikasabb471f2017-05-09 16:25:43 -0700257
258 uploadTask.repositories.mavenDeployer.pom*.whenConfigured { pom ->
259 pom.dependencies.findAll { dep ->
260 dep.groupId == 'com.android.support' &&
261 dep.artifactId != 'support-annotations'
262 }*.type = 'aar'
263 }
264
265 // Before the upload, make sure the repo is ready.
266 uploadTask.dependsOn rootProject.tasks.prepareRepo
267
268 // Make the mainupload depend on this one.
269 mainUpload.dependsOn uploadTask
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800270 }
271
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800272 }
273 }
274
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800275 // Copy instrumentation test APKs and app APKs into the dist dir
276 // For test apks, they are uploaded only if we have java test sources.
277 // For regular app apks, they are uploaded only if they have java sources.
278 project.tasks.whenTaskAdded { task ->
279 if (task.name.startsWith("packageDebug")) {
280 def testApk = task.name.contains("AndroidTest")
281 task.doLast {
282 def source = testApk ? project.android.sourceSets.androidTest
283 : project.android.sourceSets.main
284 if (task.hasProperty("outputFile") && !source.java.sourceFiles.isEmpty()) {
285 copy {
286 from(task.outputFile)
287 into(rootProject.ext.testApkDistOut)
288 rename { String fileName ->
289 // multiple modules may have the same name so prefix the name with
290 // the module's path to ensure it is unique.
291 // e.g. palette-v7-debug-androidTest.apk becomes
292 // support-palette-v7_palette-v7-debug-androidTest.apk
293 "${project.getPath().replace(':', '-').substring(1)}_${fileName}"
294 }
295 }
296 }
297 }
298 }
299 }
300
301 // copy host side test results to DIST
302 project.tasks.whenTaskAdded { task ->
303 if (task instanceof org.gradle.api.tasks.testing.Test) {
304 def junitReport = task.reports.junitXml
305 if (junitReport.enabled) {
306 def zipTask = project.tasks.create(name : "zipResultsOf${task.name.capitalize()}", type : Zip) {
307 destinationDir(testResultsDistDir)
Yigit Boyar278676d2017-03-10 15:29:11 -0800308 // first one is always :, drop it.
309 archiveName("${project.getPath().split(":").join("_").substring(1)}.zip")
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800310 }
311 if (project.rootProject.ext.runningInBuildServer) {
312 task.ignoreFailures = true
313 }
314 task.finalizedBy zipTask
315 task.doFirst {
316 zipTask.from(junitReport.destination)
317 }
318 }
319 }
320 }
321
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800322 project.afterEvaluate {
323 // The archivesBaseName isn't available initially, so set it now
324 def createZipTask = project.tasks.findByName("createSeparateZip")
325 if (createZipTask != null) {
326 createZipTask.appendix = archivesBaseName
327 createZipTask.from versionDir()
328 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800329 }
330
331 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
361ext.init.loadDefaultVersions = this.&loadDefaultVersions
Aurimas Liutikas38e8f4d2017-06-16 08:31:03 -0700362ext.init.configureSubProjects = this.&configureSubProjects
363ext.init.configureBuildOnServer = this.&configureBuildOnServer