blob: cef2bedbc825c87ff41a916ea48bf0408d98a202 [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 Liutikas6eba1fc2018-01-02 16:02:50 -080019import android.support.SupportConfig
Aurimas Liutikas1187af12017-11-28 15:28:01 -080020import android.support.gmaven.GMavenVersionChecker
Yigit Boyar88c16ce2017-02-08 16:06:14 -080021import com.android.build.gradle.internal.coverage.JacocoReportTask
22import com.android.build.gradle.internal.tasks.DeviceProviderInstrumentTestTask
Aurimas Liutikasa0150042017-04-21 09:46:12 -070023import org.gradle.api.logging.configuration.ShowStacktrace
Yigit Boyar88c16ce2017-02-08 16:06:14 -080024
Aurimas Liutikas419f9932017-12-18 12:53:17 -080025import javax.tools.ToolProvider
26
Yigit Boyar88c16ce2017-02-08 16:06:14 -080027def supportRoot = ext.supportRootFolder
28if (supportRoot == null) {
29 throw new RuntimeException("variable supportRootFolder is not set. you must set it before" +
30 " including this script")
31}
32def init = new Properties()
33ext.init = init
Yigit Boyara9ac19d2017-09-20 16:11:49 -070034rootProject.ext.versionChecker = new GMavenVersionChecker(rootProject)
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -070035ext.runningInBuildServer = System.env.DIST_DIR != null && System.env.OUT_DIR != null
Yigit Boyar88c16ce2017-02-08 16:06:14 -080036
37apply from: "${supportRoot}/buildSrc/dependencies.gradle"
Aurimas Liutikas1187af12017-11-28 15:28:01 -080038apply from: "${supportRoot}/buildSrc/build_dependencies.gradle"
Yigit Boyar88c16ce2017-02-08 16:06:14 -080039
Sergey Vasilinets16385352017-12-13 16:56:47 -080040def enableDoclavaAndJDiff(p, dacOptions) {
Yigit Boyar88c16ce2017-02-08 16:06:14 -080041 p.configurations {
42 doclava
43 jdiff
44 }
45
46 p.dependencies {
Aurimas Liutikas419f9932017-12-18 12:53:17 -080047 doclava build_libs.doclava
48 // tools.jar required for com.sun.javadoc
49 doclava files(((URLClassLoader) ToolProvider.getSystemToolClassLoader()).getURLs())
50 jdiff build_libs.jdiff
Aurimas Liutikas1187af12017-11-28 15:28:01 -080051 jdiff build_libs.xml_parser_apis
52 jdiff build_libs.xerces_impl
Yigit Boyar88c16ce2017-02-08 16:06:14 -080053 }
Sergey Vasilinets16385352017-12-13 16:56:47 -080054 def allChecks = DiffAndDocs.configureDiffAndDocs(rootProject, supportRootFolder, dacOptions)
55 createArchive.dependsOn(allChecks)
Yigit Boyar88c16ce2017-02-08 16:06:14 -080056}
57
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070058def getFullSdkPath() {
Aurimas Liutikasb06771d2017-04-20 09:46:49 -070059 final String osName = System.getProperty("os.name").toLowerCase();
60 final boolean isMacOsX =
61 osName.contains("mac os x") || osName.contains("darwin") || osName.contains("osx");
62 final String platform = isMacOsX ? 'darwin' : 'linux'
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070063 return "${repos.prebuiltsRoot}/fullsdk-${platform}"
64}
65
66def setSdkInLocalPropertiesFile() {
Aurimas Liutikas6eba1fc2018-01-02 16:02:50 -080067 final File fullSdkPath = file(getFullSdkPath())
68 if (fullSdkPath.exists()) {
69 project.ext.fullSdkPath = fullSdkPath
Yigit Boyar88c16ce2017-02-08 16:06:14 -080070 File props = file("local.properties")
Aurimas Liutikas6eba1fc2018-01-02 16:02:50 -080071 props.write "sdk.dir=${fullSdkPath.getAbsolutePath()}"
Aurimas Liutikas3c666002017-03-08 19:30:28 -080072 ext.usingFullSdk = true
Yigit Boyar88c16ce2017-02-08 16:06:14 -080073 } else {
Aurimas Liutikas6eba1fc2018-01-02 16:02:50 -080074 throw Exception("You are using non ub-supportlib-* checkout. You need to check out "
75 + "ub-supportlib-* to work on support library. See go/supportlib for details.")
Yigit Boyar88c16ce2017-02-08 16:06:14 -080076 }
77}
78
79def setupRepoOutAndBuildNumber() {
Yigit Boyaref300662017-05-01 11:52:07 -070080 // common support repo folder which works well for prebuilts.
Yigit Boyar88c16ce2017-02-08 16:06:14 -080081 ext.supportRepoOut = ''
Aurimas Liutikas76542da2017-06-29 17:00:29 -070082 ext.buildNumber = "0"
Yigit Boyar88c16ce2017-02-08 16:06:14 -080083 /*
84 * With the build server you are given two env variables.
85 * The OUT_DIR is a temporary directory you can use to put things during the build.
86 * The DIST_DIR is where you want to save things from the build.
87 *
88 * The build server will copy the contents of DIST_DIR to somewhere and make it available.
89 */
Yigit Boyar7bfacb72017-03-02 14:27:41 -080090 if (ext.runningInBuildServer) {
Yigit Boyar88c16ce2017-02-08 16:06:14 -080091 buildDir = new File(System.env.OUT_DIR + '/gradle/frameworks/support/build')
92 .getCanonicalFile()
93 project.ext.distDir = new File(System.env.DIST_DIR).getCanonicalFile()
94
95 // the build server does not pass the build number so we infer it from the last folder of
96 // the dist path.
97 ext.buildNumber = project.ext.distDir.getName()
Aurimas Liutikasa0150042017-04-21 09:46:12 -070098
99 // the build server should always print out full stack traces for any failures.
100 gradle.startParameter.showStacktrace = ShowStacktrace.ALWAYS
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800101 } else {
102 buildDir = file("${ext.supportRootFolder}/../../out/host/gradle/frameworks/support/build")
103 project.ext.distDir = new File("${ext.supportRootFolder}/../../out/dist")
104 }
105 subprojects {
106 // Change buildDir first so that all plugins pick up the new value.
107 project.buildDir = new File("$project.parent.buildDir/../$project.name/build")
108 }
109 ext.supportRepoOut = new File(buildDir, 'support_repo')
110 ext.testApkDistOut = ext.distDir
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800111 ext.testResultsDistDir = new File(distDir, "host-test-reports")
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800112 ext.docsDir = new File(buildDir, 'javadoc')
113}
114
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700115def configureBuildOnServer() {
116 def buildOnServerTask = rootProject.tasks.create("buildOnServer")
117 rootProject.tasks.whenTaskAdded { task ->
Aurimas Liutikas212905f2017-10-09 15:28:28 -0700118 if ("createArchive".equals(task.name) || "distDocs".equals(task.name)) {
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700119 buildOnServerTask.dependsOn task
120 }
121 }
122
123 subprojects {
124 project.tasks.whenTaskAdded { task ->
125 if ("assembleErrorProne".equals(task.name) || "assembleAndroidTest".equals(task.name)) {
126 buildOnServerTask.dependsOn task
127 }
128 }
129 }
Aurimas Liutikas7cf34972017-12-18 09:27:47 -0800130 buildOnServerTask.dependsOn createJacocoAntUberJarTask()
131}
132
133def createJacocoAntUberJarTask() {
134 def myJacoco = project.configurations.create('myJacoco')
135 project.dependencies.add('myJacoco', build_libs.jacoco_ant)
136
137 return project.tasks.create(
138 name: "JacocoAntUberJar",
139 type: Jar) {
140 inputs.files myJacoco
141 from {
142 myJacoco
143 .resolvedConfiguration
144 .resolvedArtifacts.collect{ zipTree(it.getFile()) }} {
145 // exclude all the signatures the jar might have
146 exclude "META-INF/*.SF"
147 exclude "META-INF/*.DSA"
148 exclude "META-INF/*.RSA"
149 }
150 destinationDir file(project.distDir)
151 archiveName "jacocoant.jar"
152 }
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700153}
154
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800155def configureSubProjects() {
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800156 subprojects {
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700157 repos.addMavenRepositories(repositories)
158
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800159 // Only modify Android projects.
Aurimas Liutikas419f9932017-12-18 12:53:17 -0800160 if (project.name.equals('noto-emoji-compat')
Hyundo Moonda9ee6b22017-07-21 14:32:12 +0900161 || project.name.equals('support-media-compat-test-lib')) {
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800162 // disable tests and return
163 project.tasks.whenTaskAdded { task ->
164 if (task instanceof org.gradle.api.tasks.testing.Test) {
165 task.enabled = false
166 }
167 }
168 return
169 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800170
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800171 project.plugins.whenPluginAdded { plugin ->
172 def isAndroidLibrary = "com.android.build.gradle.LibraryPlugin"
173 .equals(plugin.class.name)
174 def isAndroidApp = "com.android.build.gradle.AppPlugin".equals(plugin.class.name)
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800175
176 if (isAndroidLibrary || isAndroidApp) {
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800177 // Enable code coverage for debug builds only if we are not running inside the IDE,
178 // since enabling coverage reports breaks the method parameter resolution in the IDE
179 // debugger.
180 project.android.buildTypes.debug.testCoverageEnabled =
Sergey Vasilinetsac8e72b2017-04-26 15:55:17 -0700181 !project.hasProperty('android.injected.invoked.from.ide')
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800182
183 // Copy the class files in a jar to be later used to generate code coverage report
184 project.android.testVariants.all { v ->
185 // check if the variant has any source files
186 // and test coverage is enabled
187 if (v.buildType.testCoverageEnabled
188 && v.sourceSets.any { !it.java.sourceFiles.isEmpty() }) {
189 def jarifyTask = project.tasks.create(
190 name: "package${v.name.capitalize()}ClassFilesForCoverageReport",
191 type: Jar) {
192 from v.testedVariant.javaCompile.destinationDir
193 exclude "**/R.class"
194 exclude "**/R\$*.class"
195 exclude "**/BuildConfig.class"
196 destinationDir file(project.distDir)
Aurimas Liutikas76542da2017-06-29 17:00:29 -0700197 archiveName "${project.name}-${v.baseName}-allclasses.jar"
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800198 }
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700199
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800200 jarifyTask.dependsOn v.getJavaCompiler()
Aurimas Liutikas7cf34972017-12-18 09:27:47 -0800201 v.assemble.dependsOn jarifyTask
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800202 }
203 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800204 }
205 }
206
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800207 // Copy instrumentation test APKs and app APKs into the dist dir
208 // For test apks, they are uploaded only if we have java test sources.
209 // For regular app apks, they are uploaded only if they have java sources.
210 project.tasks.whenTaskAdded { task ->
211 if (task.name.startsWith("packageDebug")) {
212 def testApk = task.name.contains("AndroidTest")
213 task.doLast {
214 def source = testApk ? project.android.sourceSets.androidTest
215 : project.android.sourceSets.main
Yigit Boyare606d6d2017-09-07 12:37:36 -0700216 def hasKotlinSources = false
217 if (source.hasProperty('kotlin')) {
218 if (!source.kotlin.files.isEmpty()) {
219 hasKotlinSources = true
220 } else {
221 // kotlin files does not show in java sources due to the *.java filter
222 // so we need to check them manually
223 hasKotlinSources = source.java.sourceDirectoryTrees.any {
224 !fileTree(dir: it.dir, include:'**/*.kt').files.isEmpty()
225 }
226 }
227 }
228 def hasSourceCode = !source.java.sourceFiles.isEmpty() || hasKotlinSources
229 if (task.hasProperty("outputDirectory") && (hasSourceCode || !testApk)) {
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800230 copy {
Aurimas Liutikasef3b4922017-06-28 10:44:59 -0700231 from(task.outputDirectory)
232 include '*.apk'
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800233 into(rootProject.ext.testApkDistOut)
234 rename { String fileName ->
Hyundo Moon9938e172017-07-26 20:51:11 +0900235 // Exclude media-compat-test-* modules from existing support library
236 // presubmit tests.
237 if (fileName.contains("media-compat-test")) {
238 fileName.replace("-debug-androidTest", "")
Yigit Boyar96a0fcd2017-08-25 18:18:28 -0700239 } else {
240 // multiple modules may have the same name so prefix the name with
241 // the module's path to ensure it is unique.
242 // e.g. palette-v7-debug-androidTest.apk becomes
243 // support-palette-v7_palette-v7-debug-androidTest.apk
244 "${project.getPath().replace(':', '-').substring(1)}_${fileName}"
Hyundo Moon9938e172017-07-26 20:51:11 +0900245 }
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800246 }
247 }
248 }
249 }
250 }
251 }
252
253 // copy host side test results to DIST
254 project.tasks.whenTaskAdded { task ->
255 if (task instanceof org.gradle.api.tasks.testing.Test) {
256 def junitReport = task.reports.junitXml
257 if (junitReport.enabled) {
258 def zipTask = project.tasks.create(name : "zipResultsOf${task.name.capitalize()}", type : Zip) {
259 destinationDir(testResultsDistDir)
Yigit Boyar278676d2017-03-10 15:29:11 -0800260 // first one is always :, drop it.
261 archiveName("${project.getPath().split(":").join("_").substring(1)}.zip")
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800262 }
263 if (project.rootProject.ext.runningInBuildServer) {
264 task.ignoreFailures = true
265 }
266 task.finalizedBy zipTask
267 task.doFirst {
268 zipTask.from(junitReport.destination)
269 }
270 }
271 }
272 }
273
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800274 project.afterEvaluate { p ->
275 // remove dependency on the test so that we still get coverage even if some tests fail
276 p.tasks.findAll { it instanceof JacocoReportTask }.each { task ->
277 def toBeRemoved = new ArrayList()
278 def dependencyList = task.taskDependencies.values
279 dependencyList.each { dep ->
280 if (dep instanceof String) {
281 def t = tasks.findByName(dep)
282 if (t instanceof DeviceProviderInstrumentTestTask) {
283 toBeRemoved.add(dep)
284 task.mustRunAfter(t)
285 }
286 }
287 }
288 toBeRemoved.each { dep ->
289 dependencyList.remove(dep)
290 }
291 }
292 }
293 }
294}
295
296def setupRelease() {
297 apply from: "${ext.supportRootFolder}/buildSrc/release.gradle"
298}
299
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800300ext.init.enableDoclavaAndJDiff = this.&enableDoclavaAndJDiff
301ext.init.setSdkInLocalPropertiesFile = this.&setSdkInLocalPropertiesFile
302ext.init.setupRepoOutAndBuildNumber = this.&setupRepoOutAndBuildNumber
303ext.init.setupRelease = this.&setupRelease
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700304ext.init.configureSubProjects = this.&configureSubProjects
Yigit Boyare1bbf712017-03-08 13:52:37 -0800305ext.init.configureBuildOnServer = this.&configureBuildOnServer