blob: 4914ceeffbfbd0aa907726412998e3345c56e07b [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
Sergey Vasilinets22b62142017-11-22 20:32:27 -080017
Sergey Vasilinets22b62142017-11-22 20:32:27 -080018import android.support.DiffAndDocs
Aurimas Liutikas1187af12017-11-28 15:28:01 -080019import android.support.gmaven.GMavenVersionChecker
Yigit Boyar88c16ce2017-02-08 16:06:14 -080020import com.android.build.gradle.internal.coverage.JacocoReportTask
21import com.android.build.gradle.internal.tasks.DeviceProviderInstrumentTestTask
Aurimas Liutikasa0150042017-04-21 09:46:12 -070022import org.gradle.api.logging.configuration.ShowStacktrace
Yigit Boyar88c16ce2017-02-08 16:06:14 -080023
Aurimas Liutikas419f9932017-12-18 12:53:17 -080024import javax.tools.ToolProvider
25
Yigit Boyar88c16ce2017-02-08 16:06:14 -080026def supportRoot = ext.supportRootFolder
27if (supportRoot == null) {
28 throw new RuntimeException("variable supportRootFolder is not set. you must set it before" +
29 " including this script")
30}
31def init = new Properties()
32ext.init = init
Aurimas Liutikas5f3a2772018-01-05 19:07:40 -080033rootProject.ext.versionChecker = new GMavenVersionChecker(rootProject.logger)
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"
Aurimas Liutikas1187af12017-11-28 15:28:01 -080037apply from: "${supportRoot}/buildSrc/build_dependencies.gradle"
Yigit Boyar88c16ce2017-02-08 16:06:14 -080038
Sergey Vasilinets16385352017-12-13 16:56:47 -080039def enableDoclavaAndJDiff(p, dacOptions) {
Yigit Boyar88c16ce2017-02-08 16:06:14 -080040 p.configurations {
41 doclava
42 jdiff
43 }
44
45 p.dependencies {
Aurimas Liutikas419f9932017-12-18 12:53:17 -080046 doclava build_libs.doclava
47 // tools.jar required for com.sun.javadoc
48 doclava files(((URLClassLoader) ToolProvider.getSystemToolClassLoader()).getURLs())
49 jdiff build_libs.jdiff
Aurimas Liutikas1187af12017-11-28 15:28:01 -080050 jdiff build_libs.xml_parser_apis
51 jdiff build_libs.xerces_impl
Yigit Boyar88c16ce2017-02-08 16:06:14 -080052 }
Sergey Vasilinets16385352017-12-13 16:56:47 -080053 def allChecks = DiffAndDocs.configureDiffAndDocs(rootProject, supportRootFolder, dacOptions)
54 createArchive.dependsOn(allChecks)
Yigit Boyar88c16ce2017-02-08 16:06:14 -080055}
56
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070057def getFullSdkPath() {
Aurimas Liutikasb06771d2017-04-20 09:46:49 -070058 final String osName = System.getProperty("os.name").toLowerCase();
59 final boolean isMacOsX =
60 osName.contains("mac os x") || osName.contains("darwin") || osName.contains("osx");
61 final String platform = isMacOsX ? 'darwin' : 'linux'
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070062 return "${repos.prebuiltsRoot}/fullsdk-${platform}"
63}
64
65def setSdkInLocalPropertiesFile() {
Aurimas Liutikas6eba1fc2018-01-02 16:02:50 -080066 final File fullSdkPath = file(getFullSdkPath())
67 if (fullSdkPath.exists()) {
68 project.ext.fullSdkPath = fullSdkPath
Yigit Boyar88c16ce2017-02-08 16:06:14 -080069 File props = file("local.properties")
Aurimas Liutikas6eba1fc2018-01-02 16:02:50 -080070 props.write "sdk.dir=${fullSdkPath.getAbsolutePath()}"
Aurimas Liutikas3c666002017-03-08 19:30:28 -080071 ext.usingFullSdk = true
Yigit Boyar88c16ce2017-02-08 16:06:14 -080072 } else {
Aurimas Liutikas6eba1fc2018-01-02 16:02:50 -080073 throw Exception("You are using non ub-supportlib-* checkout. You need to check out "
74 + "ub-supportlib-* to work on support library. See go/supportlib for details.")
Yigit Boyar88c16ce2017-02-08 16:06:14 -080075 }
76}
77
78def setupRepoOutAndBuildNumber() {
Yigit Boyaref300662017-05-01 11:52:07 -070079 // common support repo folder which works well for prebuilts.
Yigit Boyar88c16ce2017-02-08 16:06:14 -080080 ext.supportRepoOut = ''
Aurimas Liutikas76542da2017-06-29 17:00:29 -070081 ext.buildNumber = "0"
Yigit Boyar88c16ce2017-02-08 16:06:14 -080082 /*
83 * With the build server you are given two env variables.
84 * The OUT_DIR is a temporary directory you can use to put things during the build.
85 * The DIST_DIR is where you want to save things from the build.
86 *
87 * The build server will copy the contents of DIST_DIR to somewhere and make it available.
88 */
Yigit Boyar7bfacb72017-03-02 14:27:41 -080089 if (ext.runningInBuildServer) {
Yigit Boyar88c16ce2017-02-08 16:06:14 -080090 buildDir = new File(System.env.OUT_DIR + '/gradle/frameworks/support/build')
91 .getCanonicalFile()
92 project.ext.distDir = new File(System.env.DIST_DIR).getCanonicalFile()
93
94 // the build server does not pass the build number so we infer it from the last folder of
95 // the dist path.
96 ext.buildNumber = project.ext.distDir.getName()
Aurimas Liutikasa0150042017-04-21 09:46:12 -070097
98 // the build server should always print out full stack traces for any failures.
99 gradle.startParameter.showStacktrace = ShowStacktrace.ALWAYS
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800100 } else {
101 buildDir = file("${ext.supportRootFolder}/../../out/host/gradle/frameworks/support/build")
102 project.ext.distDir = new File("${ext.supportRootFolder}/../../out/dist")
103 }
104 subprojects {
105 // Change buildDir first so that all plugins pick up the new value.
106 project.buildDir = new File("$project.parent.buildDir/../$project.name/build")
107 }
108 ext.supportRepoOut = new File(buildDir, 'support_repo')
109 ext.testApkDistOut = ext.distDir
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800110 ext.testResultsDistDir = new File(distDir, "host-test-reports")
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800111 ext.docsDir = new File(buildDir, 'javadoc')
112}
113
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700114def configureBuildOnServer() {
115 def buildOnServerTask = rootProject.tasks.create("buildOnServer")
116 rootProject.tasks.whenTaskAdded { task ->
Aurimas Liutikas212905f2017-10-09 15:28:28 -0700117 if ("createArchive".equals(task.name) || "distDocs".equals(task.name)) {
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700118 buildOnServerTask.dependsOn task
119 }
120 }
121
122 subprojects {
123 project.tasks.whenTaskAdded { task ->
124 if ("assembleErrorProne".equals(task.name) || "assembleAndroidTest".equals(task.name)) {
125 buildOnServerTask.dependsOn task
126 }
127 }
128 }
Aurimas Liutikas7cf34972017-12-18 09:27:47 -0800129 buildOnServerTask.dependsOn createJacocoAntUberJarTask()
130}
131
132def createJacocoAntUberJarTask() {
133 def myJacoco = project.configurations.create('myJacoco')
134 project.dependencies.add('myJacoco', build_libs.jacoco_ant)
135
136 return project.tasks.create(
137 name: "JacocoAntUberJar",
138 type: Jar) {
139 inputs.files myJacoco
140 from {
141 myJacoco
142 .resolvedConfiguration
143 .resolvedArtifacts.collect{ zipTree(it.getFile()) }} {
144 // exclude all the signatures the jar might have
145 exclude "META-INF/*.SF"
146 exclude "META-INF/*.DSA"
147 exclude "META-INF/*.RSA"
148 }
149 destinationDir file(project.distDir)
150 archiveName "jacocoant.jar"
151 }
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700152}
153
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800154def configureSubProjects() {
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800155 subprojects {
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700156 repos.addMavenRepositories(repositories)
157
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800158 // Only modify Android projects.
Aurimas Liutikas419f9932017-12-18 12:53:17 -0800159 if (project.name.equals('noto-emoji-compat')
Hyundo Moonda9ee6b22017-07-21 14:32:12 +0900160 || project.name.equals('support-media-compat-test-lib')) {
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800161 // disable tests and return
162 project.tasks.whenTaskAdded { task ->
163 if (task instanceof org.gradle.api.tasks.testing.Test) {
164 task.enabled = false
165 }
166 }
167 return
168 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800169
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800170 project.plugins.whenPluginAdded { plugin ->
171 def isAndroidLibrary = "com.android.build.gradle.LibraryPlugin"
172 .equals(plugin.class.name)
173 def isAndroidApp = "com.android.build.gradle.AppPlugin".equals(plugin.class.name)
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800174
175 if (isAndroidLibrary || isAndroidApp) {
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800176 // Enable code coverage for debug builds only if we are not running inside the IDE,
177 // since enabling coverage reports breaks the method parameter resolution in the IDE
178 // debugger.
179 project.android.buildTypes.debug.testCoverageEnabled =
Sergey Vasilinetsac8e72b2017-04-26 15:55:17 -0700180 !project.hasProperty('android.injected.invoked.from.ide')
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800181
182 // Copy the class files in a jar to be later used to generate code coverage report
183 project.android.testVariants.all { v ->
184 // check if the variant has any source files
185 // and test coverage is enabled
186 if (v.buildType.testCoverageEnabled
187 && v.sourceSets.any { !it.java.sourceFiles.isEmpty() }) {
188 def jarifyTask = project.tasks.create(
189 name: "package${v.name.capitalize()}ClassFilesForCoverageReport",
190 type: Jar) {
191 from v.testedVariant.javaCompile.destinationDir
192 exclude "**/R.class"
193 exclude "**/R\$*.class"
194 exclude "**/BuildConfig.class"
195 destinationDir file(project.distDir)
Aurimas Liutikas76542da2017-06-29 17:00:29 -0700196 archiveName "${project.name}-${v.baseName}-allclasses.jar"
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800197 }
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700198
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800199 jarifyTask.dependsOn v.getJavaCompiler()
Aurimas Liutikas7cf34972017-12-18 09:27:47 -0800200 v.assemble.dependsOn jarifyTask
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800201 }
202 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800203 }
204 }
205
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800206 // Copy instrumentation test APKs and app APKs into the dist dir
207 // For test apks, they are uploaded only if we have java test sources.
208 // For regular app apks, they are uploaded only if they have java sources.
209 project.tasks.whenTaskAdded { task ->
210 if (task.name.startsWith("packageDebug")) {
211 def testApk = task.name.contains("AndroidTest")
212 task.doLast {
213 def source = testApk ? project.android.sourceSets.androidTest
214 : project.android.sourceSets.main
Yigit Boyare606d6d2017-09-07 12:37:36 -0700215 def hasKotlinSources = false
216 if (source.hasProperty('kotlin')) {
217 if (!source.kotlin.files.isEmpty()) {
218 hasKotlinSources = true
219 } else {
220 // kotlin files does not show in java sources due to the *.java filter
221 // so we need to check them manually
222 hasKotlinSources = source.java.sourceDirectoryTrees.any {
223 !fileTree(dir: it.dir, include:'**/*.kt').files.isEmpty()
224 }
225 }
226 }
227 def hasSourceCode = !source.java.sourceFiles.isEmpty() || hasKotlinSources
228 if (task.hasProperty("outputDirectory") && (hasSourceCode || !testApk)) {
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800229 copy {
Aurimas Liutikasef3b4922017-06-28 10:44:59 -0700230 from(task.outputDirectory)
231 include '*.apk'
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800232 into(rootProject.ext.testApkDistOut)
233 rename { String fileName ->
Hyundo Moon9938e172017-07-26 20:51:11 +0900234 // Exclude media-compat-test-* modules from existing support library
235 // presubmit tests.
236 if (fileName.contains("media-compat-test")) {
237 fileName.replace("-debug-androidTest", "")
Yigit Boyar96a0fcd2017-08-25 18:18:28 -0700238 } else {
239 // multiple modules may have the same name so prefix the name with
240 // the module's path to ensure it is unique.
241 // e.g. palette-v7-debug-androidTest.apk becomes
242 // support-palette-v7_palette-v7-debug-androidTest.apk
243 "${project.getPath().replace(':', '-').substring(1)}_${fileName}"
Hyundo Moon9938e172017-07-26 20:51:11 +0900244 }
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800245 }
246 }
247 }
248 }
249 }
250 }
251
252 // copy host side test results to DIST
253 project.tasks.whenTaskAdded { task ->
254 if (task instanceof org.gradle.api.tasks.testing.Test) {
255 def junitReport = task.reports.junitXml
256 if (junitReport.enabled) {
257 def zipTask = project.tasks.create(name : "zipResultsOf${task.name.capitalize()}", type : Zip) {
258 destinationDir(testResultsDistDir)
Yigit Boyar278676d2017-03-10 15:29:11 -0800259 // first one is always :, drop it.
260 archiveName("${project.getPath().split(":").join("_").substring(1)}.zip")
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800261 }
262 if (project.rootProject.ext.runningInBuildServer) {
263 task.ignoreFailures = true
264 }
265 task.finalizedBy zipTask
266 task.doFirst {
267 zipTask.from(junitReport.destination)
268 }
269 }
270 }
271 }
272
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800273 project.afterEvaluate { p ->
274 // remove dependency on the test so that we still get coverage even if some tests fail
275 p.tasks.findAll { it instanceof JacocoReportTask }.each { task ->
276 def toBeRemoved = new ArrayList()
277 def dependencyList = task.taskDependencies.values
278 dependencyList.each { dep ->
279 if (dep instanceof String) {
280 def t = tasks.findByName(dep)
281 if (t instanceof DeviceProviderInstrumentTestTask) {
282 toBeRemoved.add(dep)
283 task.mustRunAfter(t)
284 }
285 }
286 }
287 toBeRemoved.each { dep ->
288 dependencyList.remove(dep)
289 }
290 }
291 }
292 }
293}
294
295def setupRelease() {
296 apply from: "${ext.supportRootFolder}/buildSrc/release.gradle"
297}
298
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800299ext.init.enableDoclavaAndJDiff = this.&enableDoclavaAndJDiff
300ext.init.setSdkInLocalPropertiesFile = this.&setSdkInLocalPropertiesFile
301ext.init.setupRepoOutAndBuildNumber = this.&setupRepoOutAndBuildNumber
302ext.init.setupRelease = this.&setupRelease
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700303ext.init.configureSubProjects = this.&configureSubProjects
Yigit Boyare1bbf712017-03-08 13:52:37 -0800304ext.init.configureBuildOnServer = this.&configureBuildOnServer