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