blob: c56953f349b607e0e882e668be22940e2c3354c6 [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 {
Aurimas Liutikasabb471f2017-05-09 16:25:43 -0700133 repos.addMavenRepositories(repositories)
134
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800135 // Only modify Android projects.
Aurimas Liutikasabb471f2017-05-09 16:25:43 -0700136 if (project.name.equals('doclava')
137 || project.name.equals('jdiff')
138 || project.name.equals('support-testutils')
139 || project.name.equals('noto-emoji-compat')) {
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800140 // disable tests and return
141 project.tasks.whenTaskAdded { task ->
142 if (task instanceof org.gradle.api.tasks.testing.Test) {
143 task.enabled = false
144 }
145 }
146 return
147 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800148
149 // Current SDK is set in studioCompat.gradle.
150 project.ext.currentSdk = gradle.ext.currentSdk
151 apply plugin: 'maven'
152
153 version = rootProject.ext.supportVersion
154 group = 'com.android.support'
155
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800156 project.plugins.whenPluginAdded { plugin ->
157 def isAndroidLibrary = "com.android.build.gradle.LibraryPlugin"
158 .equals(plugin.class.name)
159 def isAndroidApp = "com.android.build.gradle.AppPlugin".equals(plugin.class.name)
160 def isJavaLibrary = "org.gradle.api.plugins.JavaPlugin".equals(plugin.class.name)
161
162 if (isAndroidLibrary || isAndroidApp) {
163 project.android.buildToolsVersion = rootProject.buildToolsVersion
164
165 // Enable code coverage for debug builds only if we are not running inside the IDE,
166 // since enabling coverage reports breaks the method parameter resolution in the IDE
167 // debugger.
168 project.android.buildTypes.debug.testCoverageEnabled =
Sergey Vasilinetsac8e72b2017-04-26 15:55:17 -0700169 !project.hasProperty('android.injected.invoked.from.ide')
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800170
171 // Copy the class files in a jar to be later used to generate code coverage report
172 project.android.testVariants.all { v ->
173 // check if the variant has any source files
174 // and test coverage is enabled
175 if (v.buildType.testCoverageEnabled
176 && v.sourceSets.any { !it.java.sourceFiles.isEmpty() }) {
177 def jarifyTask = project.tasks.create(
178 name: "package${v.name.capitalize()}ClassFilesForCoverageReport",
179 type: Jar) {
180 from v.testedVariant.javaCompile.destinationDir
181 exclude "**/R.class"
182 exclude "**/R\$*.class"
183 exclude "**/BuildConfig.class"
184 destinationDir file(project.distDir)
185 archiveName "${project.archivesBaseName}-${v.baseName}-allclasses.jar"
186 }
187 def jacocoAntConfig =
188 project.configurations[JacocoPlugin.ANT_CONFIGURATION_NAME]
189 def jacocoAntArtifacts = jacocoAntConfig.resolvedConfiguration
190 .resolvedArtifacts
191 def version = jacocoAntArtifacts.find { "org.jacoco.ant".equals(it.name) }
192 .moduleVersion.id.version
193 def collectJacocoAntPackages = project.tasks.create(
194 name: "collectJacocoAntPackages",
195 type: Jar) {
196 from(jacocoAntArtifacts.collect { zipTree(it.getFile()) }) {
197 // exclude all the signatures the jar might have
198 exclude "META-INF/*.SF"
199 exclude "META-INF/*.DSA"
200 exclude "META-INF/*.RSA"
201 }
202 destinationDir file(project.distDir)
203 archiveName "jacocoant-" + version + ".jar"
204 }
205 jarifyTask.dependsOn v.getJavaCompiler()
206 v.assemble.dependsOn jarifyTask, collectJacocoAntPackages
207 }
208 }
209
210 // Enforce NewApi lint check as fatal.
211 project.android.lintOptions.check 'NewApi'
212 project.android.lintOptions.fatal 'NewApi'
213 lintTask.dependsOn project.lint
214 }
215
216 if (isAndroidLibrary || isJavaLibrary) {
217 // Add library to the aggregate dependency report.
218 task allDeps(type: DependencyReportTask) {}
219
Aurimas Liutikasabb471f2017-05-09 16:25:43 -0700220 project.afterEvaluate {
221 Upload uploadTask = (Upload) project.tasks.uploadArchives;
222 uploadTask.repositories.mavenDeployer {
223 // Disable unique names for SNAPSHOTS so they can be updated in place.
224 setUniqueVersion(false)
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800225
Aurimas Liutikasabb471f2017-05-09 16:25:43 -0700226 // 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*')
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800237 }
238 }
239 }
Aurimas Liutikasabb471f2017-05-09 16:25:43 -0700240
241 uploadTask.repositories.mavenDeployer.pom*.whenConfigured { pom ->
242 pom.dependencies.findAll { dep ->
243 dep.groupId == 'com.android.support' &&
244 dep.artifactId != 'support-annotations'
245 }*.type = 'aar'
246 }
247
248 // Before the upload, make sure the repo is ready.
249 uploadTask.dependsOn rootProject.tasks.prepareRepo
250
251 // Make the mainupload depend on this one.
252 mainUpload.dependsOn uploadTask
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800253 }
254
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800255 }
256 }
257
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800258 // Copy instrumentation test APKs and app APKs into the dist dir
259 // For test apks, they are uploaded only if we have java test sources.
260 // For regular app apks, they are uploaded only if they have java sources.
261 project.tasks.whenTaskAdded { task ->
262 if (task.name.startsWith("packageDebug")) {
263 def testApk = task.name.contains("AndroidTest")
264 task.doLast {
265 def source = testApk ? project.android.sourceSets.androidTest
266 : project.android.sourceSets.main
267 if (task.hasProperty("outputFile") && !source.java.sourceFiles.isEmpty()) {
268 copy {
269 from(task.outputFile)
270 into(rootProject.ext.testApkDistOut)
271 rename { String fileName ->
272 // multiple modules may have the same name so prefix the name with
273 // the module's path to ensure it is unique.
274 // e.g. palette-v7-debug-androidTest.apk becomes
275 // support-palette-v7_palette-v7-debug-androidTest.apk
276 "${project.getPath().replace(':', '-').substring(1)}_${fileName}"
277 }
278 }
279 }
280 }
281 }
282 }
283
284 // copy host side test results to DIST
285 project.tasks.whenTaskAdded { task ->
286 if (task instanceof org.gradle.api.tasks.testing.Test) {
287 def junitReport = task.reports.junitXml
288 if (junitReport.enabled) {
289 def zipTask = project.tasks.create(name : "zipResultsOf${task.name.capitalize()}", type : Zip) {
290 destinationDir(testResultsDistDir)
Yigit Boyar278676d2017-03-10 15:29:11 -0800291 // first one is always :, drop it.
292 archiveName("${project.getPath().split(":").join("_").substring(1)}.zip")
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800293 }
294 if (project.rootProject.ext.runningInBuildServer) {
295 task.ignoreFailures = true
296 }
297 task.finalizedBy zipTask
298 task.doFirst {
299 zipTask.from(junitReport.destination)
300 }
301 }
302 }
303 }
304
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800305 project.afterEvaluate {
306 // The archivesBaseName isn't available initially, so set it now
307 def createZipTask = project.tasks.findByName("createSeparateZip")
308 if (createZipTask != null) {
309 createZipTask.appendix = archivesBaseName
310 createZipTask.from versionDir()
311 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800312 }
313
314 project.afterEvaluate { p ->
315 // remove dependency on the test so that we still get coverage even if some tests fail
316 p.tasks.findAll { it instanceof JacocoReportTask }.each { task ->
317 def toBeRemoved = new ArrayList()
318 def dependencyList = task.taskDependencies.values
319 dependencyList.each { dep ->
320 if (dep instanceof String) {
321 def t = tasks.findByName(dep)
322 if (t instanceof DeviceProviderInstrumentTestTask) {
323 toBeRemoved.add(dep)
324 task.mustRunAfter(t)
325 }
326 }
327 }
328 toBeRemoved.each { dep ->
329 dependencyList.remove(dep)
330 }
331 }
332 }
333 }
334}
335
336def setupRelease() {
337 apply from: "${ext.supportRootFolder}/buildSrc/release.gradle"
338}
339
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800340ext.init.enableDoclavaAndJDiff = this.&enableDoclavaAndJDiff
341ext.init.setSdkInLocalPropertiesFile = this.&setSdkInLocalPropertiesFile
342ext.init.setupRepoOutAndBuildNumber = this.&setupRepoOutAndBuildNumber
343ext.init.setupRelease = this.&setupRelease
344ext.init.loadDefaultVersions = this.&loadDefaultVersions
345ext.init.configureSubProjects = this.&configureSubProjects