blob: d47d5e87090b811b6cd3dc37283febc4a841e981 [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.JacocoPlugin
21import 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
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 Boyara9ac19d2017-09-20 16:11:49 -070033rootProject.ext.versionChecker = new GMavenVersionChecker(rootProject)
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 Liutikaseca7a072017-07-11 01:11:52 +000046 doclava project(':doclava')
47 jdiff project(':jdiff')
Aurimas Liutikas1187af12017-11-28 15:28:01 -080048 jdiff build_libs.xml_parser_apis
49 jdiff build_libs.xerces_impl
Yigit Boyar88c16ce2017-02-08 16:06:14 -080050 }
Sergey Vasilinets16385352017-12-13 16:56:47 -080051 def allChecks = DiffAndDocs.configureDiffAndDocs(rootProject, supportRootFolder, dacOptions)
52 createArchive.dependsOn(allChecks)
Yigit Boyar88c16ce2017-02-08 16:06:14 -080053}
54
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070055def getFullSdkPath() {
Aurimas Liutikasb06771d2017-04-20 09:46:49 -070056 final String osName = System.getProperty("os.name").toLowerCase();
57 final boolean isMacOsX =
58 osName.contains("mac os x") || osName.contains("darwin") || osName.contains("osx");
59 final String platform = isMacOsX ? 'darwin' : 'linux'
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070060 return "${repos.prebuiltsRoot}/fullsdk-${platform}"
61}
62
63def setSdkInLocalPropertiesFile() {
Aurimas Liutikas97c75372017-09-28 17:44:34 -070064 ext.buildToolsVersion = '27.0.0'
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070065 final String fullSdkPath = getFullSdkPath();
Yigit Boyar88c16ce2017-02-08 16:06:14 -080066 if (file(fullSdkPath).exists()) {
67 gradle.ext.currentSdk = 26
Alan Viveretteeb9f3322017-03-09 16:29:57 -050068 project.ext.androidJar =
69 files("${fullSdkPath}/platforms/android-${gradle.currentSdk}/android.jar")
70 project.ext.androidSrcJar =
71 file("${fullSdkPath}/platforms/android-${gradle.currentSdk}/android-stubs-src.jar")
Alan Viverette810a1482017-03-20 12:43:43 -040072 project.ext.androidApiTxt = null
Yigit Boyar88c16ce2017-02-08 16:06:14 -080073 File props = file("local.properties")
74 props.write "sdk.dir=${fullSdkPath}"
Aurimas Liutikas3c666002017-03-08 19:30:28 -080075 ext.usingFullSdk = true
Yigit Boyar88c16ce2017-02-08 16:06:14 -080076 } else {
77 gradle.ext.currentSdk = 'current'
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -070078 project.ext.androidJar = files("${repos.prebuiltsRoot}/sdk/current/android.jar")
Alan Viverette810a1482017-03-20 12:43:43 -040079 project.ext.androidSrcJar = null
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -070080 project.ext.androidApiTxt = file("${repos.prebuiltsRoot}/sdk/api/26.txt")
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070081 System.setProperty('android.dir', "${supportRootFolder}/../../")
Yigit Boyar88c16ce2017-02-08 16:06:14 -080082 File props = file("local.properties")
83 props.write "android.dir=../../"
Aurimas Liutikas3c666002017-03-08 19:30:28 -080084 ext.usingFullSdk = false
Yigit Boyar88c16ce2017-02-08 16:06:14 -080085 }
86}
87
88def setupRepoOutAndBuildNumber() {
Yigit Boyaref300662017-05-01 11:52:07 -070089 // common support repo folder which works well for prebuilts.
Yigit Boyar88c16ce2017-02-08 16:06:14 -080090 ext.supportRepoOut = ''
Aurimas Liutikas76542da2017-06-29 17:00:29 -070091 ext.buildNumber = "0"
Yigit Boyar88c16ce2017-02-08 16:06:14 -080092 /*
93 * With the build server you are given two env variables.
94 * The OUT_DIR is a temporary directory you can use to put things during the build.
95 * The DIST_DIR is where you want to save things from the build.
96 *
97 * The build server will copy the contents of DIST_DIR to somewhere and make it available.
98 */
Yigit Boyar7bfacb72017-03-02 14:27:41 -080099 if (ext.runningInBuildServer) {
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800100 buildDir = new File(System.env.OUT_DIR + '/gradle/frameworks/support/build')
101 .getCanonicalFile()
102 project.ext.distDir = new File(System.env.DIST_DIR).getCanonicalFile()
103
104 // the build server does not pass the build number so we infer it from the last folder of
105 // the dist path.
106 ext.buildNumber = project.ext.distDir.getName()
Aurimas Liutikasa0150042017-04-21 09:46:12 -0700107
108 // the build server should always print out full stack traces for any failures.
109 gradle.startParameter.showStacktrace = ShowStacktrace.ALWAYS
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800110 } else {
111 buildDir = file("${ext.supportRootFolder}/../../out/host/gradle/frameworks/support/build")
112 project.ext.distDir = new File("${ext.supportRootFolder}/../../out/dist")
113 }
114 subprojects {
115 // Change buildDir first so that all plugins pick up the new value.
116 project.buildDir = new File("$project.parent.buildDir/../$project.name/build")
117 }
118 ext.supportRepoOut = new File(buildDir, 'support_repo')
119 ext.testApkDistOut = ext.distDir
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800120 ext.testResultsDistDir = new File(distDir, "host-test-reports")
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800121 ext.docsDir = new File(buildDir, 'javadoc')
122}
123
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700124def configureBuildOnServer() {
125 def buildOnServerTask = rootProject.tasks.create("buildOnServer")
126 rootProject.tasks.whenTaskAdded { task ->
Aurimas Liutikas212905f2017-10-09 15:28:28 -0700127 if ("createArchive".equals(task.name) || "distDocs".equals(task.name)) {
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700128 buildOnServerTask.dependsOn task
129 }
130 }
131
132 subprojects {
133 project.tasks.whenTaskAdded { task ->
134 if ("assembleErrorProne".equals(task.name) || "assembleAndroidTest".equals(task.name)) {
135 buildOnServerTask.dependsOn task
136 }
137 }
138 }
139}
140
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800141def configureSubProjects() {
142 // lint every library
143 def lintTask = project.tasks.create("lint")
144 subprojects {
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700145 repos.addMavenRepositories(repositories)
146
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800147 // Only modify Android projects.
Aurimas Liutikaseca7a072017-07-11 01:11:52 +0000148 if (project.name.equals('doclava')
149 || project.name.equals('jdiff')
Hyundo Moonda9ee6b22017-07-21 14:32:12 +0900150 || project.name.equals('noto-emoji-compat')
151 || project.name.equals('support-media-compat-test-lib')) {
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800152 // disable tests and return
153 project.tasks.whenTaskAdded { task ->
154 if (task instanceof org.gradle.api.tasks.testing.Test) {
155 task.enabled = false
156 }
157 }
158 return
159 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800160
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800161 project.ext.currentSdk = gradle.ext.currentSdk
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800162
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800163 project.plugins.whenPluginAdded { plugin ->
164 def isAndroidLibrary = "com.android.build.gradle.LibraryPlugin"
165 .equals(plugin.class.name)
166 def isAndroidApp = "com.android.build.gradle.AppPlugin".equals(plugin.class.name)
167 def isJavaLibrary = "org.gradle.api.plugins.JavaPlugin".equals(plugin.class.name)
168
169 if (isAndroidLibrary || isAndroidApp) {
170 project.android.buildToolsVersion = rootProject.buildToolsVersion
171
172 // Enable code coverage for debug builds only if we are not running inside the IDE,
173 // since enabling coverage reports breaks the method parameter resolution in the IDE
174 // debugger.
175 project.android.buildTypes.debug.testCoverageEnabled =
Sergey Vasilinetsac8e72b2017-04-26 15:55:17 -0700176 !project.hasProperty('android.injected.invoked.from.ide')
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800177
178 // Copy the class files in a jar to be later used to generate code coverage report
179 project.android.testVariants.all { v ->
180 // check if the variant has any source files
181 // and test coverage is enabled
182 if (v.buildType.testCoverageEnabled
183 && v.sourceSets.any { !it.java.sourceFiles.isEmpty() }) {
184 def jarifyTask = project.tasks.create(
185 name: "package${v.name.capitalize()}ClassFilesForCoverageReport",
186 type: Jar) {
187 from v.testedVariant.javaCompile.destinationDir
188 exclude "**/R.class"
189 exclude "**/R\$*.class"
190 exclude "**/BuildConfig.class"
191 destinationDir file(project.distDir)
Aurimas Liutikas76542da2017-06-29 17:00:29 -0700192 archiveName "${project.name}-${v.baseName}-allclasses.jar"
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800193 }
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700194
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800195 def collectJacocoAntPackages = project.tasks.create(
196 name: "collectJacocoAntPackages",
197 type: Jar) {
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700198 inputs.files project.configurations[JacocoPlugin.ANT_CONFIGURATION_NAME]
199 from {
Aurimas Liutikas585a9342017-06-25 16:07:56 -0700200 project.configurations[JacocoPlugin.ANT_CONFIGURATION_NAME]
201 .resolvedConfiguration
202 .resolvedArtifacts.collect{ zipTree(it.getFile()) }} {
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700203 // exclude all the signatures the jar might have
204 exclude "META-INF/*.SF"
205 exclude "META-INF/*.DSA"
206 exclude "META-INF/*.RSA"
207 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800208 destinationDir file(project.distDir)
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700209 archiveName "jacocoant.jar"
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800210 }
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700211
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800212 jarifyTask.dependsOn v.getJavaCompiler()
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700213 v.assemble.dependsOn jarifyTask , collectJacocoAntPackages
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800214 }
215 }
216
217 // Enforce NewApi lint check as fatal.
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800218 project.android.lintOptions.fatal 'NewApi'
Aurimas Liutikas6c625702017-09-20 14:35:47 -0700219 lintTask.dependsOn {project.lint}
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800220 }
221
222 if (isAndroidLibrary || isJavaLibrary) {
223 // Add library to the aggregate dependency report.
224 task allDeps(type: DependencyReportTask) {}
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800225
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700226 project.afterEvaluate {
227 Upload uploadTask = (Upload) project.tasks.uploadArchives;
228 uploadTask.repositories.mavenDeployer {
Aurimas Liutikas279780d2017-08-09 14:03:59 -0700229 repository(url: uri("$rootProject.ext.supportRepoOut"))
230 setUniqueVersion(true)
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700231 }
232
233 // Before the upload, make sure the repo is ready.
234 uploadTask.dependsOn rootProject.tasks.prepareRepo
235
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700236 // Make the mainupload depend on this one.
237 mainUpload.dependsOn uploadTask
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800238 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800239 }
240 }
241
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800242 // Copy instrumentation test APKs and app APKs into the dist dir
243 // For test apks, they are uploaded only if we have java test sources.
244 // For regular app apks, they are uploaded only if they have java sources.
245 project.tasks.whenTaskAdded { task ->
246 if (task.name.startsWith("packageDebug")) {
247 def testApk = task.name.contains("AndroidTest")
248 task.doLast {
249 def source = testApk ? project.android.sourceSets.androidTest
250 : project.android.sourceSets.main
Yigit Boyare606d6d2017-09-07 12:37:36 -0700251 def hasKotlinSources = false
252 if (source.hasProperty('kotlin')) {
253 if (!source.kotlin.files.isEmpty()) {
254 hasKotlinSources = true
255 } else {
256 // kotlin files does not show in java sources due to the *.java filter
257 // so we need to check them manually
258 hasKotlinSources = source.java.sourceDirectoryTrees.any {
259 !fileTree(dir: it.dir, include:'**/*.kt').files.isEmpty()
260 }
261 }
262 }
263 def hasSourceCode = !source.java.sourceFiles.isEmpty() || hasKotlinSources
264 if (task.hasProperty("outputDirectory") && (hasSourceCode || !testApk)) {
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800265 copy {
Aurimas Liutikasef3b4922017-06-28 10:44:59 -0700266 from(task.outputDirectory)
267 include '*.apk'
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800268 into(rootProject.ext.testApkDistOut)
269 rename { String fileName ->
Hyundo Moon9938e172017-07-26 20:51:11 +0900270 // Exclude media-compat-test-* modules from existing support library
271 // presubmit tests.
272 if (fileName.contains("media-compat-test")) {
273 fileName.replace("-debug-androidTest", "")
Yigit Boyar96a0fcd2017-08-25 18:18:28 -0700274 } else {
275 // multiple modules may have the same name so prefix the name with
276 // the module's path to ensure it is unique.
277 // e.g. palette-v7-debug-androidTest.apk becomes
278 // support-palette-v7_palette-v7-debug-androidTest.apk
279 "${project.getPath().replace(':', '-').substring(1)}_${fileName}"
Hyundo Moon9938e172017-07-26 20:51:11 +0900280 }
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800281 }
282 }
283 }
284 }
285 }
286 }
287
288 // copy host side test results to DIST
289 project.tasks.whenTaskAdded { task ->
290 if (task instanceof org.gradle.api.tasks.testing.Test) {
291 def junitReport = task.reports.junitXml
292 if (junitReport.enabled) {
293 def zipTask = project.tasks.create(name : "zipResultsOf${task.name.capitalize()}", type : Zip) {
294 destinationDir(testResultsDistDir)
Yigit Boyar278676d2017-03-10 15:29:11 -0800295 // first one is always :, drop it.
296 archiveName("${project.getPath().split(":").join("_").substring(1)}.zip")
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800297 }
298 if (project.rootProject.ext.runningInBuildServer) {
299 task.ignoreFailures = true
300 }
301 task.finalizedBy zipTask
302 task.doFirst {
303 zipTask.from(junitReport.destination)
304 }
305 }
306 }
307 }
308
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800309 project.afterEvaluate { p ->
310 // remove dependency on the test so that we still get coverage even if some tests fail
311 p.tasks.findAll { it instanceof JacocoReportTask }.each { task ->
312 def toBeRemoved = new ArrayList()
313 def dependencyList = task.taskDependencies.values
314 dependencyList.each { dep ->
315 if (dep instanceof String) {
316 def t = tasks.findByName(dep)
317 if (t instanceof DeviceProviderInstrumentTestTask) {
318 toBeRemoved.add(dep)
319 task.mustRunAfter(t)
320 }
321 }
322 }
323 toBeRemoved.each { dep ->
324 dependencyList.remove(dep)
325 }
326 }
327 }
328 }
329}
330
331def setupRelease() {
332 apply from: "${ext.supportRootFolder}/buildSrc/release.gradle"
333}
334
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800335ext.init.enableDoclavaAndJDiff = this.&enableDoclavaAndJDiff
336ext.init.setSdkInLocalPropertiesFile = this.&setSdkInLocalPropertiesFile
337ext.init.setupRepoOutAndBuildNumber = this.&setupRepoOutAndBuildNumber
338ext.init.setupRelease = this.&setupRelease
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700339ext.init.configureSubProjects = this.&configureSubProjects
Yigit Boyare1bbf712017-03-08 13:52:37 -0800340ext.init.configureBuildOnServer = this.&configureBuildOnServer