blob: eb3865a1e48d529fa1769380a21249f138c2c0bb [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
20import com.google.common.base.Charsets
21import com.google.common.io.Files
22import org.gradle.internal.os.OperatingSystem
23
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
31ext.init.checkoutRoot = "${supportRoot}/../.."
32ext.init.prebuiltsRoot = "${init.checkoutRoot}/prebuilts"
33ext.init.prebuiltsRootUri = "file://${init.prebuiltsRoot}"
34ext.init.enablePublicRepos = System.getenv("ALLOW_PUBLIC_REPOS") != null
Yigit Boyar7bfacb72017-03-02 14:27:41 -080035ext.runningInBuildServer = System.env.DIST_DIR != null && System.env.OUT_DIR != null
Yigit Boyar88c16ce2017-02-08 16:06:14 -080036
37ext.repoNames = ["${init.prebuiltsRoot}/gradle-plugin",
38 "${init.prebuiltsRoot}/tools/common/m2/repository",
39 "${init.prebuiltsRoot}/tools/common/m2/internal",
40 "${init.prebuiltsRoot}/maven_repo/android"]
41
42apply from: "${supportRoot}/buildSrc/dependencies.gradle"
43
44
45def loadDefaultVersions() {
46 apply from: "${supportRootFolder}/buildSrc/versions.gradle"
47}
48
49def addMavenRepositories(RepositoryHandler handler) {
50 repoNames.each { repo ->
51 handler.maven {
52 url repo
53 }
54 if (ext.init.enablePublicRepos) {
55 handler.mavenCentral()
56 handler.jcenter()
57 }
58 }
59}
60
61def enableDoclavaAndJDiff(p) {
62 p.configurations {
63 doclava
64 jdiff
65 }
66
67 p.dependencies {
68 doclava project(':doclava')
69 jdiff project(':jdiff')
70 jdiff libs.xml_parser_apis
71 jdiff libs.xerces_impl
72 }
73 apply from: "${ext.supportRootFolder}/buildSrc/diff_and_docs.gradle"
74}
75
76def setSdkInLocalPropertiesFile() {
77 final String platform = OperatingSystem.current().isMacOsX() ? 'darwin' : 'linux'
78 System.setProperty('android.dir', "${supportRootFolder}/../../")
79 final String fullSdkPath = "${init.prebuiltsRoot}/fullsdk-${platform}"
80 if (file(fullSdkPath).exists()) {
81 gradle.ext.currentSdk = 26
82 ext.buildToolsVersion = '26.0.0'
83 project.ext.androidJar = files("${fullSdkPath}/platforms/android-${gradle.ext.currentSdk}" +
84 "/android.jar")
85 System.setProperty('android.home', "${init.prebuiltsRoot}/fullsdk-${platform}")
86 File props = file("local.properties")
87 props.write "sdk.dir=${fullSdkPath}"
88 } else {
89 gradle.ext.currentSdk = 'current'
90 ext.buildToolsVersion = '24.0.1'
91 project.ext.androidJar = files("${init.prebuiltsRoot}/sdk/current/android.jar")
92 File props = file("local.properties")
93 props.write "android.dir=../../"
94 }
95}
96
97def setupRepoOutAndBuildNumber() {
98 ext.supportRepoOut = ''
99 ext.buildNumber = Integer.toString(ext.extraVersion)
100
101 /*
102 * With the build server you are given two env variables.
103 * The OUT_DIR is a temporary directory you can use to put things during the build.
104 * The DIST_DIR is where you want to save things from the build.
105 *
106 * The build server will copy the contents of DIST_DIR to somewhere and make it available.
107 */
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800108 if (ext.runningInBuildServer) {
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800109 buildDir = new File(System.env.OUT_DIR + '/gradle/frameworks/support/build')
110 .getCanonicalFile()
111 project.ext.distDir = new File(System.env.DIST_DIR).getCanonicalFile()
112
113 // the build server does not pass the build number so we infer it from the last folder of
114 // the dist path.
115 ext.buildNumber = project.ext.distDir.getName()
116 } else {
117 buildDir = file("${ext.supportRootFolder}/../../out/host/gradle/frameworks/support/build")
118 project.ext.distDir = new File("${ext.supportRootFolder}/../../out/dist")
119 }
120 subprojects {
121 // Change buildDir first so that all plugins pick up the new value.
122 project.buildDir = new File("$project.parent.buildDir/../$project.name/build")
123 }
124 ext.supportRepoOut = new File(buildDir, 'support_repo')
125 ext.testApkDistOut = ext.distDir
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800126 ext.testResultsDistDir = new File(distDir, "host-test-reports")
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800127 ext.docsDir = new File(buildDir, 'javadoc')
128}
129
130def configureSubProjects() {
131 // lint every library
132 def lintTask = project.tasks.create("lint")
133 subprojects {
134 // Only modify Android projects.
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800135 if (project.name.equals('doclava') || project.name.equals('jdiff')) {
136 // disable tests and return
137 project.tasks.whenTaskAdded { task ->
138 if (task instanceof org.gradle.api.tasks.testing.Test) {
139 task.enabled = false
140 }
141 }
142 return
143 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800144
145 // Current SDK is set in studioCompat.gradle.
146 project.ext.currentSdk = gradle.ext.currentSdk
147 apply plugin: 'maven'
148
149 version = rootProject.ext.supportVersion
150 group = 'com.android.support'
151
152 init.addMavenRepositories(repositories)
153 project.plugins.whenPluginAdded { plugin ->
154 def isAndroidLibrary = "com.android.build.gradle.LibraryPlugin"
155 .equals(plugin.class.name)
156 def isAndroidApp = "com.android.build.gradle.AppPlugin".equals(plugin.class.name)
157 def isJavaLibrary = "org.gradle.api.plugins.JavaPlugin".equals(plugin.class.name)
158
159 if (isAndroidLibrary || isAndroidApp) {
160 project.android.buildToolsVersion = rootProject.buildToolsVersion
161
162 // Enable code coverage for debug builds only if we are not running inside the IDE,
163 // since enabling coverage reports breaks the method parameter resolution in the IDE
164 // debugger.
165 project.android.buildTypes.debug.testCoverageEnabled =
166 !hasProperty('android.injected.invoked.from.ide')
167
168 // Copy the class files in a jar to be later used to generate code coverage report
169 project.android.testVariants.all { v ->
170 // check if the variant has any source files
171 // and test coverage is enabled
172 if (v.buildType.testCoverageEnabled
173 && v.sourceSets.any { !it.java.sourceFiles.isEmpty() }) {
174 def jarifyTask = project.tasks.create(
175 name: "package${v.name.capitalize()}ClassFilesForCoverageReport",
176 type: Jar) {
177 from v.testedVariant.javaCompile.destinationDir
178 exclude "**/R.class"
179 exclude "**/R\$*.class"
180 exclude "**/BuildConfig.class"
181 destinationDir file(project.distDir)
182 archiveName "${project.archivesBaseName}-${v.baseName}-allclasses.jar"
183 }
184 def jacocoAntConfig =
185 project.configurations[JacocoPlugin.ANT_CONFIGURATION_NAME]
186 def jacocoAntArtifacts = jacocoAntConfig.resolvedConfiguration
187 .resolvedArtifacts
188 def version = jacocoAntArtifacts.find { "org.jacoco.ant".equals(it.name) }
189 .moduleVersion.id.version
190 def collectJacocoAntPackages = project.tasks.create(
191 name: "collectJacocoAntPackages",
192 type: Jar) {
193 from(jacocoAntArtifacts.collect { zipTree(it.getFile()) }) {
194 // exclude all the signatures the jar might have
195 exclude "META-INF/*.SF"
196 exclude "META-INF/*.DSA"
197 exclude "META-INF/*.RSA"
198 }
199 destinationDir file(project.distDir)
200 archiveName "jacocoant-" + version + ".jar"
201 }
202 jarifyTask.dependsOn v.getJavaCompiler()
203 v.assemble.dependsOn jarifyTask, collectJacocoAntPackages
204 }
205 }
206
207 // Enforce NewApi lint check as fatal.
208 project.android.lintOptions.check 'NewApi'
209 project.android.lintOptions.fatal 'NewApi'
210 lintTask.dependsOn project.lint
211 }
212
213 if (isAndroidLibrary || isJavaLibrary) {
214 // Add library to the aggregate dependency report.
215 task allDeps(type: DependencyReportTask) {}
216
217 // Create release and separate zip task for library.
218 task release(type: Upload) {
219 configuration = configurations.archives
220 repositories {
221 mavenDeployer {
222 repository(url: uri("$rootProject.ext.supportRepoOut"))
223
224 // Disable unique names for SNAPSHOTS so they can be updated in place.
225 setUniqueVersion(false)
226 doLast {
227 // Remove any invalid maven-metadata.xml files that may have been
228 // created for SNAPSHOT versions that are *not* uniquely versioned.
229 pom*.each { pom ->
230 if (pom.version.endsWith('-SNAPSHOT')) {
231 final File artifactDir = new File(
232 rootProject.ext.supportRepoOut,
233 pom.groupId.replace('.', '/')
234 + '/' + pom.artifactId
235 + '/' + pom.version)
236 delete fileTree(dir: artifactDir,
237 include: 'maven-metadata.xml*')
238 }
239 }
240 }
241 }
242 }
243 }
244
245 def deployer = release.repositories.mavenDeployer
246 deployer.pom*.whenConfigured { pom ->
247 pom.dependencies.findAll { dep ->
248 dep.groupId == 'com.android.support' &&
249 dep.artifactId != 'support-annotations'
250 }*.type = 'aar'
251 }
252
253 ext.versionDir = {
254 def groupDir = new File(rootProject.ext.supportRepoOut,
255 project.group.replace('.', '/'))
256 def artifactDir = new File(groupDir, archivesBaseName)
257 return new File(artifactDir, version)
258 }
259
260 task generateSourceProps(dependsOn: createRepository)
261 generateSourceProps.doLast({
262 def content = "Maven.GroupId=$deployer.pom.groupId\n" +
263 "Maven.ArtifactId=$deployer.pom.artifactId\n" +
264 "Maven.Version=$deployer.pom.version\n" +
265 "Extra.VendorDisplay=Android\n" +
266 "Extra.VendorId=android\n" +
267 "Pkg.Desc=$project.name\n" +
268 "Pkg.Revision=1\n" +
269 "Maven.Dependencies=" +
270 String.join(",", project.configurations.compile.allDependencies
271 .collect {
272 def p = parent.findProject(it.name)
273 return p ? "$p.group:$p.archivesBaseName:$p.version" : null
274 }.grep()) +
275 "\n"
276 Files.write(content, new File(versionDir(), 'source.properties'),
277 Charsets.UTF_8)
278 })
279
280 task createSeparateZip(type: Zip, dependsOn: generateSourceProps) {
281 into archivesBaseName
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800282 destinationDir rootProject.ext.distDir
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800283 baseName = project.group
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800284 version = rootProject.ext.buildNumber
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800285 }
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800286 rootProject.createArchive.dependsOn createSeparateZip
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800287
288 // Before the upload, make sure the repo is ready.
289 release.dependsOn rootProject.tasks.prepareRepo
290
291 // Make the mainupload depend on this one.
292 mainUpload.dependsOn release
293 }
294 }
295
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800296 // Copy instrumentation test APKs and app APKs into the dist dir
297 // For test apks, they are uploaded only if we have java test sources.
298 // For regular app apks, they are uploaded only if they have java sources.
299 project.tasks.whenTaskAdded { task ->
300 if (task.name.startsWith("packageDebug")) {
301 def testApk = task.name.contains("AndroidTest")
302 task.doLast {
303 def source = testApk ? project.android.sourceSets.androidTest
304 : project.android.sourceSets.main
305 if (task.hasProperty("outputFile") && !source.java.sourceFiles.isEmpty()) {
306 copy {
307 from(task.outputFile)
308 into(rootProject.ext.testApkDistOut)
309 rename { String fileName ->
310 // multiple modules may have the same name so prefix the name with
311 // the module's path to ensure it is unique.
312 // e.g. palette-v7-debug-androidTest.apk becomes
313 // support-palette-v7_palette-v7-debug-androidTest.apk
314 "${project.getPath().replace(':', '-').substring(1)}_${fileName}"
315 }
316 }
317 }
318 }
319 }
320 }
321
322 // copy host side test results to DIST
323 project.tasks.whenTaskAdded { task ->
324 if (task instanceof org.gradle.api.tasks.testing.Test) {
325 def junitReport = task.reports.junitXml
326 if (junitReport.enabled) {
327 def zipTask = project.tasks.create(name : "zipResultsOf${task.name.capitalize()}", type : Zip) {
328 destinationDir(testResultsDistDir)
329 archiveName("${project.name}.zip")
330 }
331 if (project.rootProject.ext.runningInBuildServer) {
332 task.ignoreFailures = true
333 }
334 task.finalizedBy zipTask
335 task.doFirst {
336 zipTask.from(junitReport.destination)
337 }
338 }
339 }
340 }
341
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800342 project.afterEvaluate {
343 // The archivesBaseName isn't available initially, so set it now
344 def createZipTask = project.tasks.findByName("createSeparateZip")
345 if (createZipTask != null) {
346 createZipTask.appendix = archivesBaseName
347 createZipTask.from versionDir()
348 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800349 }
350
351 project.afterEvaluate { p ->
352 // remove dependency on the test so that we still get coverage even if some tests fail
353 p.tasks.findAll { it instanceof JacocoReportTask }.each { task ->
354 def toBeRemoved = new ArrayList()
355 def dependencyList = task.taskDependencies.values
356 dependencyList.each { dep ->
357 if (dep instanceof String) {
358 def t = tasks.findByName(dep)
359 if (t instanceof DeviceProviderInstrumentTestTask) {
360 toBeRemoved.add(dep)
361 task.mustRunAfter(t)
362 }
363 }
364 }
365 toBeRemoved.each { dep ->
366 dependencyList.remove(dep)
367 }
368 }
369 }
370 }
371}
372
373def setupRelease() {
374 apply from: "${ext.supportRootFolder}/buildSrc/release.gradle"
375}
376
377ext.init.addMavenRepositories = this.&addMavenRepositories
378ext.init.enableDoclavaAndJDiff = this.&enableDoclavaAndJDiff
379ext.init.setSdkInLocalPropertiesFile = this.&setSdkInLocalPropertiesFile
380ext.init.setupRepoOutAndBuildNumber = this.&setupRepoOutAndBuildNumber
381ext.init.setupRelease = this.&setupRelease
382ext.init.loadDefaultVersions = this.&loadDefaultVersions
383ext.init.configureSubProjects = this.&configureSubProjects