blob: d43888bd4dbb56644512f4287ba6672772dd2b6b [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 {
Aurimas Liutikasf09d8582017-01-20 21:49:51 -080022 apply from: 'buildSrc/dependencies.gradle'
23
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -080024 repositories {
25 maven { url '../../prebuilts/gradle-plugin' }
26 maven { url '../../prebuilts/tools/common/m2/repository' }
27 maven { url '../../prebuilts/tools/common/m2/internal' }
Aurimas Liutikasf09d8582017-01-20 21:49:51 -080028 maven { url '../../prebuilts/maven_repo/android' }
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -080029 }
30 dependencies {
Aurimas Liutikasf09d8582017-01-20 21:49:51 -080031 classpath libs.gradle
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -080032 }
33}
34
Aurimas Liutikasf09d8582017-01-20 21:49:51 -080035apply from: 'buildSrc/dependencies.gradle'
36
Alan Viverettecc5197e2016-06-13 12:45:07 -040037repositories {
38 maven { url '../../prebuilts/tools/common/m2/repository' }
39}
40
41configurations {
42 doclava
Alan Viverette9290dcd2016-12-15 17:29:58 -050043 jdiff
Alan Viverettecc5197e2016-06-13 12:45:07 -040044}
45
46dependencies {
47 doclava project(':doclava')
Alan Viverette9290dcd2016-12-15 17:29:58 -050048 jdiff project(':jdiff')
Aurimas Liutikasf09d8582017-01-20 21:49:51 -080049 jdiff libs.xml_parser_apis
50 jdiff libs.xerces_impl
Alan Viverettecc5197e2016-06-13 12:45:07 -040051}
52
Alan Viverette9290dcd2016-12-15 17:29:58 -050053// Version code components.
54ext.supportVersion = "26.0.0-SNAPSHOT"
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -080055
Alan Viverette9290dcd2016-12-15 17:29:58 -050056// This number gets incremented for each public release.
Kathy Kam047e4f82017-01-30 12:46:13 -080057ext.extraVersion = 43
Alan Viverette9290dcd2016-12-15 17:29:58 -050058
Alan Viverettecc5197e2016-06-13 12:45:07 -040059// Enforce the use of prebuilt dependencies in all sub-projects. This is
60// required for the doclava dependency.
61ext.usePrebuilts = "true"
62
Aurimas Liutikasd4d9cde2016-11-23 12:12:50 -080063final String platform = OperatingSystem.current().isMacOsX() ? 'darwin' : 'linux'
64System.setProperty('android.dir', "${rootDir}/../../")
65final String fullSdkPath = "${rootDir}/../../prebuilts/fullsdk-${platform}"
66if (file(fullSdkPath).exists()) {
67 gradle.ext.currentSdk = 26
68 ext.buildToolsVersion = '26.0.0'
69 project.ext.androidJar = files("${fullSdkPath}/platforms/android-${gradle.ext.currentSdk}/android.jar")
70 System.setProperty('android.home', "${rootDir}/../../prebuilts/fullsdk-${platform}")
71 File props = file("local.properties")
72 props.write "sdk.dir=${fullSdkPath}"
73} else {
74 gradle.ext.currentSdk = 'current'
75 ext.buildToolsVersion = '24.0.1'
76 project.ext.androidJar = files("${project.rootDir}/../../prebuilts/sdk/current/android.jar")
77 File props = file("local.properties")
78 props.write "android.dir=../../"
79}
80
Alan Viverette9290dcd2016-12-15 17:29:58 -050081ext.supportRepoOut = ''
82ext.buildNumber = Integer.toString(ext.extraVersion)
83
Xavier Ducrohet020e4322014-03-18 16:41:30 -070084/*
85 * With the build server you are given two env variables.
86 * The OUT_DIR is a temporary directory you can use to put things during the build.
87 * The DIST_DIR is where you want to save things from the build.
88 *
89 * The build server will copy the contents of DIST_DIR to somewhere and make it available.
90 */
91if (System.env.DIST_DIR != null && System.env.OUT_DIR != null) {
Xavier Ducrohet4e04b7a2014-10-17 18:02:33 -070092 buildDir = new File(System.env.OUT_DIR + '/gradle/frameworks/support/build').getCanonicalFile()
93 project.ext.distDir = new File(System.env.DIST_DIR).getCanonicalFile()
Xavier Ducrohetfa385272014-11-14 13:12:09 -080094
95 // the build server does not pass the build number so we infer it from the last folder of the dist path.
96 ext.buildNumber = project.ext.distDir.getName()
Xavier Ducrohet020e4322014-03-18 16:41:30 -070097} else {
Alan Viverette7b59d3a2016-06-13 12:52:20 -040098 buildDir = file("${project.rootDir}/../../out/host/gradle/frameworks/support/build")
99 project.ext.distDir = file("${project.rootDir}/../../out/dist")
Xavier Ducrohet020e4322014-03-18 16:41:30 -0700100}
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -0800101
Alan Viverette9b5fe932016-07-22 10:28:31 -0400102subprojects {
103 // Change buildDir first so that all plugins pick up the new value.
104 project.buildDir = project.file("$project.parent.buildDir/../$project.name/build")
105}
106
Alan Viverettecc5197e2016-06-13 12:45:07 -0400107ext.docsDir = new File(buildDir, 'javadoc')
Xavier Ducrohet020e4322014-03-18 16:41:30 -0700108ext.supportRepoOut = new File(buildDir, 'support_repo')
Yigit Boyarf18d9752015-12-01 13:45:28 -0800109ext.testApkDistOut = ext.distDir
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -0800110
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -0700111// Main task called by the build server.
Alan Viverette9290dcd2016-12-15 17:29:58 -0500112task(createArchive)
Xavier Ducrohet020e4322014-03-18 16:41:30 -0700113
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -0700114// upload anchor for subprojects to upload their artifacts
115// to the local repo.
Alan Viverette9290dcd2016-12-15 17:29:58 -0500116task(mainUpload)
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -0700117
118// repository creation task
119task createRepository(type: Zip, dependsOn: mainUpload) {
Xavier Ducrohet020e4322014-03-18 16:41:30 -0700120 from project.ext.supportRepoOut
121 destinationDir project.ext.distDir
Xavier Ducrohet9dc44802014-03-20 14:15:16 -0700122 into 'm2repository'
Xavier Ducrohetfa385272014-11-14 13:12:09 -0800123 baseName = String.format("sdk-repo-linux-m2repository-%s", project.ext.buildNumber)
Xavier Ducrohet020e4322014-03-18 16:41:30 -0700124}
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -0700125createArchive.dependsOn createRepository
Xavier Ducrohet020e4322014-03-18 16:41:30 -0700126
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -0700127// prepare repository with older versions
Xavier Ducrohet64fe2322014-06-16 17:59:34 -0700128task unzipRepo(type: Copy) {
Alan Viverette7b59d3a2016-06-13 12:52:20 -0400129 from "${project.rootDir}/../../prebuilts/maven_repo/android"
Xavier Ducrohet855a9222014-01-02 19:00:43 -0800130 into project.ext.supportRepoOut
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -0800131}
132
Xavier Ducrohet64fe2322014-06-16 17:59:34 -0700133unzipRepo.doFirst {
Xavier Ducrohet020e4322014-03-18 16:41:30 -0700134 project.ext.supportRepoOut.deleteDir()
135 project.ext.supportRepoOut.mkdirs()
136}
137
Xavier Ducrohet64fe2322014-06-16 17:59:34 -0700138// anchor for prepare repo. This is post unzip + sourceProp.
Alan Viverette9290dcd2016-12-15 17:29:58 -0500139task(prepareRepo)
Xavier Ducrohet64fe2322014-06-16 17:59:34 -0700140
Alan Viveretteaf6b2512016-11-16 17:33:10 -0500141// lint every library
Alan Viverette9290dcd2016-12-15 17:29:58 -0500142task(lint)
Alan Viveretteaf6b2512016-11-16 17:33:10 -0500143
Alan Viverette9290dcd2016-12-15 17:29:58 -0500144task(createXml).doLast({
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -0700145 def repoArchive = createRepository.archivePath
146 def repoArchiveName = createRepository.archiveName
147 def size = repoArchive.length()
148 def sha1 = getSha1(repoArchive)
149
150 def xml =
151"<sdk:sdk-addon xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:sdk=\"http://schemas.android.com/sdk/android/addon/6\">\n\
152 <sdk:extra>\n\
153 <sdk:revision>\n\
154 <sdk:major>${project.ext.extraVersion}</sdk:major>\n\
155 </sdk:revision>\n\
156 <sdk:vendor-display>Android</sdk:vendor-display>\n\
157 <sdk:vendor-id>android</sdk:vendor-id>\n\
158 <sdk:name-display>Local Maven repository for Support Libraries</sdk:name-display>\n\
159 <sdk:path>m2repository</sdk:path>\n\
160 <sdk:archives>\n\
Xavier Ducrohetc16b62d2014-12-09 12:37:45 -0800161 <sdk:archive>\n\
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -0700162 <sdk:size>${size}</sdk:size>\n\
163 <sdk:checksum type=\"sha1\">${sha1}</sdk:checksum>\n\
164 <sdk:url>${repoArchiveName}</sdk:url>\n\
165 </sdk:archive>\n\
166 </sdk:archives>\n\
167 </sdk:extra>\n\
168</sdk:sdk-addon>"
169
170 Files.write(xml, new File(project.ext.distDir, 'repo-extras.xml'), Charsets.UTF_8)
Alan Viverette9290dcd2016-12-15 17:29:58 -0500171})
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -0700172createArchive.dependsOn createXml
Aurimas Liutikasfe67eb02017-01-27 11:04:44 -0800173createXml.dependsOn createRepository
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -0700174
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) {
Aurimas Liutikas11670682017-02-14 23:21:54 -0800219 group = JavaBasePlugin.DOCUMENTATION_GROUP
220 description = 'Generates d.android.com style documentation.'
221
Alan Viverette9562a3b2016-07-01 13:26:39 -0400222 docletpath = configurations.doclava.resolve()
223 destinationDir = new File(project.docsDir, "online")
224
225 // Base classpath is Android SDK, sub-projects add their own.
Aurimas Liutikasd4d9cde2016-11-23 12:12:50 -0800226 classpath = project.ext.androidJar
Alan Viverette9562a3b2016-07-01 13:26:39 -0400227
228 def hdfOption = new DoclavaMultilineJavadocOptionFileOption('hdf')
229 hdfOption.add(
230 ['android.whichdoc', 'online'],
231 ['android.hasSamples', 'true']);
232
Alan Viverette989f27a2016-11-29 17:28:15 -0500233 // Default hidden errors + hidden superclass (111) and
234 // deprecation mismatch (113) to match framework docs.
235 final def hidden = [105, 107, 111, 112, 113, 115, 116, 121]
236
237 doclavaErrors = (101..122) - hidden
238 doclavaWarnings = []
239 doclavaHidden += hidden
240
Alan Viverette9562a3b2016-07-01 13:26:39 -0400241 options {
242 addStringOption "templatedir",
243 "${project.rootDir}/../../build/tools/droiddoc/templates-sdk"
244 addStringOption "federate Android", "http://developer.android.com"
Alan Viverette9562a3b2016-07-01 13:26:39 -0400245 addStringOption "stubpackages", "android.support.*"
246 addStringOption "samplesdir", "${project.rootDir}/samples"
247 addOption hdfOption
248 }
249
250 exclude '**/BuildConfig.java'
251}
Alan Viverettecc5197e2016-06-13 12:45:07 -0400252
Alan Viverette9290dcd2016-12-15 17:29:58 -0500253JDiffTask createApiDiffsTask(String taskName, File oldApiXml, File newApiXml, File outDir,
254 Configuration jdiff, Task... dependencies) {
255 return tasks.create(name: taskName, type: JDiffTask.class) {
256 dependsOn jdiff
257 dependsOn dependencies
258
259 docletpath = jdiff.resolve()
260
261 oldApiXmlFile oldApiXml
262 newApiXmlFile newApiXml
263 destinationDir = outDir
264
265 // This prefix is based on the assumption that the output diffs will
266 // ultimately land in frameworks/base/docs/html/sdk/support_api_diff/.
267 newJavadocPrefix = "../reference/"
268 }
269}
270
Alan Viverettecc5197e2016-06-13 12:45:07 -0400271// Generates API files.
272task generateApi(type: DoclavaTask, dependsOn: configurations.doclava) {
273 docletpath = configurations.doclava.resolve()
274 destinationDir = project.docsDir
275
276 // Base classpath is Android SDK, sub-projects add their own.
Aurimas Liutikasd4d9cde2016-11-23 12:12:50 -0800277 classpath = project.ext.androidJar
Alan Viverettecc5197e2016-06-13 12:45:07 -0400278
279 apiFile = new File(project.docsDir, 'release/current.txt')
280 removedApiFile = new File(project.docsDir, 'release/removed.txt')
281 generateDocs = false
282
283 options {
284 addStringOption "templatedir",
285 "${project.rootDir}/../../build/tools/droiddoc/templates-sdk"
286 addStringOption "federate Android", "http://developer.android.com"
Alan Viverettecc5197e2016-06-13 12:45:07 -0400287 addStringOption "stubpackages", "android.support.*"
288 }
289 exclude '**/BuildConfig.java'
290 exclude '**/R.java'
291}
292
293// Copies generated API files to current version.
294task updateApi(type: UpdateApiTask, dependsOn: generateApi) {
Aurimas Liutikas11670682017-02-14 23:21:54 -0800295 group JavaBasePlugin.VERIFICATION_GROUP
296 description 'Invoke Doclava\'s ApiCheck tool to update current.txt based on current changes.'
297
Alan Viverettecc5197e2016-06-13 12:45:07 -0400298 newApiFile = new File(project.docsDir, 'release/current.txt')
Alan Viverette7b59d3a2016-06-13 12:52:20 -0400299 oldApiFile = new File(project.rootDir, 'api/current.txt')
Alan Viverettecc5197e2016-06-13 12:45:07 -0400300 newRemovedApiFile = new File(project.docsDir, 'release/removed.txt')
Alan Viverette7b59d3a2016-06-13 12:52:20 -0400301 oldRemovedApiFile = new File(project.rootDir, 'api/removed.txt')
Alan Viverettecc5197e2016-06-13 12:45:07 -0400302}
303
304// Checks generated API files against current version.
305task checkApi(type: CheckApiTask, dependsOn: generateApi) {
306 doclavaClasspath = generateApi.docletpath
307
308 checkApiTaskPath = name
309 updateApiTaskPath = updateApi.name
310
Alan Viverette62ff5d92016-12-07 09:51:16 -0500311 // Check that the API we're building hasn't changed from the development
312 // version. These typed of changes require an explicit API file update.
313 checkApiErrors = (2..30)-[22]
314 checkApiWarnings = []
315 checkApiHidden = [22]
316
Alan Viverettecc5197e2016-06-13 12:45:07 -0400317 newApiFile = new File(project.docsDir, 'release/current.txt')
Alan Viverette7b59d3a2016-06-13 12:52:20 -0400318 oldApiFile = new File(project.rootDir, 'api/current.txt')
Alan Viverettecc5197e2016-06-13 12:45:07 -0400319 newRemovedApiFile = new File(project.docsDir, 'release/removed.txt')
Alan Viverette7b59d3a2016-06-13 12:52:20 -0400320 oldRemovedApiFile = new File(project.rootDir, 'api/removed.txt')
Yigit Boyar3986e042016-02-08 18:31:38 -0800321}
Alan Viverettedd377c92016-06-24 09:51:49 -0400322createArchive.dependsOn checkApi
Yigit Boyar3986e042016-02-08 18:31:38 -0800323
Alan Viverette62ff5d92016-12-07 09:51:16 -0500324// Checks generated API files against current version.
325task checkApiStable(type: CheckApiTask, dependsOn: generateApi) {
326 doclavaClasspath = generateApi.docletpath
327
328 checkApiTaskPath = name
329 updateApiTaskPath = updateApi.name
330
331 // Check that the API we're building hasn't broken the last-released
332 // library version. These types of changes are forbidden.
333 checkApiErrors = (7..18)
334 checkApiWarnings = [23, 24]
335 checkApiHidden = (2..6) + (19..22) + (25..30)
336
Alan Viverette62ff5d92016-12-07 09:51:16 -0500337 newApiFile = new File(project.docsDir, 'release/current.txt')
Alan Viverette9290dcd2016-12-15 17:29:58 -0500338 oldApiFile = getReleasedApiFile()
Alan Viverette62ff5d92016-12-07 09:51:16 -0500339 newRemovedApiFile = new File(project.docsDir, 'release/removed.txt')
340 oldRemovedApiFile = new File(project.rootDir, 'api/removed.txt')
341}
342checkApi.dependsOn checkApiStable
343
Alan Viverette9290dcd2016-12-15 17:29:58 -0500344/**
345 * Converts the <code>toApi</code>.txt file (or current.txt if not explicitly
Alan Viverette6e8741c2017-01-25 10:44:07 -0500346 * defined using -PtoAPi=<file>) to XML format for use by JDiff.
Alan Viverette9290dcd2016-12-15 17:29:58 -0500347 */
348task newApiXml(type: ApiXmlConversionTask, dependsOn: configurations.doclava) {
349 classpath configurations.doclava.resolve()
350
351 if (project.hasProperty("toApi")) {
352 // Use an explicit API file.
353 inputApiFile = new File(project.rootDir, "api/${toApi}.txt")
354 } else {
355 // Use the current API file (e.g. current.txt).
356 inputApiFile = generateApi.apiFile
357 dependsOn generateApi
358 }
359
360 int lastDot = inputApiFile.name.lastIndexOf('.')
361 outputApiXmlFile = new File(project.docsDir,
362 "release/" + inputApiFile.name.substring(0, lastDot) + ".xml")
363}
364
365/**
366 * Converts the <code>fromApi</code>.txt file (or the most recently released
Alan Viverette6e8741c2017-01-25 10:44:07 -0500367 * X.Y.Z.txt if not explicitly defined using -PfromAPi=<file>) to XML format
Alan Viverette9290dcd2016-12-15 17:29:58 -0500368 * for use by JDiff.
369 */
370task oldApiXml(type: ApiXmlConversionTask, dependsOn: configurations.doclava) {
371 classpath configurations.doclava.resolve()
372
373 if (project.hasProperty("fromApi")) {
374 // Use an explicit API file.
375 inputApiFile = new File(project.rootDir, "api/${fromApi}.txt")
376 } else if (project.hasProperty("toApi") && toApi.matches(~/(\d+\.){2}\d+/)) {
377 // If toApi matches released API (X.Y.Z) format, use the most recently
378 // released API file prior to toApi.
379 inputApiFile = getReleasedApiFile(toApi)
380 } else {
381 // Use the most recently released API file.
382 inputApiFile = getReleasedApiFile();
383 }
384
385 int lastDot = inputApiFile.name.lastIndexOf('.')
386 outputApiXmlFile = new File(project.docsDir,
387 "release/" + inputApiFile.name.substring(0, lastDot) + ".xml")
388}
389
390/**
391 * Generates API diffs.
392 * <p>
393 * By default, diffs are generated for the delta between current.txt and the
394 * next most recent X.Y.Z.txt API file. Behavior may be changed by specifying
Alan Viverette6e8741c2017-01-25 10:44:07 -0500395 * one or both of -PtoApi and -PfromApi.
Alan Viverette9290dcd2016-12-15 17:29:58 -0500396 * <p>
397 * If both fromApi and toApi are specified, diffs will be generated for
398 * fromApi -> toApi. For example, 25.0.0 -> 26.0.0 diffs could be generated by
399 * using:
400 * <br><code>
Alan Viverette6e8741c2017-01-25 10:44:07 -0500401 * ./gradlew generateDiffs -PfromApi=25.0.0 -PtoApi=26.0.0
Alan Viverette9290dcd2016-12-15 17:29:58 -0500402 * </code>
403 * <p>
404 * If only toApi is specified, it MUST be specified as X.Y.Z and diffs will be
405 * generated for (release before toApi) -> toApi. For example, 24.2.0 -> 25.0.0
406 * diffs could be generated by using:
407 * <br><code>
Alan Viverette6e8741c2017-01-25 10:44:07 -0500408 * ./gradlew generateDiffs -PtoApi=25.0.0
Alan Viverette9290dcd2016-12-15 17:29:58 -0500409 * </code>
410 * <p>
411 * If only fromApi is specified, diffs will be generated for fromApi -> current.
412 * For example, lastApiReview -> current diffs could be generated by using:
413 * <br><code>
Alan Viverette6e8741c2017-01-25 10:44:07 -0500414 * ./gradlew generateDiffs -PfromApi=lastApiReview
Alan Viverette9290dcd2016-12-15 17:29:58 -0500415 * </code>
416 * <p>
417 */
418task generateDiffs(type: JDiffTask, dependsOn: [configurations.jdiff, configurations.doclava,
Alan Viverette6e8741c2017-01-25 10:44:07 -0500419 oldApiXml, newApiXml, generateDocs]) {
Alan Viverette9290dcd2016-12-15 17:29:58 -0500420 // Base classpath is Android SDK, sub-projects add their own.
421 classpath = project.ext.androidJar
422
423 // JDiff properties.
424 oldApiXmlFile = oldApiXml.outputApiXmlFile
425 newApiXmlFile = newApiXml.outputApiXmlFile
426 newJavadocPrefix = "../../../../reference/"
427
428 String newApi = newApiXmlFile.name
429 int lastDot = newApi.lastIndexOf('.')
430 newApi = newApi.substring(0, lastDot)
431
432 // Javadoc properties.
433 docletpath = configurations.jdiff.resolve()
Alan Viverette6e8741c2017-01-25 10:44:07 -0500434 destinationDir = new File(project.docsDir, "online/sdk/support_api_diff/$newApi")
Alan Viverette9290dcd2016-12-15 17:29:58 -0500435 title = "Support&nbsp;Library&nbsp;API&nbsp;Differences&nbsp;Report"
436
437 exclude '**/BuildConfig.java'
438 exclude '**/R.java'
439}
440
441/**
442 * Returns the most recently released API, optionally restricting to APIs
443 * before <code>beforeApi</code>.
444 *
445 * @param beforeApi the API to find an API file before, ex. 25.0.0
446 * @return the most recently released API file
447 */
448File getReleasedApiFile(String beforeApi = null) {
449 String beforeApiFileName = beforeApi != null ? beforeApi + ".txt" : null
450 File lastReleasedApiFile = null
451 File apiDir = new File(project.rootDir, 'api')
452
453 apiDir.eachFileMatch FileType.FILES, ~/(\d+\.){3}txt/, { File apiFile ->
454 // Is the current API file newer than the last one we saw?
455 if (lastReleasedApiFile == null || apiFile.name > lastReleasedApiFile.name) {
456 // Is the current API file older than the "before" API?
457 if (beforeApiFileName == null || apiFile.name < beforeApiFileName) {
458 lastReleasedApiFile = apiFile
459 }
460 }
461 }
462
463 return lastReleasedApiFile
464}
465
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -0800466subprojects {
Alan Viverette50323732016-12-02 11:18:03 -0500467 // Only modify Android projects.
Alan Viverette9290dcd2016-12-15 17:29:58 -0500468 if (project.name.equals('doclava') || project.name.equals('jdiff')) return;
Alan Viverettecc5197e2016-06-13 12:45:07 -0400469
Alan Viverette50323732016-12-02 11:18:03 -0500470 // Current SDK is set in studioCompat.gradle.
Yigit Boyar3986e042016-02-08 18:31:38 -0800471 project.ext.currentSdk = gradle.ext.currentSdk
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -0800472 apply plugin: 'maven'
Yigit Boyar3986e042016-02-08 18:31:38 -0800473
Xavier Ducrohet855a9222014-01-02 19:00:43 -0800474 version = rootProject.ext.supportVersion
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -0800475 group = 'com.android.support'
476
Yigit Boyarbe7a54a2015-04-07 13:23:50 -0700477 repositories {
478 maven { url "${project.parent.projectDir}/../../prebuilts/tools/common/m2/repository" }
479 maven { url "${project.parent.projectDir}/../../prebuilts/tools/common/m2/internal" }
480 maven { url "${project.parent.projectDir}/../../prebuilts/maven_repo/android" }
481 }
482
Jeff Davidson84faec52014-06-18 09:10:36 -0700483 project.plugins.whenPluginAdded { plugin ->
Alan Viverette50323732016-12-02 11:18:03 -0500484 def isAndroidLibrary = "com.android.build.gradle.LibraryPlugin".equals(plugin.class.name)
485 def isAndroidApp = "com.android.build.gradle.AppPlugin".equals(plugin.class.name)
486 def isJavaLibrary = "org.gradle.api.plugins.JavaPlugin".equals(plugin.class.name)
487
488 if (isAndroidLibrary || isAndroidApp) {
Jeff Davidson84faec52014-06-18 09:10:36 -0700489 project.android.buildToolsVersion = rootProject.buildToolsVersion
Alan Viverette50323732016-12-02 11:18:03 -0500490
491 // Enable code coverage for debug builds only if we are not running inside the IDE,
492 // since enabling coverage reports breaks the method parameter resolution in the IDE
493 // debugger.
Yigit Boyard8d42d52016-04-12 18:20:18 -0700494 project.android.buildTypes.debug.testCoverageEnabled = !hasProperty('android.injected.invoked.from.ide')
Alan Viveretteaf6b2512016-11-16 17:33:10 -0500495
Shankhoneer Chakrovarty2e6107d2016-10-17 17:39:35 -0700496 // Copy the class files in a jar to be later used to generate code coverage report
497 project.android.testVariants.all { v ->
498 // check if the variant has any source files
499 // and test coverage is enabled
500 if (v.buildType.testCoverageEnabled
501 && v.sourceSets.any { !it.java.sourceFiles.isEmpty() }) {
502 def jarifyTask = project.tasks.create(
503 name: "package${v.name.capitalize()}ClassFilesForCoverageReport",
504 type: Jar) {
Shankhoneer Chakrovarty719c3232016-11-23 15:39:01 -0800505 from v.testedVariant.javaCompile.destinationDir
Shankhoneer Chakrovarty00565342017-01-03 17:34:35 -0800506 exclude "**/R.class"
507 exclude "**/R\$*.class"
Aurimas Liutikas91d26042017-02-06 16:49:36 -0800508 exclude "**/BuildConfig.class"
Shankhoneer Chakrovarty6e45f612016-11-14 16:28:31 -0800509 destinationDir file(project.distDir)
510 archiveName "${project.archivesBaseName}-${v.baseName}-allclasses.jar"
Shankhoneer Chakrovarty2e6107d2016-10-17 17:39:35 -0700511 }
Shankhoneer Chakrovarty6e45f612016-11-14 16:28:31 -0800512 def jacocoAntConfig =
513 project.configurations[JacocoPlugin.ANT_CONFIGURATION_NAME]
514 def jacocoAntArtifacts = jacocoAntConfig.resolvedConfiguration.resolvedArtifacts
515 def version = jacocoAntArtifacts.find { "org.jacoco.ant".equals(it.name) }
516 .moduleVersion.id.version
517 def collectJacocoAntPackages = project.tasks.create(
518 name: "collectJacocoAntPackages",
519 type: Jar) {
520 from (jacocoAntArtifacts.collect { zipTree(it.getFile()) }) {
521 // exclude all the signatures the jar might have
522 exclude "META-INF/*.SF"
523 exclude "META-INF/*.DSA"
524 exclude "META-INF/*.RSA"
525 }
526 destinationDir file(project.distDir)
527 archiveName "jacocoant-" + version + ".jar"
Shankhoneer Chakrovarty2e6107d2016-10-17 17:39:35 -0700528 }
Aurimas Liutikas91d26042017-02-06 16:49:36 -0800529 jarifyTask.dependsOn v.getJavaCompiler()
Shankhoneer Chakrovarty6e45f612016-11-14 16:28:31 -0800530 v.assemble.dependsOn jarifyTask, collectJacocoAntPackages
Shankhoneer Chakrovarty2e6107d2016-10-17 17:39:35 -0700531 }
532 }
Alan Viverettee7589642016-11-16 18:18:36 -0500533
Alan Viverette50323732016-12-02 11:18:03 -0500534 // Enforce NewApi lint check as fatal.
Alan Viveretteaf6b2512016-11-16 17:33:10 -0500535 project.android.lintOptions.check 'NewApi'
536 project.android.lintOptions.fatal 'NewApi'
537 project.parent.lint.dependsOn project.lint
Jeff Davidson84faec52014-06-18 09:10:36 -0700538 }
Alan Viverette573630e2016-07-08 17:17:47 -0400539
Alan Viverette50323732016-12-02 11:18:03 -0500540 if (isAndroidLibrary || isJavaLibrary) {
541 // Add library to the aggregate dependency report.
542 task allDeps(type: DependencyReportTask) {}
543
544 // Create release and separate zip task for library.
Alan Viverette573630e2016-07-08 17:17:47 -0400545 task release(type: Upload) {
546 configuration = configurations.archives
547 repositories {
548 mavenDeployer {
549 repository(url: uri("$rootProject.ext.supportRepoOut"))
550
551 // Disable unique names for SNAPSHOTS so they can be updated in place.
552 setUniqueVersion(false)
553 doLast {
554 // Remove any invalid maven-metadata.xml files that may have been
555 // created for SNAPSHOT versions that are *not* uniquely versioned.
556 pom*.each { pom ->
557 if (pom.version.endsWith('-SNAPSHOT')) {
558 final File artifactDir = new File(
559 rootProject.ext.supportRepoOut,
560 pom.groupId.replace('.', '/')
561 + '/' + pom.artifactId
562 + '/' + pom.version)
563 delete fileTree(dir: artifactDir,
564 include: 'maven-metadata.xml*')
565 }
566 }
567 }
568 }
569 }
570 }
571
572 def deployer = release.repositories.mavenDeployer
573 deployer.pom*.whenConfigured { pom ->
574 pom.dependencies.findAll { dep ->
575 dep.groupId == 'com.android.support' && dep.artifactId != 'support-annotations'
576 }*.type = 'aar'
577 }
578
579 ext.versionDir = {
580 def groupDir = new File(rootProject.ext.supportRepoOut,
581 project.group.replace('.','/'))
582 def artifactDir = new File(groupDir, archivesBaseName)
583 return new File(artifactDir, version)
584 }
585
Alan Viverette9290dcd2016-12-15 17:29:58 -0500586 task generateSourceProps(dependsOn: createRepository)
587 generateSourceProps.doLast({
Alan Viverette573630e2016-07-08 17:17:47 -0400588 def content = "Maven.GroupId=$deployer.pom.groupId\n" +
589 "Maven.ArtifactId=$deployer.pom.artifactId\n" +
590 "Maven.Version=$deployer.pom.version\n" +
Alan Viverette4453f0d2016-09-14 12:32:27 -0700591 "Extra.VendorDisplay=Android\n" +
592 "Extra.VendorId=android\n" +
Alan Viverette573630e2016-07-08 17:17:47 -0400593 "Pkg.Desc=$project.name\n" +
594 "Pkg.Revision=1\n" +
Alan Viverette4453f0d2016-09-14 12:32:27 -0700595 "Maven.Dependencies=" +
Alan Viverette573630e2016-07-08 17:17:47 -0400596 String.join(",", project.configurations.compile.allDependencies.collect {
597 def p = parent.findProject(it.name)
598 return p ? "$p.group:$p.archivesBaseName:$p.version" : null
599 }.grep()) +
600 "\n"
601 Files.write(content, new File(versionDir(), 'source.properties'), Charsets.UTF_8)
Alan Viverette9290dcd2016-12-15 17:29:58 -0500602 })
Alan Viverette573630e2016-07-08 17:17:47 -0400603
604 task createSeparateZip(type: Zip, dependsOn: generateSourceProps) {
605 into archivesBaseName
606 destinationDir project.parent.ext.distDir
607 baseName = project.group
608 version = project.parent.ext.buildNumber
609 }
610 project.parent.createArchive.dependsOn createSeparateZip
611
Alan Viverette50323732016-12-02 11:18:03 -0500612 // Before the upload, make sure the repo is ready.
Alan Viverette573630e2016-07-08 17:17:47 -0400613 release.dependsOn rootProject.tasks.prepareRepo
Alan Viverette50323732016-12-02 11:18:03 -0500614
615 // Make the mainupload depend on this one.
Alan Viverette573630e2016-07-08 17:17:47 -0400616 mainUpload.dependsOn release
617 }
Jeff Davidson84faec52014-06-18 09:10:36 -0700618 }
Chris Banesdaea0692015-12-29 12:48:24 +0000619
Chris Banesdaea0692015-12-29 12:48:24 +0000620 project.afterEvaluate {
Aurimas Liutikas11670682017-02-14 23:21:54 -0800621 // The archivesBaseName isn't available initially, so set it now
Alan Viverette573630e2016-07-08 17:17:47 -0400622 def createZipTask = project.tasks.findByName("createSeparateZip")
623 if (createZipTask != null) {
624 createZipTask.appendix = archivesBaseName
625 createZipTask.from versionDir()
626 }
Joe Baker-Malone5e2c51d2016-05-13 15:09:24 -0700627
628 // Copy instrumentation test APK into the dist dir
Chris Banesdaea0692015-12-29 12:48:24 +0000629 def assembleTestTask = project.tasks.findByPath('assembleAndroidTest')
630 if (assembleTestTask != null) {
631 assembleTestTask.doLast {
632 // If the project actually has some instrumentation tests, copy its APK
633 if (!project.android.sourceSets.androidTest.java.sourceFiles.isEmpty()) {
634 def pkgTask = project.tasks.findByPath('packageDebugAndroidTest')
635 copy {
636 from(pkgTask.outputFile)
637 into(rootProject.ext.testApkDistOut)
638 }
639 }
640 }
641 }
642 }
Yigit Boyarea5d9b22016-03-08 13:25:26 -0800643
644 project.afterEvaluate { p ->
645 // remove dependency on the test so that we still get coverage even if some tests fail
646 p.tasks.findAll { it instanceof JacocoReportTask}.each { task ->
647 def toBeRemoved = new ArrayList()
648 def dependencyList = task.taskDependencies.values
649 dependencyList.each { dep ->
650 if (dep instanceof String) {
651 def t = tasks.findByName(dep)
652 if (t instanceof DeviceProviderInstrumentTestTask) {
653 toBeRemoved.add(dep)
654 task.mustRunAfter(t)
655 }
656 }
657 }
658 toBeRemoved.each { dep ->
659 dependencyList.remove(dep)
660 }
661 }
662 }
Alan Viverettecc5197e2016-06-13 12:45:07 -0400663
664 project.afterEvaluate { p ->
665 if (p.hasProperty('android')
666 && p.android.hasProperty('libraryVariants')
667 && !(p.android.hasProperty('noDocs') && p.android.noDocs)) {
668 p.android.libraryVariants.all { v ->
669 if (v.name == 'release') {
Alan Viverette9562a3b2016-07-01 13:26:39 -0400670 registerForDocsTask(rootProject.generateDocs, p, v)
671 registerForDocsTask(rootProject.generateApi, p, v)
Alan Viverette9290dcd2016-12-15 17:29:58 -0500672 registerForDocsTask(rootProject.generateDiffs, p, v)
Alan Viverettecc5197e2016-06-13 12:45:07 -0400673 }
674 }
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}