blob: 99d5c42b187c7b2e7cfbb06776e0e7152d630dcb [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
24def supportRoot = ext.supportRootFolder
25if (supportRoot == null) {
26 throw new RuntimeException("variable supportRootFolder is not set. you must set it before" +
27 " including this script")
28}
29def init = new Properties()
30ext.init = init
Alan Viverette1b862372017-03-22 11:32:28 -040031ext.init.debugKeystore = file("${supportRoot}/development/keystore/debug.keystore")
Yigit Boyara9ac19d2017-09-20 16:11:49 -070032rootProject.ext.versionChecker = new GMavenVersionChecker(rootProject)
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -070033ext.runningInBuildServer = System.env.DIST_DIR != null && System.env.OUT_DIR != null
Yigit Boyar88c16ce2017-02-08 16:06:14 -080034
35apply from: "${supportRoot}/buildSrc/dependencies.gradle"
Aurimas Liutikas1187af12017-11-28 15:28:01 -080036apply from: "${supportRoot}/buildSrc/build_dependencies.gradle"
Yigit Boyar88c16ce2017-02-08 16:06:14 -080037
Sergey Vasilinets16385352017-12-13 16:56:47 -080038def enableDoclavaAndJDiff(p, dacOptions) {
Yigit Boyar88c16ce2017-02-08 16:06:14 -080039 p.configurations {
40 doclava
41 jdiff
42 }
43
44 p.dependencies {
Aurimas Liutikaseca7a072017-07-11 01:11:52 +000045 doclava project(':doclava')
46 jdiff project(':jdiff')
Aurimas Liutikas1187af12017-11-28 15:28:01 -080047 jdiff build_libs.xml_parser_apis
48 jdiff build_libs.xerces_impl
Yigit Boyar88c16ce2017-02-08 16:06:14 -080049 }
Sergey Vasilinets16385352017-12-13 16:56:47 -080050 def allChecks = DiffAndDocs.configureDiffAndDocs(rootProject, supportRootFolder, dacOptions)
51 createArchive.dependsOn(allChecks)
Yigit Boyar88c16ce2017-02-08 16:06:14 -080052}
53
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070054def getFullSdkPath() {
Aurimas Liutikasb06771d2017-04-20 09:46:49 -070055 final String osName = System.getProperty("os.name").toLowerCase();
56 final boolean isMacOsX =
57 osName.contains("mac os x") || osName.contains("darwin") || osName.contains("osx");
58 final String platform = isMacOsX ? 'darwin' : 'linux'
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070059 return "${repos.prebuiltsRoot}/fullsdk-${platform}"
60}
61
62def setSdkInLocalPropertiesFile() {
Aurimas Liutikas97c75372017-09-28 17:44:34 -070063 ext.buildToolsVersion = '27.0.0'
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070064 final String fullSdkPath = getFullSdkPath();
Yigit Boyar88c16ce2017-02-08 16:06:14 -080065 if (file(fullSdkPath).exists()) {
66 gradle.ext.currentSdk = 26
Alan Viveretteeb9f3322017-03-09 16:29:57 -050067 project.ext.androidJar =
68 files("${fullSdkPath}/platforms/android-${gradle.currentSdk}/android.jar")
69 project.ext.androidSrcJar =
70 file("${fullSdkPath}/platforms/android-${gradle.currentSdk}/android-stubs-src.jar")
Alan Viverette810a1482017-03-20 12:43:43 -040071 project.ext.androidApiTxt = null
Yigit Boyar88c16ce2017-02-08 16:06:14 -080072 File props = file("local.properties")
73 props.write "sdk.dir=${fullSdkPath}"
Aurimas Liutikas3c666002017-03-08 19:30:28 -080074 ext.usingFullSdk = true
Yigit Boyar88c16ce2017-02-08 16:06:14 -080075 } else {
76 gradle.ext.currentSdk = 'current'
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -070077 project.ext.androidJar = files("${repos.prebuiltsRoot}/sdk/current/android.jar")
Alan Viverette810a1482017-03-20 12:43:43 -040078 project.ext.androidSrcJar = null
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -070079 project.ext.androidApiTxt = file("${repos.prebuiltsRoot}/sdk/api/26.txt")
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070080 System.setProperty('android.dir', "${supportRootFolder}/../../")
Yigit Boyar88c16ce2017-02-08 16:06:14 -080081 File props = file("local.properties")
82 props.write "android.dir=../../"
Aurimas Liutikas3c666002017-03-08 19:30:28 -080083 ext.usingFullSdk = false
Yigit Boyar88c16ce2017-02-08 16:06:14 -080084 }
85}
86
87def setupRepoOutAndBuildNumber() {
Yigit Boyaref300662017-05-01 11:52:07 -070088 // common support repo folder which works well for prebuilts.
Yigit Boyar88c16ce2017-02-08 16:06:14 -080089 ext.supportRepoOut = ''
Aurimas Liutikas76542da2017-06-29 17:00:29 -070090 ext.buildNumber = "0"
Yigit Boyar88c16ce2017-02-08 16:06:14 -080091 /*
92 * With the build server you are given two env variables.
93 * The OUT_DIR is a temporary directory you can use to put things during the build.
94 * The DIST_DIR is where you want to save things from the build.
95 *
96 * The build server will copy the contents of DIST_DIR to somewhere and make it available.
97 */
Yigit Boyar7bfacb72017-03-02 14:27:41 -080098 if (ext.runningInBuildServer) {
Yigit Boyar88c16ce2017-02-08 16:06:14 -080099 buildDir = new File(System.env.OUT_DIR + '/gradle/frameworks/support/build')
100 .getCanonicalFile()
101 project.ext.distDir = new File(System.env.DIST_DIR).getCanonicalFile()
102
103 // the build server does not pass the build number so we infer it from the last folder of
104 // the dist path.
105 ext.buildNumber = project.ext.distDir.getName()
Aurimas Liutikasa0150042017-04-21 09:46:12 -0700106
107 // the build server should always print out full stack traces for any failures.
108 gradle.startParameter.showStacktrace = ShowStacktrace.ALWAYS
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800109 } else {
110 buildDir = file("${ext.supportRootFolder}/../../out/host/gradle/frameworks/support/build")
111 project.ext.distDir = new File("${ext.supportRootFolder}/../../out/dist")
112 }
113 subprojects {
114 // Change buildDir first so that all plugins pick up the new value.
115 project.buildDir = new File("$project.parent.buildDir/../$project.name/build")
116 }
117 ext.supportRepoOut = new File(buildDir, 'support_repo')
118 ext.testApkDistOut = ext.distDir
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800119 ext.testResultsDistDir = new File(distDir, "host-test-reports")
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800120 ext.docsDir = new File(buildDir, 'javadoc')
121}
122
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700123def configureBuildOnServer() {
124 def buildOnServerTask = rootProject.tasks.create("buildOnServer")
125 rootProject.tasks.whenTaskAdded { task ->
Aurimas Liutikas212905f2017-10-09 15:28:28 -0700126 if ("createArchive".equals(task.name) || "distDocs".equals(task.name)) {
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700127 buildOnServerTask.dependsOn task
128 }
129 }
130
131 subprojects {
132 project.tasks.whenTaskAdded { task ->
133 if ("assembleErrorProne".equals(task.name) || "assembleAndroidTest".equals(task.name)) {
134 buildOnServerTask.dependsOn task
135 }
136 }
137 }
Aurimas Liutikas7cf34972017-12-18 09:27:47 -0800138 buildOnServerTask.dependsOn createJacocoAntUberJarTask()
139}
140
141def createJacocoAntUberJarTask() {
142 def myJacoco = project.configurations.create('myJacoco')
143 project.dependencies.add('myJacoco', build_libs.jacoco_ant)
144
145 return project.tasks.create(
146 name: "JacocoAntUberJar",
147 type: Jar) {
148 inputs.files myJacoco
149 from {
150 myJacoco
151 .resolvedConfiguration
152 .resolvedArtifacts.collect{ zipTree(it.getFile()) }} {
153 // exclude all the signatures the jar might have
154 exclude "META-INF/*.SF"
155 exclude "META-INF/*.DSA"
156 exclude "META-INF/*.RSA"
157 }
158 destinationDir file(project.distDir)
159 archiveName "jacocoant.jar"
160 }
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700161}
162
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800163def configureSubProjects() {
164 // lint every library
165 def lintTask = project.tasks.create("lint")
166 subprojects {
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700167 repos.addMavenRepositories(repositories)
168
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800169 // Only modify Android projects.
Aurimas Liutikaseca7a072017-07-11 01:11:52 +0000170 if (project.name.equals('doclava')
171 || project.name.equals('jdiff')
Hyundo Moonda9ee6b22017-07-21 14:32:12 +0900172 || project.name.equals('noto-emoji-compat')
173 || project.name.equals('support-media-compat-test-lib')) {
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800174 // disable tests and return
175 project.tasks.whenTaskAdded { task ->
176 if (task instanceof org.gradle.api.tasks.testing.Test) {
177 task.enabled = false
178 }
179 }
180 return
181 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800182
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800183 project.ext.currentSdk = gradle.ext.currentSdk
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800184
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800185 project.plugins.whenPluginAdded { plugin ->
186 def isAndroidLibrary = "com.android.build.gradle.LibraryPlugin"
187 .equals(plugin.class.name)
188 def isAndroidApp = "com.android.build.gradle.AppPlugin".equals(plugin.class.name)
189 def isJavaLibrary = "org.gradle.api.plugins.JavaPlugin".equals(plugin.class.name)
190
191 if (isAndroidLibrary || isAndroidApp) {
192 project.android.buildToolsVersion = rootProject.buildToolsVersion
193
194 // Enable code coverage for debug builds only if we are not running inside the IDE,
195 // since enabling coverage reports breaks the method parameter resolution in the IDE
196 // debugger.
197 project.android.buildTypes.debug.testCoverageEnabled =
Sergey Vasilinetsac8e72b2017-04-26 15:55:17 -0700198 !project.hasProperty('android.injected.invoked.from.ide')
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800199
200 // Copy the class files in a jar to be later used to generate code coverage report
201 project.android.testVariants.all { v ->
202 // check if the variant has any source files
203 // and test coverage is enabled
204 if (v.buildType.testCoverageEnabled
205 && v.sourceSets.any { !it.java.sourceFiles.isEmpty() }) {
206 def jarifyTask = project.tasks.create(
207 name: "package${v.name.capitalize()}ClassFilesForCoverageReport",
208 type: Jar) {
209 from v.testedVariant.javaCompile.destinationDir
210 exclude "**/R.class"
211 exclude "**/R\$*.class"
212 exclude "**/BuildConfig.class"
213 destinationDir file(project.distDir)
Aurimas Liutikas76542da2017-06-29 17:00:29 -0700214 archiveName "${project.name}-${v.baseName}-allclasses.jar"
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800215 }
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700216
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800217 jarifyTask.dependsOn v.getJavaCompiler()
Aurimas Liutikas7cf34972017-12-18 09:27:47 -0800218 v.assemble.dependsOn jarifyTask
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800219 }
220 }
221
222 // Enforce NewApi lint check as fatal.
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800223 project.android.lintOptions.fatal 'NewApi'
Aurimas Liutikas6c625702017-09-20 14:35:47 -0700224 lintTask.dependsOn {project.lint}
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800225 }
226
227 if (isAndroidLibrary || isJavaLibrary) {
228 // Add library to the aggregate dependency report.
229 task allDeps(type: DependencyReportTask) {}
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800230
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700231 project.afterEvaluate {
232 Upload uploadTask = (Upload) project.tasks.uploadArchives;
233 uploadTask.repositories.mavenDeployer {
Aurimas Liutikas279780d2017-08-09 14:03:59 -0700234 repository(url: uri("$rootProject.ext.supportRepoOut"))
235 setUniqueVersion(true)
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700236 }
237
238 // Before the upload, make sure the repo is ready.
239 uploadTask.dependsOn rootProject.tasks.prepareRepo
240
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700241 // Make the mainupload depend on this one.
242 mainUpload.dependsOn uploadTask
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800243 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800244 }
245 }
246
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800247 // Copy instrumentation test APKs and app APKs into the dist dir
248 // For test apks, they are uploaded only if we have java test sources.
249 // For regular app apks, they are uploaded only if they have java sources.
250 project.tasks.whenTaskAdded { task ->
251 if (task.name.startsWith("packageDebug")) {
252 def testApk = task.name.contains("AndroidTest")
253 task.doLast {
254 def source = testApk ? project.android.sourceSets.androidTest
255 : project.android.sourceSets.main
Yigit Boyare606d6d2017-09-07 12:37:36 -0700256 def hasKotlinSources = false
257 if (source.hasProperty('kotlin')) {
258 if (!source.kotlin.files.isEmpty()) {
259 hasKotlinSources = true
260 } else {
261 // kotlin files does not show in java sources due to the *.java filter
262 // so we need to check them manually
263 hasKotlinSources = source.java.sourceDirectoryTrees.any {
264 !fileTree(dir: it.dir, include:'**/*.kt').files.isEmpty()
265 }
266 }
267 }
268 def hasSourceCode = !source.java.sourceFiles.isEmpty() || hasKotlinSources
269 if (task.hasProperty("outputDirectory") && (hasSourceCode || !testApk)) {
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800270 copy {
Aurimas Liutikasef3b4922017-06-28 10:44:59 -0700271 from(task.outputDirectory)
272 include '*.apk'
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800273 into(rootProject.ext.testApkDistOut)
274 rename { String fileName ->
Hyundo Moon9938e172017-07-26 20:51:11 +0900275 // Exclude media-compat-test-* modules from existing support library
276 // presubmit tests.
277 if (fileName.contains("media-compat-test")) {
278 fileName.replace("-debug-androidTest", "")
Yigit Boyar96a0fcd2017-08-25 18:18:28 -0700279 } else {
280 // multiple modules may have the same name so prefix the name with
281 // the module's path to ensure it is unique.
282 // e.g. palette-v7-debug-androidTest.apk becomes
283 // support-palette-v7_palette-v7-debug-androidTest.apk
284 "${project.getPath().replace(':', '-').substring(1)}_${fileName}"
Hyundo Moon9938e172017-07-26 20:51:11 +0900285 }
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800286 }
287 }
288 }
289 }
290 }
291 }
292
293 // copy host side test results to DIST
294 project.tasks.whenTaskAdded { task ->
295 if (task instanceof org.gradle.api.tasks.testing.Test) {
296 def junitReport = task.reports.junitXml
297 if (junitReport.enabled) {
298 def zipTask = project.tasks.create(name : "zipResultsOf${task.name.capitalize()}", type : Zip) {
299 destinationDir(testResultsDistDir)
Yigit Boyar278676d2017-03-10 15:29:11 -0800300 // first one is always :, drop it.
301 archiveName("${project.getPath().split(":").join("_").substring(1)}.zip")
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800302 }
303 if (project.rootProject.ext.runningInBuildServer) {
304 task.ignoreFailures = true
305 }
306 task.finalizedBy zipTask
307 task.doFirst {
308 zipTask.from(junitReport.destination)
309 }
310 }
311 }
312 }
313
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800314 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
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700344ext.init.configureSubProjects = this.&configureSubProjects
Yigit Boyare1bbf712017-03-08 13:52:37 -0800345ext.init.configureBuildOnServer = this.&configureBuildOnServer