blob: 5149a0a6582ae8b970767e7ce53e61512832f9d3 [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
Aurimas Liutikasa0150042017-04-21 09:46:12 -070023import org.gradle.api.logging.configuration.ShowStacktrace
Yigit Boyar88c16ce2017-02-08 16:06:14 -080024
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"
Alan Viverette5b3946f2017-04-27 09:59:22 -040037ext.docs = [:]
38ext.docs.offline = rootProject.getProperties().containsKey("offlineDocs")
39ext.docs.dac = [
40 libraryroot: "android/support",
41 dataname: "SUPPORT_DATA"
42]
Yigit Boyar88c16ce2017-02-08 16:06:14 -080043
44def loadDefaultVersions() {
45 apply from: "${supportRootFolder}/buildSrc/versions.gradle"
46}
47
Yigit Boyar88c16ce2017-02-08 16:06:14 -080048def enableDoclavaAndJDiff(p) {
49 p.configurations {
50 doclava
51 jdiff
52 }
53
54 p.dependencies {
55 doclava project(':doclava')
56 jdiff project(':jdiff')
57 jdiff libs.xml_parser_apis
58 jdiff libs.xerces_impl
59 }
60 apply from: "${ext.supportRootFolder}/buildSrc/diff_and_docs.gradle"
61}
62
63def setSdkInLocalPropertiesFile() {
Aurimas Liutikasb06771d2017-04-20 09:46:49 -070064 final String osName = System.getProperty("os.name").toLowerCase();
65 final boolean isMacOsX =
66 osName.contains("mac os x") || osName.contains("darwin") || osName.contains("osx");
67 final String platform = isMacOsX ? 'darwin' : 'linux'
Yigit Boyar88c16ce2017-02-08 16:06:14 -080068 System.setProperty('android.dir', "${supportRootFolder}/../../")
Aurimas Liutikasd2d9bda2017-03-03 16:06:39 -080069 ext.buildToolsVersion = '26.0.0'
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -070070 final String fullSdkPath = "${repos.prebuiltsRoot}/fullsdk-${platform}"
Yigit Boyar88c16ce2017-02-08 16:06:14 -080071 if (file(fullSdkPath).exists()) {
72 gradle.ext.currentSdk = 26
Alan Viveretteeb9f3322017-03-09 16:29:57 -050073 project.ext.androidJar =
74 files("${fullSdkPath}/platforms/android-${gradle.currentSdk}/android.jar")
75 project.ext.androidSrcJar =
76 file("${fullSdkPath}/platforms/android-${gradle.currentSdk}/android-stubs-src.jar")
Alan Viverette810a1482017-03-20 12:43:43 -040077 project.ext.androidApiTxt = null
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -070078 System.setProperty('android.home', "${repos.prebuiltsRoot}/fullsdk-${platform}")
Yigit Boyar88c16ce2017-02-08 16:06:14 -080079 File props = file("local.properties")
80 props.write "sdk.dir=${fullSdkPath}"
Aurimas Liutikas3c666002017-03-08 19:30:28 -080081 ext.usingFullSdk = true
Yigit Boyar88c16ce2017-02-08 16:06:14 -080082 } else {
83 gradle.ext.currentSdk = 'current'
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -070084 project.ext.androidJar = files("${repos.prebuiltsRoot}/sdk/current/android.jar")
Alan Viverette810a1482017-03-20 12:43:43 -040085 project.ext.androidSrcJar = null
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -070086 project.ext.androidApiTxt = file("${repos.prebuiltsRoot}/sdk/api/26.txt")
Yigit Boyar88c16ce2017-02-08 16:06:14 -080087 File props = file("local.properties")
88 props.write "android.dir=../../"
Aurimas Liutikas3c666002017-03-08 19:30:28 -080089 ext.usingFullSdk = false
Yigit Boyar88c16ce2017-02-08 16:06:14 -080090 }
91}
92
93def setupRepoOutAndBuildNumber() {
94 ext.supportRepoOut = ''
95 ext.buildNumber = Integer.toString(ext.extraVersion)
96
97 /*
98 * With the build server you are given two env variables.
99 * The OUT_DIR is a temporary directory you can use to put things during the build.
100 * The DIST_DIR is where you want to save things from the build.
101 *
102 * The build server will copy the contents of DIST_DIR to somewhere and make it available.
103 */
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800104 if (ext.runningInBuildServer) {
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800105 buildDir = new File(System.env.OUT_DIR + '/gradle/frameworks/support/build')
106 .getCanonicalFile()
107 project.ext.distDir = new File(System.env.DIST_DIR).getCanonicalFile()
108
109 // the build server does not pass the build number so we infer it from the last folder of
110 // the dist path.
111 ext.buildNumber = project.ext.distDir.getName()
Aurimas Liutikasa0150042017-04-21 09:46:12 -0700112
113 // the build server should always print out full stack traces for any failures.
114 gradle.startParameter.showStacktrace = ShowStacktrace.ALWAYS
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800115 } 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
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -0700151 repos.addMavenRepositories(repositories)
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800152 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 =
Sergey Vasilinetsac8e72b2017-04-26 15:55:17 -0700165 !project.hasProperty('android.injected.invoked.from.ide')
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800166
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
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800279 // Before the upload, make sure the repo is ready.
280 release.dependsOn rootProject.tasks.prepareRepo
281
282 // Make the mainupload depend on this one.
283 mainUpload.dependsOn release
284 }
285 }
286
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800287 // Copy instrumentation test APKs and app APKs into the dist dir
288 // For test apks, they are uploaded only if we have java test sources.
289 // For regular app apks, they are uploaded only if they have java sources.
290 project.tasks.whenTaskAdded { task ->
291 if (task.name.startsWith("packageDebug")) {
292 def testApk = task.name.contains("AndroidTest")
293 task.doLast {
294 def source = testApk ? project.android.sourceSets.androidTest
295 : project.android.sourceSets.main
296 if (task.hasProperty("outputFile") && !source.java.sourceFiles.isEmpty()) {
297 copy {
298 from(task.outputFile)
299 into(rootProject.ext.testApkDistOut)
300 rename { String fileName ->
301 // multiple modules may have the same name so prefix the name with
302 // the module's path to ensure it is unique.
303 // e.g. palette-v7-debug-androidTest.apk becomes
304 // support-palette-v7_palette-v7-debug-androidTest.apk
305 "${project.getPath().replace(':', '-').substring(1)}_${fileName}"
306 }
307 }
308 }
309 }
310 }
311 }
312
313 // copy host side test results to DIST
314 project.tasks.whenTaskAdded { task ->
315 if (task instanceof org.gradle.api.tasks.testing.Test) {
316 def junitReport = task.reports.junitXml
317 if (junitReport.enabled) {
318 def zipTask = project.tasks.create(name : "zipResultsOf${task.name.capitalize()}", type : Zip) {
319 destinationDir(testResultsDistDir)
Yigit Boyar278676d2017-03-10 15:29:11 -0800320 // first one is always :, drop it.
321 archiveName("${project.getPath().split(":").join("_").substring(1)}.zip")
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800322 }
323 if (project.rootProject.ext.runningInBuildServer) {
324 task.ignoreFailures = true
325 }
326 task.finalizedBy zipTask
327 task.doFirst {
328 zipTask.from(junitReport.destination)
329 }
330 }
331 }
332 }
333
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800334 project.afterEvaluate {
335 // The archivesBaseName isn't available initially, so set it now
336 def createZipTask = project.tasks.findByName("createSeparateZip")
337 if (createZipTask != null) {
338 createZipTask.appendix = archivesBaseName
339 createZipTask.from versionDir()
340 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800341 }
342
343 project.afterEvaluate { p ->
344 // remove dependency on the test so that we still get coverage even if some tests fail
345 p.tasks.findAll { it instanceof JacocoReportTask }.each { task ->
346 def toBeRemoved = new ArrayList()
347 def dependencyList = task.taskDependencies.values
348 dependencyList.each { dep ->
349 if (dep instanceof String) {
350 def t = tasks.findByName(dep)
351 if (t instanceof DeviceProviderInstrumentTestTask) {
352 toBeRemoved.add(dep)
353 task.mustRunAfter(t)
354 }
355 }
356 }
357 toBeRemoved.each { dep ->
358 dependencyList.remove(dep)
359 }
360 }
361 }
362 }
363}
364
365def setupRelease() {
366 apply from: "${ext.supportRootFolder}/buildSrc/release.gradle"
367}
368
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800369ext.init.enableDoclavaAndJDiff = this.&enableDoclavaAndJDiff
370ext.init.setSdkInLocalPropertiesFile = this.&setSdkInLocalPropertiesFile
371ext.init.setupRepoOutAndBuildNumber = this.&setupRepoOutAndBuildNumber
372ext.init.setupRelease = this.&setupRelease
373ext.init.loadDefaultVersions = this.&loadDefaultVersions
374ext.init.configureSubProjects = this.&configureSubProjects