blob: 7871a0be46d87d4302d13f7badc5d01d281501a5 [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
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -070022
Yigit Boyar88c16ce2017-02-08 16:06:14 -080023import org.gradle.internal.os.OperatingSystem
24
25def supportRoot = ext.supportRootFolder
26if (supportRoot == null) {
27 throw new RuntimeException("variable supportRootFolder is not set. you must set it before" +
28 " including this script")
29}
30def init = new Properties()
31ext.init = init
Alan Viverette1b862372017-03-22 11:32:28 -040032ext.init.debugKeystore = file("${supportRoot}/development/keystore/debug.keystore")
Yigit Boyar88c16ce2017-02-08 16:06:14 -080033
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -070034ext.runningInBuildServer = System.env.DIST_DIR != null && System.env.OUT_DIR != null
Yigit Boyar88c16ce2017-02-08 16:06:14 -080035
36apply from: "${supportRoot}/buildSrc/dependencies.gradle"
Yigit Boyar63e6c672017-03-31 13:32:17 -070037ext.buildOfflineDocs = rootProject.getProperties().containsKey("offlineDocs")
Yigit Boyar88c16ce2017-02-08 16:06:14 -080038
39def loadDefaultVersions() {
40 apply from: "${supportRootFolder}/buildSrc/versions.gradle"
41}
42
Yigit Boyar88c16ce2017-02-08 16:06:14 -080043def enableDoclavaAndJDiff(p) {
44 p.configurations {
45 doclava
46 jdiff
47 }
48
49 p.dependencies {
50 doclava project(':doclava')
51 jdiff project(':jdiff')
52 jdiff libs.xml_parser_apis
53 jdiff libs.xerces_impl
54 }
55 apply from: "${ext.supportRootFolder}/buildSrc/diff_and_docs.gradle"
56}
57
58def setSdkInLocalPropertiesFile() {
59 final String platform = OperatingSystem.current().isMacOsX() ? 'darwin' : 'linux'
60 System.setProperty('android.dir', "${supportRootFolder}/../../")
Aurimas Liutikasd2d9bda2017-03-03 16:06:39 -080061 ext.buildToolsVersion = '26.0.0'
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -070062 final String fullSdkPath = "${repos.prebuiltsRoot}/fullsdk-${platform}"
Yigit Boyar88c16ce2017-02-08 16:06:14 -080063 if (file(fullSdkPath).exists()) {
64 gradle.ext.currentSdk = 26
Alan Viveretteeb9f3322017-03-09 16:29:57 -050065 project.ext.androidJar =
66 files("${fullSdkPath}/platforms/android-${gradle.currentSdk}/android.jar")
67 project.ext.androidSrcJar =
68 file("${fullSdkPath}/platforms/android-${gradle.currentSdk}/android-stubs-src.jar")
Alan Viverette810a1482017-03-20 12:43:43 -040069 project.ext.androidApiTxt = null
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -070070 System.setProperty('android.home', "${repos.prebuiltsRoot}/fullsdk-${platform}")
Yigit Boyar88c16ce2017-02-08 16:06:14 -080071 File props = file("local.properties")
72 props.write "sdk.dir=${fullSdkPath}"
Aurimas Liutikas3c666002017-03-08 19:30:28 -080073 ext.usingFullSdk = true
Yigit Boyar88c16ce2017-02-08 16:06:14 -080074 } else {
75 gradle.ext.currentSdk = 'current'
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -070076 project.ext.androidJar = files("${repos.prebuiltsRoot}/sdk/current/android.jar")
Alan Viverette810a1482017-03-20 12:43:43 -040077 project.ext.androidSrcJar = null
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -070078 project.ext.androidApiTxt = file("${repos.prebuiltsRoot}/sdk/api/26.txt")
Yigit Boyar88c16ce2017-02-08 16:06:14 -080079 File props = file("local.properties")
80 props.write "android.dir=../../"
Aurimas Liutikas3c666002017-03-08 19:30:28 -080081 ext.usingFullSdk = false
Yigit Boyar88c16ce2017-02-08 16:06:14 -080082 }
83}
84
85def setupRepoOutAndBuildNumber() {
86 ext.supportRepoOut = ''
87 ext.buildNumber = Integer.toString(ext.extraVersion)
88
89 /*
90 * With the build server you are given two env variables.
91 * The OUT_DIR is a temporary directory you can use to put things during the build.
92 * The DIST_DIR is where you want to save things from the build.
93 *
94 * The build server will copy the contents of DIST_DIR to somewhere and make it available.
95 */
Yigit Boyar7bfacb72017-03-02 14:27:41 -080096 if (ext.runningInBuildServer) {
Yigit Boyar88c16ce2017-02-08 16:06:14 -080097 buildDir = new File(System.env.OUT_DIR + '/gradle/frameworks/support/build')
98 .getCanonicalFile()
99 project.ext.distDir = new File(System.env.DIST_DIR).getCanonicalFile()
100
101 // the build server does not pass the build number so we infer it from the last folder of
102 // the dist path.
103 ext.buildNumber = project.ext.distDir.getName()
104 } else {
105 buildDir = file("${ext.supportRootFolder}/../../out/host/gradle/frameworks/support/build")
106 project.ext.distDir = new File("${ext.supportRootFolder}/../../out/dist")
107 }
108 subprojects {
109 // Change buildDir first so that all plugins pick up the new value.
110 project.buildDir = new File("$project.parent.buildDir/../$project.name/build")
111 }
112 ext.supportRepoOut = new File(buildDir, 'support_repo')
113 ext.testApkDistOut = ext.distDir
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800114 ext.testResultsDistDir = new File(distDir, "host-test-reports")
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800115 ext.docsDir = new File(buildDir, 'javadoc')
116}
117
118def configureSubProjects() {
119 // lint every library
120 def lintTask = project.tasks.create("lint")
121 subprojects {
122 // Only modify Android projects.
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800123 if (project.name.equals('doclava') || project.name.equals('jdiff')) {
124 // disable tests and return
125 project.tasks.whenTaskAdded { task ->
126 if (task instanceof org.gradle.api.tasks.testing.Test) {
127 task.enabled = false
128 }
129 }
130 return
131 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800132
133 // Current SDK is set in studioCompat.gradle.
134 project.ext.currentSdk = gradle.ext.currentSdk
135 apply plugin: 'maven'
136
137 version = rootProject.ext.supportVersion
138 group = 'com.android.support'
139
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -0700140 repos.addMavenRepositories(repositories)
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800141 project.plugins.whenPluginAdded { plugin ->
142 def isAndroidLibrary = "com.android.build.gradle.LibraryPlugin"
143 .equals(plugin.class.name)
144 def isAndroidApp = "com.android.build.gradle.AppPlugin".equals(plugin.class.name)
145 def isJavaLibrary = "org.gradle.api.plugins.JavaPlugin".equals(plugin.class.name)
146
147 if (isAndroidLibrary || isAndroidApp) {
148 project.android.buildToolsVersion = rootProject.buildToolsVersion
149
150 // Enable code coverage for debug builds only if we are not running inside the IDE,
151 // since enabling coverage reports breaks the method parameter resolution in the IDE
152 // debugger.
153 project.android.buildTypes.debug.testCoverageEnabled =
154 !hasProperty('android.injected.invoked.from.ide')
155
156 // Copy the class files in a jar to be later used to generate code coverage report
157 project.android.testVariants.all { v ->
158 // check if the variant has any source files
159 // and test coverage is enabled
160 if (v.buildType.testCoverageEnabled
161 && v.sourceSets.any { !it.java.sourceFiles.isEmpty() }) {
162 def jarifyTask = project.tasks.create(
163 name: "package${v.name.capitalize()}ClassFilesForCoverageReport",
164 type: Jar) {
165 from v.testedVariant.javaCompile.destinationDir
166 exclude "**/R.class"
167 exclude "**/R\$*.class"
168 exclude "**/BuildConfig.class"
169 destinationDir file(project.distDir)
170 archiveName "${project.archivesBaseName}-${v.baseName}-allclasses.jar"
171 }
172 def jacocoAntConfig =
173 project.configurations[JacocoPlugin.ANT_CONFIGURATION_NAME]
174 def jacocoAntArtifacts = jacocoAntConfig.resolvedConfiguration
175 .resolvedArtifacts
176 def version = jacocoAntArtifacts.find { "org.jacoco.ant".equals(it.name) }
177 .moduleVersion.id.version
178 def collectJacocoAntPackages = project.tasks.create(
179 name: "collectJacocoAntPackages",
180 type: Jar) {
181 from(jacocoAntArtifacts.collect { zipTree(it.getFile()) }) {
182 // exclude all the signatures the jar might have
183 exclude "META-INF/*.SF"
184 exclude "META-INF/*.DSA"
185 exclude "META-INF/*.RSA"
186 }
187 destinationDir file(project.distDir)
188 archiveName "jacocoant-" + version + ".jar"
189 }
190 jarifyTask.dependsOn v.getJavaCompiler()
191 v.assemble.dependsOn jarifyTask, collectJacocoAntPackages
192 }
193 }
194
195 // Enforce NewApi lint check as fatal.
196 project.android.lintOptions.check 'NewApi'
197 project.android.lintOptions.fatal 'NewApi'
198 lintTask.dependsOn project.lint
199 }
200
201 if (isAndroidLibrary || isJavaLibrary) {
202 // Add library to the aggregate dependency report.
203 task allDeps(type: DependencyReportTask) {}
204
205 // Create release and separate zip task for library.
206 task release(type: Upload) {
207 configuration = configurations.archives
208 repositories {
209 mavenDeployer {
210 repository(url: uri("$rootProject.ext.supportRepoOut"))
211
212 // Disable unique names for SNAPSHOTS so they can be updated in place.
213 setUniqueVersion(false)
214 doLast {
215 // Remove any invalid maven-metadata.xml files that may have been
216 // created for SNAPSHOT versions that are *not* uniquely versioned.
217 pom*.each { pom ->
218 if (pom.version.endsWith('-SNAPSHOT')) {
219 final File artifactDir = new File(
220 rootProject.ext.supportRepoOut,
221 pom.groupId.replace('.', '/')
222 + '/' + pom.artifactId
223 + '/' + pom.version)
224 delete fileTree(dir: artifactDir,
225 include: 'maven-metadata.xml*')
226 }
227 }
228 }
229 }
230 }
231 }
232
233 def deployer = release.repositories.mavenDeployer
234 deployer.pom*.whenConfigured { pom ->
235 pom.dependencies.findAll { dep ->
236 dep.groupId == 'com.android.support' &&
237 dep.artifactId != 'support-annotations'
238 }*.type = 'aar'
239 }
240
241 ext.versionDir = {
242 def groupDir = new File(rootProject.ext.supportRepoOut,
243 project.group.replace('.', '/'))
244 def artifactDir = new File(groupDir, archivesBaseName)
245 return new File(artifactDir, version)
246 }
247
248 task generateSourceProps(dependsOn: createRepository)
249 generateSourceProps.doLast({
250 def content = "Maven.GroupId=$deployer.pom.groupId\n" +
251 "Maven.ArtifactId=$deployer.pom.artifactId\n" +
252 "Maven.Version=$deployer.pom.version\n" +
253 "Extra.VendorDisplay=Android\n" +
254 "Extra.VendorId=android\n" +
255 "Pkg.Desc=$project.name\n" +
256 "Pkg.Revision=1\n" +
257 "Maven.Dependencies=" +
258 String.join(",", project.configurations.compile.allDependencies
259 .collect {
260 def p = parent.findProject(it.name)
261 return p ? "$p.group:$p.archivesBaseName:$p.version" : null
262 }.grep()) +
263 "\n"
264 Files.write(content, new File(versionDir(), 'source.properties'),
265 Charsets.UTF_8)
266 })
267
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800268 // Before the upload, make sure the repo is ready.
269 release.dependsOn rootProject.tasks.prepareRepo
270
271 // Make the mainupload depend on this one.
272 mainUpload.dependsOn release
273 }
274 }
275
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800276 // Copy instrumentation test APKs and app APKs into the dist dir
277 // For test apks, they are uploaded only if we have java test sources.
278 // For regular app apks, they are uploaded only if they have java sources.
279 project.tasks.whenTaskAdded { task ->
280 if (task.name.startsWith("packageDebug")) {
281 def testApk = task.name.contains("AndroidTest")
282 task.doLast {
283 def source = testApk ? project.android.sourceSets.androidTest
284 : project.android.sourceSets.main
285 if (task.hasProperty("outputFile") && !source.java.sourceFiles.isEmpty()) {
286 copy {
287 from(task.outputFile)
288 into(rootProject.ext.testApkDistOut)
289 rename { String fileName ->
290 // multiple modules may have the same name so prefix the name with
291 // the module's path to ensure it is unique.
292 // e.g. palette-v7-debug-androidTest.apk becomes
293 // support-palette-v7_palette-v7-debug-androidTest.apk
294 "${project.getPath().replace(':', '-').substring(1)}_${fileName}"
295 }
296 }
297 }
298 }
299 }
300 }
301
302 // copy host side test results to DIST
303 project.tasks.whenTaskAdded { task ->
304 if (task instanceof org.gradle.api.tasks.testing.Test) {
305 def junitReport = task.reports.junitXml
306 if (junitReport.enabled) {
307 def zipTask = project.tasks.create(name : "zipResultsOf${task.name.capitalize()}", type : Zip) {
308 destinationDir(testResultsDistDir)
Yigit Boyar278676d2017-03-10 15:29:11 -0800309 // first one is always :, drop it.
310 archiveName("${project.getPath().split(":").join("_").substring(1)}.zip")
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800311 }
312 if (project.rootProject.ext.runningInBuildServer) {
313 task.ignoreFailures = true
314 }
315 task.finalizedBy zipTask
316 task.doFirst {
317 zipTask.from(junitReport.destination)
318 }
319 }
320 }
321 }
322
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800323 project.afterEvaluate {
324 // The archivesBaseName isn't available initially, so set it now
325 def createZipTask = project.tasks.findByName("createSeparateZip")
326 if (createZipTask != null) {
327 createZipTask.appendix = archivesBaseName
328 createZipTask.from versionDir()
329 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800330 }
331
332 project.afterEvaluate { p ->
333 // remove dependency on the test so that we still get coverage even if some tests fail
334 p.tasks.findAll { it instanceof JacocoReportTask }.each { task ->
335 def toBeRemoved = new ArrayList()
336 def dependencyList = task.taskDependencies.values
337 dependencyList.each { dep ->
338 if (dep instanceof String) {
339 def t = tasks.findByName(dep)
340 if (t instanceof DeviceProviderInstrumentTestTask) {
341 toBeRemoved.add(dep)
342 task.mustRunAfter(t)
343 }
344 }
345 }
346 toBeRemoved.each { dep ->
347 dependencyList.remove(dep)
348 }
349 }
350 }
351 }
352}
353
354def setupRelease() {
355 apply from: "${ext.supportRootFolder}/buildSrc/release.gradle"
356}
357
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800358ext.init.enableDoclavaAndJDiff = this.&enableDoclavaAndJDiff
359ext.init.setSdkInLocalPropertiesFile = this.&setSdkInLocalPropertiesFile
360ext.init.setupRepoOutAndBuildNumber = this.&setupRepoOutAndBuildNumber
361ext.init.setupRelease = this.&setupRelease
362ext.init.loadDefaultVersions = this.&loadDefaultVersions
363ext.init.configureSubProjects = this.&configureSubProjects