blob: 68cc5a0e47ed1059518a83489db97c12a6a7756b [file] [log] [blame]
Alan Viverette9290dcd2016-12-15 17:29:58 -05001import android.support.checkapi.ApiXmlConversionTask
Alan Viverette62ff5d92016-12-07 09:51:16 -05002import android.support.checkapi.CheckApiTask
3import android.support.checkapi.UpdateApiTask
Alan Viverette9562a3b2016-07-01 13:26:39 -04004import android.support.doclava.DoclavaMultilineJavadocOptionFileOption
Alan Viverette62ff5d92016-12-07 09:51:16 -05005import android.support.doclava.DoclavaTask
Alan Viverette9290dcd2016-12-15 17:29:58 -05006import android.support.jdiff.JDiffTask
Alan Viverette62ff5d92016-12-07 09:51:16 -05007
8import com.android.build.gradle.internal.coverage.JacocoPlugin
Yigit Boyarea5d9b22016-03-08 13:25:26 -08009import com.android.build.gradle.internal.coverage.JacocoReportTask
10import com.android.build.gradle.internal.tasks.DeviceProviderInstrumentTestTask
Aurimas Liutikasd4d9cde2016-11-23 12:12:50 -080011import org.gradle.internal.os.OperatingSystem
Yigit Boyarea5d9b22016-03-08 13:25:26 -080012
Alan Viverette62ff5d92016-12-07 09:51:16 -050013import com.google.common.base.Charsets
14import com.google.common.hash.HashCode
15import com.google.common.hash.HashFunction
16import com.google.common.hash.Hashing
17import com.google.common.io.Files
18
19import groovy.io.FileType
Alan Viverettecc5197e2016-06-13 12:45:07 -040020
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -080021buildscript {
22 repositories {
23 maven { url '../../prebuilts/gradle-plugin' }
24 maven { url '../../prebuilts/tools/common/m2/repository' }
25 maven { url '../../prebuilts/tools/common/m2/internal' }
Yigit Boyarc9750a12016-01-06 17:28:55 -080026 maven { url "../../prebuilts/maven_repo/android" }
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -080027 }
28 dependencies {
Aurimas Liutikas5c6933b2016-09-20 09:55:40 -070029 // Keep gradle plugin version in sync with ub_supportlib-master manifest.
Aurimas Liutikas9135bd92016-12-16 10:53:43 -080030 classpath 'com.android.tools.build:gradle:2.2.4'
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -080031 }
32}
33
Alan Viverettecc5197e2016-06-13 12:45:07 -040034repositories {
35 maven { url '../../prebuilts/tools/common/m2/repository' }
36}
37
38configurations {
39 doclava
Alan Viverette9290dcd2016-12-15 17:29:58 -050040 jdiff
Alan Viverettecc5197e2016-06-13 12:45:07 -040041}
42
43dependencies {
44 doclava project(':doclava')
Alan Viverette9290dcd2016-12-15 17:29:58 -050045 jdiff project(':jdiff')
46 jdiff 'xerces:xmlParserAPIs:2.6.2'
47 jdiff 'xerces:xercesImpl:2.6.2'
Alan Viverettecc5197e2016-06-13 12:45:07 -040048}
49
Alan Viverette9290dcd2016-12-15 17:29:58 -050050// Version code components.
51ext.supportVersion = "26.0.0-SNAPSHOT"
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -080052
Alan Viverette9290dcd2016-12-15 17:29:58 -050053// This number gets incremented for each public release.
54ext.extraVersion = 41
55
56// Dependency versions.
Aurimas Liutikasdfe75782016-08-03 14:27:20 -070057ext.testRunnerVersion = '0.6-alpha'
58ext.espressoVersion = '2.3-alpha'
59
Alan Viverettecc5197e2016-06-13 12:45:07 -040060// Enforce the use of prebuilt dependencies in all sub-projects. This is
61// required for the doclava dependency.
62ext.usePrebuilts = "true"
63
Aurimas Liutikasd4d9cde2016-11-23 12:12:50 -080064final String platform = OperatingSystem.current().isMacOsX() ? 'darwin' : 'linux'
65System.setProperty('android.dir', "${rootDir}/../../")
66final String fullSdkPath = "${rootDir}/../../prebuilts/fullsdk-${platform}"
67if (file(fullSdkPath).exists()) {
68 gradle.ext.currentSdk = 26
69 ext.buildToolsVersion = '26.0.0'
70 project.ext.androidJar = files("${fullSdkPath}/platforms/android-${gradle.ext.currentSdk}/android.jar")
71 System.setProperty('android.home', "${rootDir}/../../prebuilts/fullsdk-${platform}")
72 File props = file("local.properties")
73 props.write "sdk.dir=${fullSdkPath}"
74} else {
75 gradle.ext.currentSdk = 'current'
76 ext.buildToolsVersion = '24.0.1'
77 project.ext.androidJar = files("${project.rootDir}/../../prebuilts/sdk/current/android.jar")
78 File props = file("local.properties")
79 props.write "android.dir=../../"
80}
81
Alan Viverette9290dcd2016-12-15 17:29:58 -050082ext.supportRepoOut = ''
83ext.buildNumber = Integer.toString(ext.extraVersion)
84
Xavier Ducrohet020e4322014-03-18 16:41:30 -070085/*
86 * With the build server you are given two env variables.
87 * The OUT_DIR is a temporary directory you can use to put things during the build.
88 * The DIST_DIR is where you want to save things from the build.
89 *
90 * The build server will copy the contents of DIST_DIR to somewhere and make it available.
91 */
92if (System.env.DIST_DIR != null && System.env.OUT_DIR != null) {
Xavier Ducrohet4e04b7a2014-10-17 18:02:33 -070093 buildDir = new File(System.env.OUT_DIR + '/gradle/frameworks/support/build').getCanonicalFile()
94 project.ext.distDir = new File(System.env.DIST_DIR).getCanonicalFile()
Xavier Ducrohetfa385272014-11-14 13:12:09 -080095
96 // the build server does not pass the build number so we infer it from the last folder of the dist path.
97 ext.buildNumber = project.ext.distDir.getName()
Xavier Ducrohet020e4322014-03-18 16:41:30 -070098} else {
Alan Viverette7b59d3a2016-06-13 12:52:20 -040099 buildDir = file("${project.rootDir}/../../out/host/gradle/frameworks/support/build")
100 project.ext.distDir = file("${project.rootDir}/../../out/dist")
Xavier Ducrohet020e4322014-03-18 16:41:30 -0700101}
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -0800102
Alan Viverette9b5fe932016-07-22 10:28:31 -0400103subprojects {
104 // Change buildDir first so that all plugins pick up the new value.
105 project.buildDir = project.file("$project.parent.buildDir/../$project.name/build")
106}
107
Alan Viverettecc5197e2016-06-13 12:45:07 -0400108ext.docsDir = new File(buildDir, 'javadoc')
Xavier Ducrohet020e4322014-03-18 16:41:30 -0700109ext.supportRepoOut = new File(buildDir, 'support_repo')
Yigit Boyarf18d97572015-12-01 13:45:28 -0800110ext.testApkDistOut = ext.distDir
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -0800111
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -0700112// Main task called by the build server.
Alan Viverette9290dcd2016-12-15 17:29:58 -0500113task(createArchive)
Xavier Ducrohet020e4322014-03-18 16:41:30 -0700114
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -0700115// upload anchor for subprojects to upload their artifacts
116// to the local repo.
Alan Viverette9290dcd2016-12-15 17:29:58 -0500117task(mainUpload)
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -0700118
119// repository creation task
120task createRepository(type: Zip, dependsOn: mainUpload) {
Xavier Ducrohet020e4322014-03-18 16:41:30 -0700121 from project.ext.supportRepoOut
122 destinationDir project.ext.distDir
Xavier Ducrohet9dc44802014-03-20 14:15:16 -0700123 into 'm2repository'
Xavier Ducrohetfa385272014-11-14 13:12:09 -0800124 baseName = String.format("sdk-repo-linux-m2repository-%s", project.ext.buildNumber)
Xavier Ducrohet020e4322014-03-18 16:41:30 -0700125}
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -0700126createArchive.dependsOn createRepository
Xavier Ducrohet020e4322014-03-18 16:41:30 -0700127
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -0700128// prepare repository with older versions
Xavier Ducrohet64fe2322014-06-16 17:59:34 -0700129task unzipRepo(type: Copy) {
Alan Viverette7b59d3a2016-06-13 12:52:20 -0400130 from "${project.rootDir}/../../prebuilts/maven_repo/android"
Xavier Ducrohet855a9222014-01-02 19:00:43 -0800131 into project.ext.supportRepoOut
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -0800132}
133
Xavier Ducrohet64fe2322014-06-16 17:59:34 -0700134unzipRepo.doFirst {
Xavier Ducrohet020e4322014-03-18 16:41:30 -0700135 project.ext.supportRepoOut.deleteDir()
136 project.ext.supportRepoOut.mkdirs()
137}
138
Xavier Ducrohet64fe2322014-06-16 17:59:34 -0700139// anchor for prepare repo. This is post unzip + sourceProp.
Alan Viverette9290dcd2016-12-15 17:29:58 -0500140task(prepareRepo)
Xavier Ducrohet64fe2322014-06-16 17:59:34 -0700141
Alan Viveretteaf6b2512016-11-16 17:33:10 -0500142// lint every library
Alan Viverette9290dcd2016-12-15 17:29:58 -0500143task(lint)
Alan Viveretteaf6b2512016-11-16 17:33:10 -0500144
Alan Viverette9290dcd2016-12-15 17:29:58 -0500145task(createXml).doLast({
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -0700146 def repoArchive = createRepository.archivePath
147 def repoArchiveName = createRepository.archiveName
148 def size = repoArchive.length()
149 def sha1 = getSha1(repoArchive)
150
151 def xml =
152"<sdk:sdk-addon xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:sdk=\"http://schemas.android.com/sdk/android/addon/6\">\n\
153 <sdk:extra>\n\
154 <sdk:revision>\n\
155 <sdk:major>${project.ext.extraVersion}</sdk:major>\n\
156 </sdk:revision>\n\
157 <sdk:vendor-display>Android</sdk:vendor-display>\n\
158 <sdk:vendor-id>android</sdk:vendor-id>\n\
159 <sdk:name-display>Local Maven repository for Support Libraries</sdk:name-display>\n\
160 <sdk:path>m2repository</sdk:path>\n\
161 <sdk:archives>\n\
Xavier Ducrohetc16b62d2014-12-09 12:37:45 -0800162 <sdk:archive>\n\
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -0700163 <sdk:size>${size}</sdk:size>\n\
164 <sdk:checksum type=\"sha1\">${sha1}</sdk:checksum>\n\
165 <sdk:url>${repoArchiveName}</sdk:url>\n\
166 </sdk:archive>\n\
167 </sdk:archives>\n\
168 </sdk:extra>\n\
169</sdk:sdk-addon>"
170
171 Files.write(xml, new File(project.ext.distDir, 'repo-extras.xml'), Charsets.UTF_8)
Alan Viverette9290dcd2016-12-15 17:29:58 -0500172})
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -0700173createArchive.dependsOn createXml
174
Alan Viverette9290dcd2016-12-15 17:29:58 -0500175task(createSourceProp).doLast({
Xavier Ducrohet64fe2322014-06-16 17:59:34 -0700176 def sourceProp =
177"Extra.VendorDisplay=Android\n\
178Extra.Path=m2repository\n\
179Archive.Arch=ANY\n\
180Extra.NameDisplay=Android Support Repository\n\
181Archive.Os=ANY\n\
Alan Viverette5ae24d62016-04-06 16:17:13 -0400182Pkg.Desc=Local Maven repository for Support Libraries\n\
Xavier Ducrohet64fe2322014-06-16 17:59:34 -0700183Pkg.Revision=${project.ext.extraVersion}.0.0\n\
184Extra.VendorId=android"
185
186 Files.write(sourceProp, new File(project.ext.supportRepoOut, 'source.properties'), Charsets.UTF_8)
Alan Viverette9290dcd2016-12-15 17:29:58 -0500187})
Xavier Ducrohet64fe2322014-06-16 17:59:34 -0700188createSourceProp.dependsOn unzipRepo
189prepareRepo.dependsOn createSourceProp
190
Chris Banes96f1e912015-03-05 20:04:05 +0000191import java.nio.charset.Charset
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -0700192
Alan Viverettecc5197e2016-06-13 12:45:07 -0400193/**
194 * Generates SHA1 hash for the specified file's absolute path.
195 *
196 * @param inputFile file to hash
197 * @return SHA1 hash
198 */
199String getSha1(File inputFile) {
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -0700200 HashFunction hashFunction = Hashing.sha1()
Chris Banes96f1e912015-03-05 20:04:05 +0000201 HashCode hashCode = hashFunction.hashString(inputFile.getAbsolutePath(), Charset.forName("UTF-8"))
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -0700202 return hashCode.toString()
203}
204
Alan Viverette9562a3b2016-07-01 13:26:39 -0400205void registerForDocsTask(Task task, Project subProject, releaseVariant) {
206 task.dependsOn releaseVariant.javaCompile
207 task.source {
Alan Viverettecc5197e2016-06-13 12:45:07 -0400208 def buildConfig = fileTree(releaseVariant.getGenerateBuildConfig().sourceOutputDir)
209 return releaseVariant.javaCompile.source.minus(buildConfig) +
210 fileTree(releaseVariant.aidlCompile.sourceOutputDir) +
211 fileTree(releaseVariant.outputs[0].processResources.sourceOutputDir)
212 }
Alan Viverette9562a3b2016-07-01 13:26:39 -0400213 task.classpath += files(releaseVariant.javaCompile.classpath) +
Alan Viverettecc5197e2016-06-13 12:45:07 -0400214 files(releaseVariant.javaCompile.destinationDir)
Alan Viverettecc5197e2016-06-13 12:45:07 -0400215}
216
Alan Viverette9562a3b2016-07-01 13:26:39 -0400217// Generates online docs.
218task generateDocs(type: DoclavaTask, dependsOn: configurations.doclava) {
219 docletpath = configurations.doclava.resolve()
220 destinationDir = new File(project.docsDir, "online")
221
222 // Base classpath is Android SDK, sub-projects add their own.
Aurimas Liutikasd4d9cde2016-11-23 12:12:50 -0800223 classpath = project.ext.androidJar
Alan Viverette9562a3b2016-07-01 13:26:39 -0400224
225 def hdfOption = new DoclavaMultilineJavadocOptionFileOption('hdf')
226 hdfOption.add(
227 ['android.whichdoc', 'online'],
228 ['android.hasSamples', 'true']);
229
Alan Viverette989f27a2016-11-29 17:28:15 -0500230 // Default hidden errors + hidden superclass (111) and
231 // deprecation mismatch (113) to match framework docs.
232 final def hidden = [105, 107, 111, 112, 113, 115, 116, 121]
233
234 doclavaErrors = (101..122) - hidden
235 doclavaWarnings = []
236 doclavaHidden += hidden
237
Alan Viverette9562a3b2016-07-01 13:26:39 -0400238 options {
239 addStringOption "templatedir",
240 "${project.rootDir}/../../build/tools/droiddoc/templates-sdk"
241 addStringOption "federate Android", "http://developer.android.com"
Alan Viverette9562a3b2016-07-01 13:26:39 -0400242 addStringOption "stubpackages", "android.support.*"
243 addStringOption "samplesdir", "${project.rootDir}/samples"
244 addOption hdfOption
245 }
246
247 exclude '**/BuildConfig.java'
248}
Alan Viverettecc5197e2016-06-13 12:45:07 -0400249
Alan Viverette9290dcd2016-12-15 17:29:58 -0500250JDiffTask createApiDiffsTask(String taskName, File oldApiXml, File newApiXml, File outDir,
251 Configuration jdiff, Task... dependencies) {
252 return tasks.create(name: taskName, type: JDiffTask.class) {
253 dependsOn jdiff
254 dependsOn dependencies
255
256 docletpath = jdiff.resolve()
257
258 oldApiXmlFile oldApiXml
259 newApiXmlFile newApiXml
260 destinationDir = outDir
261
262 // This prefix is based on the assumption that the output diffs will
263 // ultimately land in frameworks/base/docs/html/sdk/support_api_diff/.
264 newJavadocPrefix = "../reference/"
265 }
266}
267
Alan Viverettecc5197e2016-06-13 12:45:07 -0400268// Generates API files.
269task generateApi(type: DoclavaTask, dependsOn: configurations.doclava) {
270 docletpath = configurations.doclava.resolve()
271 destinationDir = project.docsDir
272
273 // Base classpath is Android SDK, sub-projects add their own.
Aurimas Liutikasd4d9cde2016-11-23 12:12:50 -0800274 classpath = project.ext.androidJar
Alan Viverettecc5197e2016-06-13 12:45:07 -0400275
276 apiFile = new File(project.docsDir, 'release/current.txt')
277 removedApiFile = new File(project.docsDir, 'release/removed.txt')
278 generateDocs = false
279
280 options {
281 addStringOption "templatedir",
282 "${project.rootDir}/../../build/tools/droiddoc/templates-sdk"
283 addStringOption "federate Android", "http://developer.android.com"
Alan Viverettecc5197e2016-06-13 12:45:07 -0400284 addStringOption "stubpackages", "android.support.*"
285 }
286 exclude '**/BuildConfig.java'
287 exclude '**/R.java'
288}
289
290// Copies generated API files to current version.
291task updateApi(type: UpdateApiTask, dependsOn: generateApi) {
292 newApiFile = new File(project.docsDir, 'release/current.txt')
Alan Viverette7b59d3a2016-06-13 12:52:20 -0400293 oldApiFile = new File(project.rootDir, 'api/current.txt')
Alan Viverettecc5197e2016-06-13 12:45:07 -0400294 newRemovedApiFile = new File(project.docsDir, 'release/removed.txt')
Alan Viverette7b59d3a2016-06-13 12:52:20 -0400295 oldRemovedApiFile = new File(project.rootDir, 'api/removed.txt')
Alan Viverettecc5197e2016-06-13 12:45:07 -0400296}
297
298// Checks generated API files against current version.
299task checkApi(type: CheckApiTask, dependsOn: generateApi) {
300 doclavaClasspath = generateApi.docletpath
301
302 checkApiTaskPath = name
303 updateApiTaskPath = updateApi.name
304
Alan Viverette62ff5d92016-12-07 09:51:16 -0500305 // Check that the API we're building hasn't changed from the development
306 // version. These typed of changes require an explicit API file update.
307 checkApiErrors = (2..30)-[22]
308 checkApiWarnings = []
309 checkApiHidden = [22]
310
Alan Viverettecc5197e2016-06-13 12:45:07 -0400311 newApiFile = new File(project.docsDir, 'release/current.txt')
Alan Viverette7b59d3a2016-06-13 12:52:20 -0400312 oldApiFile = new File(project.rootDir, 'api/current.txt')
Alan Viverettecc5197e2016-06-13 12:45:07 -0400313 newRemovedApiFile = new File(project.docsDir, 'release/removed.txt')
Alan Viverette7b59d3a2016-06-13 12:52:20 -0400314 oldRemovedApiFile = new File(project.rootDir, 'api/removed.txt')
Yigit Boyar3986e042016-02-08 18:31:38 -0800315}
Alan Viverettedd377c92016-06-24 09:51:49 -0400316createArchive.dependsOn checkApi
Yigit Boyar3986e042016-02-08 18:31:38 -0800317
Alan Viverette62ff5d92016-12-07 09:51:16 -0500318// Checks generated API files against current version.
319task checkApiStable(type: CheckApiTask, dependsOn: generateApi) {
320 doclavaClasspath = generateApi.docletpath
321
322 checkApiTaskPath = name
323 updateApiTaskPath = updateApi.name
324
325 // Check that the API we're building hasn't broken the last-released
326 // library version. These types of changes are forbidden.
327 checkApiErrors = (7..18)
328 checkApiWarnings = [23, 24]
329 checkApiHidden = (2..6) + (19..22) + (25..30)
330
Alan Viverette62ff5d92016-12-07 09:51:16 -0500331 newApiFile = new File(project.docsDir, 'release/current.txt')
Alan Viverette9290dcd2016-12-15 17:29:58 -0500332 oldApiFile = getReleasedApiFile()
Alan Viverette62ff5d92016-12-07 09:51:16 -0500333 newRemovedApiFile = new File(project.docsDir, 'release/removed.txt')
334 oldRemovedApiFile = new File(project.rootDir, 'api/removed.txt')
335}
336checkApi.dependsOn checkApiStable
337
Alan Viverette9290dcd2016-12-15 17:29:58 -0500338/**
339 * Converts the <code>toApi</code>.txt file (or current.txt if not explicitly
340 * defined using -DtoAPi=<file>) to XML format for use by JDiff.
341 */
342task newApiXml(type: ApiXmlConversionTask, dependsOn: configurations.doclava) {
343 classpath configurations.doclava.resolve()
344
345 if (project.hasProperty("toApi")) {
346 // Use an explicit API file.
347 inputApiFile = new File(project.rootDir, "api/${toApi}.txt")
348 } else {
349 // Use the current API file (e.g. current.txt).
350 inputApiFile = generateApi.apiFile
351 dependsOn generateApi
352 }
353
354 int lastDot = inputApiFile.name.lastIndexOf('.')
355 outputApiXmlFile = new File(project.docsDir,
356 "release/" + inputApiFile.name.substring(0, lastDot) + ".xml")
357}
358
359/**
360 * Converts the <code>fromApi</code>.txt file (or the most recently released
361 * X.Y.Z.txt if not explicitly defined using -DfromAPi=<file>) to XML format
362 * for use by JDiff.
363 */
364task oldApiXml(type: ApiXmlConversionTask, dependsOn: configurations.doclava) {
365 classpath configurations.doclava.resolve()
366
367 if (project.hasProperty("fromApi")) {
368 // Use an explicit API file.
369 inputApiFile = new File(project.rootDir, "api/${fromApi}.txt")
370 } else if (project.hasProperty("toApi") && toApi.matches(~/(\d+\.){2}\d+/)) {
371 // If toApi matches released API (X.Y.Z) format, use the most recently
372 // released API file prior to toApi.
373 inputApiFile = getReleasedApiFile(toApi)
374 } else {
375 // Use the most recently released API file.
376 inputApiFile = getReleasedApiFile();
377 }
378
379 int lastDot = inputApiFile.name.lastIndexOf('.')
380 outputApiXmlFile = new File(project.docsDir,
381 "release/" + inputApiFile.name.substring(0, lastDot) + ".xml")
382}
383
384/**
385 * Generates API diffs.
386 * <p>
387 * By default, diffs are generated for the delta between current.txt and the
388 * next most recent X.Y.Z.txt API file. Behavior may be changed by specifying
389 * one or both of -DtoApi and -DfromApi.
390 * <p>
391 * If both fromApi and toApi are specified, diffs will be generated for
392 * fromApi -> toApi. For example, 25.0.0 -> 26.0.0 diffs could be generated by
393 * using:
394 * <br><code>
395 * ./gradlew generateDiffs -DfromApi=25.0.0 -DtoApi=26.0.0
396 * </code>
397 * <p>
398 * If only toApi is specified, it MUST be specified as X.Y.Z and diffs will be
399 * generated for (release before toApi) -> toApi. For example, 24.2.0 -> 25.0.0
400 * diffs could be generated by using:
401 * <br><code>
402 * ./gradlew generateDiffs -DtoApi=25.0.0
403 * </code>
404 * <p>
405 * If only fromApi is specified, diffs will be generated for fromApi -> current.
406 * For example, lastApiReview -> current diffs could be generated by using:
407 * <br><code>
408 * ./gradlew generateDiffs -DfromApi=lastApiReview
409 * </code>
410 * <p>
411 */
412task generateDiffs(type: JDiffTask, dependsOn: [configurations.jdiff, configurations.doclava,
413 oldApiXml, newApiXml]) {
414 // Base classpath is Android SDK, sub-projects add their own.
415 classpath = project.ext.androidJar
416
417 // JDiff properties.
418 oldApiXmlFile = oldApiXml.outputApiXmlFile
419 newApiXmlFile = newApiXml.outputApiXmlFile
420 newJavadocPrefix = "../../../../reference/"
421
422 String newApi = newApiXmlFile.name
423 int lastDot = newApi.lastIndexOf('.')
424 newApi = newApi.substring(0, lastDot)
425
426 // Javadoc properties.
427 docletpath = configurations.jdiff.resolve()
428 destinationDir = new File(project.docsDir, "support_api_diff/$newApi")
429 title = "Support&nbsp;Library&nbsp;API&nbsp;Differences&nbsp;Report"
430
431 exclude '**/BuildConfig.java'
432 exclude '**/R.java'
433}
434
435/**
436 * Returns the most recently released API, optionally restricting to APIs
437 * before <code>beforeApi</code>.
438 *
439 * @param beforeApi the API to find an API file before, ex. 25.0.0
440 * @return the most recently released API file
441 */
442File getReleasedApiFile(String beforeApi = null) {
443 String beforeApiFileName = beforeApi != null ? beforeApi + ".txt" : null
444 File lastReleasedApiFile = null
445 File apiDir = new File(project.rootDir, 'api')
446
447 apiDir.eachFileMatch FileType.FILES, ~/(\d+\.){3}txt/, { File apiFile ->
448 // Is the current API file newer than the last one we saw?
449 if (lastReleasedApiFile == null || apiFile.name > lastReleasedApiFile.name) {
450 // Is the current API file older than the "before" API?
451 if (beforeApiFileName == null || apiFile.name < beforeApiFileName) {
452 lastReleasedApiFile = apiFile
453 }
454 }
455 }
456
457 return lastReleasedApiFile
458}
459
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -0800460subprojects {
Alan Viverette50323732016-12-02 11:18:03 -0500461 // Only modify Android projects.
Alan Viverette9290dcd2016-12-15 17:29:58 -0500462 if (project.name.equals('doclava') || project.name.equals('jdiff')) return;
Alan Viverettecc5197e2016-06-13 12:45:07 -0400463
Alan Viverette50323732016-12-02 11:18:03 -0500464 // Current SDK is set in studioCompat.gradle.
Yigit Boyar3986e042016-02-08 18:31:38 -0800465 project.ext.currentSdk = gradle.ext.currentSdk
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -0800466 apply plugin: 'maven'
Yigit Boyar3986e042016-02-08 18:31:38 -0800467
Xavier Ducrohet855a9222014-01-02 19:00:43 -0800468 version = rootProject.ext.supportVersion
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -0800469 group = 'com.android.support'
470
Yigit Boyarbe7a54a2015-04-07 13:23:50 -0700471 repositories {
472 maven { url "${project.parent.projectDir}/../../prebuilts/tools/common/m2/repository" }
473 maven { url "${project.parent.projectDir}/../../prebuilts/tools/common/m2/internal" }
474 maven { url "${project.parent.projectDir}/../../prebuilts/maven_repo/android" }
475 }
476
Jeff Davidson84faec582014-06-18 09:10:36 -0700477 project.plugins.whenPluginAdded { plugin ->
Alan Viverette50323732016-12-02 11:18:03 -0500478 def isAndroidLibrary = "com.android.build.gradle.LibraryPlugin".equals(plugin.class.name)
479 def isAndroidApp = "com.android.build.gradle.AppPlugin".equals(plugin.class.name)
480 def isJavaLibrary = "org.gradle.api.plugins.JavaPlugin".equals(plugin.class.name)
481
482 if (isAndroidLibrary || isAndroidApp) {
Jeff Davidson84faec582014-06-18 09:10:36 -0700483 project.android.buildToolsVersion = rootProject.buildToolsVersion
Alan Viverette50323732016-12-02 11:18:03 -0500484
485 // Enable code coverage for debug builds only if we are not running inside the IDE,
486 // since enabling coverage reports breaks the method parameter resolution in the IDE
487 // debugger.
Yigit Boyard8d42d52016-04-12 18:20:18 -0700488 project.android.buildTypes.debug.testCoverageEnabled = !hasProperty('android.injected.invoked.from.ide')
Alan Viveretteaf6b2512016-11-16 17:33:10 -0500489
Shankhoneer Chakrovarty2e6107d2016-10-17 17:39:35 -0700490 // Copy the class files in a jar to be later used to generate code coverage report
491 project.android.testVariants.all { v ->
492 // check if the variant has any source files
493 // and test coverage is enabled
494 if (v.buildType.testCoverageEnabled
495 && v.sourceSets.any { !it.java.sourceFiles.isEmpty() }) {
496 def jarifyTask = project.tasks.create(
497 name: "package${v.name.capitalize()}ClassFilesForCoverageReport",
498 type: Jar) {
Shankhoneer Chakrovarty719c3232016-11-23 15:39:01 -0800499 from v.testedVariant.javaCompile.destinationDir
Shankhoneer Chakrovarty0056534b2017-01-03 17:34:35 -0800500 exclude "**/R.class"
501 exclude "**/R\$*.class"
Shankhoneer Chakrovarty6e45f612016-11-14 16:28:31 -0800502 destinationDir file(project.distDir)
503 archiveName "${project.archivesBaseName}-${v.baseName}-allclasses.jar"
Shankhoneer Chakrovarty2e6107d2016-10-17 17:39:35 -0700504 }
Shankhoneer Chakrovarty6e45f612016-11-14 16:28:31 -0800505 def jacocoAntConfig =
506 project.configurations[JacocoPlugin.ANT_CONFIGURATION_NAME]
507 def jacocoAntArtifacts = jacocoAntConfig.resolvedConfiguration.resolvedArtifacts
508 def version = jacocoAntArtifacts.find { "org.jacoco.ant".equals(it.name) }
509 .moduleVersion.id.version
510 def collectJacocoAntPackages = project.tasks.create(
511 name: "collectJacocoAntPackages",
512 type: Jar) {
513 from (jacocoAntArtifacts.collect { zipTree(it.getFile()) }) {
514 // exclude all the signatures the jar might have
515 exclude "META-INF/*.SF"
516 exclude "META-INF/*.DSA"
517 exclude "META-INF/*.RSA"
518 }
519 destinationDir file(project.distDir)
520 archiveName "jacocoant-" + version + ".jar"
Shankhoneer Chakrovarty2e6107d2016-10-17 17:39:35 -0700521 }
Shankhoneer Chakrovarty6e45f612016-11-14 16:28:31 -0800522 v.assemble.dependsOn jarifyTask, collectJacocoAntPackages
Shankhoneer Chakrovarty2e6107d2016-10-17 17:39:35 -0700523 }
524 }
Alan Viverettee7589642016-11-16 18:18:36 -0500525
Alan Viverette50323732016-12-02 11:18:03 -0500526 // Enforce NewApi lint check as fatal.
Alan Viveretteaf6b2512016-11-16 17:33:10 -0500527 project.android.lintOptions.check 'NewApi'
528 project.android.lintOptions.fatal 'NewApi'
529 project.parent.lint.dependsOn project.lint
Jeff Davidson84faec582014-06-18 09:10:36 -0700530 }
Alan Viverette573630e2016-07-08 17:17:47 -0400531
Alan Viverette50323732016-12-02 11:18:03 -0500532 if (isAndroidLibrary || isJavaLibrary) {
533 // Add library to the aggregate dependency report.
534 task allDeps(type: DependencyReportTask) {}
535
536 // Create release and separate zip task for library.
Alan Viverette573630e2016-07-08 17:17:47 -0400537 task release(type: Upload) {
538 configuration = configurations.archives
539 repositories {
540 mavenDeployer {
541 repository(url: uri("$rootProject.ext.supportRepoOut"))
542
543 // Disable unique names for SNAPSHOTS so they can be updated in place.
544 setUniqueVersion(false)
545 doLast {
546 // Remove any invalid maven-metadata.xml files that may have been
547 // created for SNAPSHOT versions that are *not* uniquely versioned.
548 pom*.each { pom ->
549 if (pom.version.endsWith('-SNAPSHOT')) {
550 final File artifactDir = new File(
551 rootProject.ext.supportRepoOut,
552 pom.groupId.replace('.', '/')
553 + '/' + pom.artifactId
554 + '/' + pom.version)
555 delete fileTree(dir: artifactDir,
556 include: 'maven-metadata.xml*')
557 }
558 }
559 }
560 }
561 }
562 }
563
564 def deployer = release.repositories.mavenDeployer
565 deployer.pom*.whenConfigured { pom ->
566 pom.dependencies.findAll { dep ->
567 dep.groupId == 'com.android.support' && dep.artifactId != 'support-annotations'
568 }*.type = 'aar'
569 }
570
571 ext.versionDir = {
572 def groupDir = new File(rootProject.ext.supportRepoOut,
573 project.group.replace('.','/'))
574 def artifactDir = new File(groupDir, archivesBaseName)
575 return new File(artifactDir, version)
576 }
577
Alan Viverette9290dcd2016-12-15 17:29:58 -0500578 task generateSourceProps(dependsOn: createRepository)
579 generateSourceProps.doLast({
Alan Viverette573630e2016-07-08 17:17:47 -0400580 def content = "Maven.GroupId=$deployer.pom.groupId\n" +
581 "Maven.ArtifactId=$deployer.pom.artifactId\n" +
582 "Maven.Version=$deployer.pom.version\n" +
Alan Viverette4453f0d2016-09-14 12:32:27 -0700583 "Extra.VendorDisplay=Android\n" +
584 "Extra.VendorId=android\n" +
Alan Viverette573630e2016-07-08 17:17:47 -0400585 "Pkg.Desc=$project.name\n" +
586 "Pkg.Revision=1\n" +
Alan Viverette4453f0d2016-09-14 12:32:27 -0700587 "Maven.Dependencies=" +
Alan Viverette573630e2016-07-08 17:17:47 -0400588 String.join(",", project.configurations.compile.allDependencies.collect {
589 def p = parent.findProject(it.name)
590 return p ? "$p.group:$p.archivesBaseName:$p.version" : null
591 }.grep()) +
592 "\n"
593 Files.write(content, new File(versionDir(), 'source.properties'), Charsets.UTF_8)
Alan Viverette9290dcd2016-12-15 17:29:58 -0500594 })
Alan Viverette573630e2016-07-08 17:17:47 -0400595
596 task createSeparateZip(type: Zip, dependsOn: generateSourceProps) {
597 into archivesBaseName
598 destinationDir project.parent.ext.distDir
599 baseName = project.group
600 version = project.parent.ext.buildNumber
601 }
602 project.parent.createArchive.dependsOn createSeparateZip
603
Alan Viverette50323732016-12-02 11:18:03 -0500604 // Before the upload, make sure the repo is ready.
Alan Viverette573630e2016-07-08 17:17:47 -0400605 release.dependsOn rootProject.tasks.prepareRepo
Alan Viverette50323732016-12-02 11:18:03 -0500606
607 // Make the mainupload depend on this one.
Alan Viverette573630e2016-07-08 17:17:47 -0400608 mainUpload.dependsOn release
609 }
Jeff Davidson84faec582014-06-18 09:10:36 -0700610 }
Chris Banesdaea0692015-12-29 12:48:24 +0000611
Chris Banesdaea0692015-12-29 12:48:24 +0000612 project.afterEvaluate {
Joe Baker-Malone5e2c51d2016-05-13 15:09:24 -0700613 // The archivesBaseName isn't available intially, so set it now
Alan Viverette573630e2016-07-08 17:17:47 -0400614 def createZipTask = project.tasks.findByName("createSeparateZip")
615 if (createZipTask != null) {
616 createZipTask.appendix = archivesBaseName
617 createZipTask.from versionDir()
618 }
Joe Baker-Malone5e2c51d2016-05-13 15:09:24 -0700619
620 // Copy instrumentation test APK into the dist dir
Chris Banesdaea0692015-12-29 12:48:24 +0000621 def assembleTestTask = project.tasks.findByPath('assembleAndroidTest')
622 if (assembleTestTask != null) {
623 assembleTestTask.doLast {
624 // If the project actually has some instrumentation tests, copy its APK
625 if (!project.android.sourceSets.androidTest.java.sourceFiles.isEmpty()) {
626 def pkgTask = project.tasks.findByPath('packageDebugAndroidTest')
627 copy {
628 from(pkgTask.outputFile)
629 into(rootProject.ext.testApkDistOut)
630 }
631 }
632 }
633 }
634 }
Yigit Boyarea5d9b22016-03-08 13:25:26 -0800635
636 project.afterEvaluate { p ->
637 // remove dependency on the test so that we still get coverage even if some tests fail
638 p.tasks.findAll { it instanceof JacocoReportTask}.each { task ->
639 def toBeRemoved = new ArrayList()
640 def dependencyList = task.taskDependencies.values
641 dependencyList.each { dep ->
642 if (dep instanceof String) {
643 def t = tasks.findByName(dep)
644 if (t instanceof DeviceProviderInstrumentTestTask) {
645 toBeRemoved.add(dep)
646 task.mustRunAfter(t)
647 }
648 }
649 }
650 toBeRemoved.each { dep ->
651 dependencyList.remove(dep)
652 }
653 }
654 }
Alan Viverettecc5197e2016-06-13 12:45:07 -0400655
656 project.afterEvaluate { p ->
657 if (p.hasProperty('android')
658 && p.android.hasProperty('libraryVariants')
659 && !(p.android.hasProperty('noDocs') && p.android.noDocs)) {
660 p.android.libraryVariants.all { v ->
661 if (v.name == 'release') {
Alan Viverette9562a3b2016-07-01 13:26:39 -0400662 registerForDocsTask(rootProject.generateDocs, p, v)
663 registerForDocsTask(rootProject.generateApi, p, v)
Alan Viverette9290dcd2016-12-15 17:29:58 -0500664 registerForDocsTask(rootProject.generateDiffs, p, v)
Alan Viverettecc5197e2016-06-13 12:45:07 -0400665 }
666 }
667 }
668 }
Chris Banes310f81d2016-11-13 16:13:53 +0000669
670 // Update the version meta-data in each Manifest
671 project.afterEvaluate { p ->
672 if (p.hasProperty('android')) {
673 p.android.defaultConfig.manifestPlaceholders =
674 ["support-version": rootProject.ext.supportVersion]
675 }
676 }
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -0800677}
678
Chris Banes9ae4ee82015-09-11 10:32:15 +1000679project.gradle.buildFinished { buildResult ->
680 if (buildResult.getFailure() != null) {
681 println()
682 println 'Build failed. Possible causes include:'
683 println ' 1) Bad codes'
684 println ' 2) Out of date prebuilts in prebuilts/sdk'
Chris Banes9e2e8032015-09-16 10:15:37 +0100685 println ' 3) Need to update the compileSdkVersion in a library\'s build.gradle'
Chris Banes9ae4ee82015-09-11 10:32:15 +1000686 println()
687 }
688}