blob: 90e8fac0a2477054e44723219cf46b8353ad0cdc [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() {
Yigit Boyaref300662017-05-01 11:52:07 -070094 // common support repo folder which works well for prebuilts.
Yigit Boyar88c16ce2017-02-08 16:06:14 -080095 ext.supportRepoOut = ''
Yigit Boyaref300662017-05-01 11:52:07 -070096 // files in artifactoryRepoOut can be safely copied into a real artifactory.
97 ext.artifactoryRepoOut = ''
Yigit Boyar88c16ce2017-02-08 16:06:14 -080098 ext.buildNumber = Integer.toString(ext.extraVersion)
99
100 /*
101 * With the build server you are given two env variables.
102 * The OUT_DIR is a temporary directory you can use to put things during the build.
103 * The DIST_DIR is where you want to save things from the build.
104 *
105 * The build server will copy the contents of DIST_DIR to somewhere and make it available.
106 */
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800107 if (ext.runningInBuildServer) {
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800108 buildDir = new File(System.env.OUT_DIR + '/gradle/frameworks/support/build')
109 .getCanonicalFile()
110 project.ext.distDir = new File(System.env.DIST_DIR).getCanonicalFile()
111
112 // the build server does not pass the build number so we infer it from the last folder of
113 // the dist path.
114 ext.buildNumber = project.ext.distDir.getName()
Aurimas Liutikasa0150042017-04-21 09:46:12 -0700115
116 // the build server should always print out full stack traces for any failures.
117 gradle.startParameter.showStacktrace = ShowStacktrace.ALWAYS
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800118 } else {
119 buildDir = file("${ext.supportRootFolder}/../../out/host/gradle/frameworks/support/build")
120 project.ext.distDir = new File("${ext.supportRootFolder}/../../out/dist")
121 }
122 subprojects {
123 // Change buildDir first so that all plugins pick up the new value.
124 project.buildDir = new File("$project.parent.buildDir/../$project.name/build")
125 }
126 ext.supportRepoOut = new File(buildDir, 'support_repo')
Yigit Boyaref300662017-05-01 11:52:07 -0700127 ext.artifactoryRepoOut = new File(buildDir, 'artifactory_repo')
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800128 ext.testApkDistOut = ext.distDir
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800129 ext.testResultsDistDir = new File(distDir, "host-test-reports")
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800130 ext.docsDir = new File(buildDir, 'javadoc')
131}
132
133def configureSubProjects() {
134 // lint every library
135 def lintTask = project.tasks.create("lint")
136 subprojects {
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700137 repos.addMavenRepositories(repositories)
138
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800139 // Only modify Android projects.
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700140 if (project.name.equals('doclava')
141 || project.name.equals('jdiff')
142 || project.name.equals('support-testutils')
143 || project.name.equals('noto-emoji-compat')) {
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800144 // disable tests and return
145 project.tasks.whenTaskAdded { task ->
146 if (task instanceof org.gradle.api.tasks.testing.Test) {
147 task.enabled = false
148 }
149 }
150 return
151 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800152
153 // Current SDK is set in studioCompat.gradle.
154 project.ext.currentSdk = gradle.ext.currentSdk
155 apply plugin: 'maven'
156
157 version = rootProject.ext.supportVersion
158 group = 'com.android.support'
159
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800160 project.plugins.whenPluginAdded { plugin ->
161 def isAndroidLibrary = "com.android.build.gradle.LibraryPlugin"
162 .equals(plugin.class.name)
163 def isAndroidApp = "com.android.build.gradle.AppPlugin".equals(plugin.class.name)
164 def isJavaLibrary = "org.gradle.api.plugins.JavaPlugin".equals(plugin.class.name)
165
166 if (isAndroidLibrary || isAndroidApp) {
167 project.android.buildToolsVersion = rootProject.buildToolsVersion
168
169 // Enable code coverage for debug builds only if we are not running inside the IDE,
170 // since enabling coverage reports breaks the method parameter resolution in the IDE
171 // debugger.
172 project.android.buildTypes.debug.testCoverageEnabled =
Sergey Vasilinetsac8e72b2017-04-26 15:55:17 -0700173 !project.hasProperty('android.injected.invoked.from.ide')
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800174
175 // Copy the class files in a jar to be later used to generate code coverage report
176 project.android.testVariants.all { v ->
177 // check if the variant has any source files
178 // and test coverage is enabled
179 if (v.buildType.testCoverageEnabled
180 && v.sourceSets.any { !it.java.sourceFiles.isEmpty() }) {
181 def jarifyTask = project.tasks.create(
182 name: "package${v.name.capitalize()}ClassFilesForCoverageReport",
183 type: Jar) {
184 from v.testedVariant.javaCompile.destinationDir
185 exclude "**/R.class"
186 exclude "**/R\$*.class"
187 exclude "**/BuildConfig.class"
188 destinationDir file(project.distDir)
189 archiveName "${project.archivesBaseName}-${v.baseName}-allclasses.jar"
190 }
191 def jacocoAntConfig =
192 project.configurations[JacocoPlugin.ANT_CONFIGURATION_NAME]
193 def jacocoAntArtifacts = jacocoAntConfig.resolvedConfiguration
194 .resolvedArtifacts
195 def version = jacocoAntArtifacts.find { "org.jacoco.ant".equals(it.name) }
196 .moduleVersion.id.version
197 def collectJacocoAntPackages = project.tasks.create(
198 name: "collectJacocoAntPackages",
199 type: Jar) {
200 from(jacocoAntArtifacts.collect { zipTree(it.getFile()) }) {
201 // exclude all the signatures the jar might have
202 exclude "META-INF/*.SF"
203 exclude "META-INF/*.DSA"
204 exclude "META-INF/*.RSA"
205 }
206 destinationDir file(project.distDir)
207 archiveName "jacocoant-" + version + ".jar"
208 }
209 jarifyTask.dependsOn v.getJavaCompiler()
210 v.assemble.dependsOn jarifyTask, collectJacocoAntPackages
211 }
212 }
213
214 // Enforce NewApi lint check as fatal.
215 project.android.lintOptions.check 'NewApi'
216 project.android.lintOptions.fatal 'NewApi'
217 lintTask.dependsOn project.lint
218 }
219
220 if (isAndroidLibrary || isJavaLibrary) {
221 // Add library to the aggregate dependency report.
222 task allDeps(type: DependencyReportTask) {}
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800223
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700224 project.afterEvaluate {
225 Upload uploadTask = (Upload) project.tasks.uploadArchives;
226 uploadTask.repositories.mavenDeployer {
227
228 // Disable unique names for SNAPSHOTS so they can be updated in place.
229 setUniqueVersion(false)
230
231 // Remove any invalid maven-metadata.xml files that may have been
232 // created for SNAPSHOT versions that are *not* uniquely versioned.
233 pom*.each { pom ->
234 if (pom.version.endsWith('-SNAPSHOT')) {
235 final File artifactDir = new File(
236 rootProject.ext.supportRepoOut,
237 pom.groupId.replace('.', '/')
238 + '/' + pom.artifactId
239 + '/' + pom.version)
240 delete fileTree(dir: artifactDir,
241 include: 'maven-metadata.xml*')
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800242 }
243 }
244 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800245
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700246 uploadTask.repositories.mavenDeployer.pom*.whenConfigured { pom ->
247 pom.dependencies.findAll { dep ->
248 dep.groupId == 'com.android.support' &&
249 dep.artifactId != 'support-annotations'
250 }*.type = 'aar'
251 }
252
253 // create a release task that produces artifactory friends artifacts
254 // a.k.a. unique versions for snapshots with their maven-metadata files.
255 task artifactoryRelease(type : Upload) {
256 configuration = uploadTask.configuration
257 repositories {
258 mavenDeployer {
259 repository(url: uri("$rootProject.ext.artifactoryRepoOut"))
260 setUniqueVersion(true)
261 }
262 }
263 }
264
265 // Before the upload, make sure the repo is ready.
266 uploadTask.dependsOn rootProject.tasks.prepareRepo
267
268 artifactoryRelease.dependsOn uploadTask
269
270 // Make the mainupload depend on this one.
271 mainUpload.dependsOn uploadTask
272 mainUpload.dependsOn artifactoryRelease
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800273 }
274
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800275 }
276 }
277
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800278 // Copy instrumentation test APKs and app APKs into the dist dir
279 // For test apks, they are uploaded only if we have java test sources.
280 // For regular app apks, they are uploaded only if they have java sources.
281 project.tasks.whenTaskAdded { task ->
282 if (task.name.startsWith("packageDebug")) {
283 def testApk = task.name.contains("AndroidTest")
284 task.doLast {
285 def source = testApk ? project.android.sourceSets.androidTest
286 : project.android.sourceSets.main
287 if (task.hasProperty("outputFile") && !source.java.sourceFiles.isEmpty()) {
288 copy {
289 from(task.outputFile)
290 into(rootProject.ext.testApkDistOut)
291 rename { String fileName ->
292 // multiple modules may have the same name so prefix the name with
293 // the module's path to ensure it is unique.
294 // e.g. palette-v7-debug-androidTest.apk becomes
295 // support-palette-v7_palette-v7-debug-androidTest.apk
296 "${project.getPath().replace(':', '-').substring(1)}_${fileName}"
297 }
298 }
299 }
300 }
301 }
302 }
303
304 // copy host side test results to DIST
305 project.tasks.whenTaskAdded { task ->
306 if (task instanceof org.gradle.api.tasks.testing.Test) {
307 def junitReport = task.reports.junitXml
308 if (junitReport.enabled) {
309 def zipTask = project.tasks.create(name : "zipResultsOf${task.name.capitalize()}", type : Zip) {
310 destinationDir(testResultsDistDir)
Yigit Boyar278676d2017-03-10 15:29:11 -0800311 // first one is always :, drop it.
312 archiveName("${project.getPath().split(":").join("_").substring(1)}.zip")
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800313 }
314 if (project.rootProject.ext.runningInBuildServer) {
315 task.ignoreFailures = true
316 }
317 task.finalizedBy zipTask
318 task.doFirst {
319 zipTask.from(junitReport.destination)
320 }
321 }
322 }
323 }
324
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800325 project.afterEvaluate {
326 // The archivesBaseName isn't available initially, so set it now
327 def createZipTask = project.tasks.findByName("createSeparateZip")
328 if (createZipTask != null) {
329 createZipTask.appendix = archivesBaseName
330 createZipTask.from versionDir()
331 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800332 }
333
334 project.afterEvaluate { p ->
335 // remove dependency on the test so that we still get coverage even if some tests fail
336 p.tasks.findAll { it instanceof JacocoReportTask }.each { task ->
337 def toBeRemoved = new ArrayList()
338 def dependencyList = task.taskDependencies.values
339 dependencyList.each { dep ->
340 if (dep instanceof String) {
341 def t = tasks.findByName(dep)
342 if (t instanceof DeviceProviderInstrumentTestTask) {
343 toBeRemoved.add(dep)
344 task.mustRunAfter(t)
345 }
346 }
347 }
348 toBeRemoved.each { dep ->
349 dependencyList.remove(dep)
350 }
351 }
352 }
353 }
354}
355
356def setupRelease() {
357 apply from: "${ext.supportRootFolder}/buildSrc/release.gradle"
358}
359
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800360ext.init.enableDoclavaAndJDiff = this.&enableDoclavaAndJDiff
361ext.init.setSdkInLocalPropertiesFile = this.&setSdkInLocalPropertiesFile
362ext.init.setupRepoOutAndBuildNumber = this.&setupRepoOutAndBuildNumber
363ext.init.setupRelease = this.&setupRelease
364ext.init.loadDefaultVersions = this.&loadDefaultVersions
365ext.init.configureSubProjects = this.&configureSubProjects