blob: a98dbeeb6635c9b9e5b7bdfebe00dac7134b14c0 [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 Liutikas62d3e1d2017-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 Liutikas62d3e1d2017-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 Liutikas62d3e1d2017-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 Liutikasd1aad142017-11-28 16:27:27 -080063 ext.buildToolsVersion = '27.0.1'
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 Moonbeffc442017-11-06 15:08:43 +0900172 || project.name.equals('noto-emoji-compat')) {
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800173 // disable tests and return
174 project.tasks.whenTaskAdded { task ->
175 if (task instanceof org.gradle.api.tasks.testing.Test) {
176 task.enabled = false
177 }
178 }
179 return
180 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800181
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800182 project.ext.currentSdk = gradle.ext.currentSdk
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800183
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800184 project.plugins.whenPluginAdded { plugin ->
185 def isAndroidLibrary = "com.android.build.gradle.LibraryPlugin"
186 .equals(plugin.class.name)
187 def isAndroidApp = "com.android.build.gradle.AppPlugin".equals(plugin.class.name)
188 def isJavaLibrary = "org.gradle.api.plugins.JavaPlugin".equals(plugin.class.name)
189
190 if (isAndroidLibrary || isAndroidApp) {
191 project.android.buildToolsVersion = rootProject.buildToolsVersion
192
193 // Enable code coverage for debug builds only if we are not running inside the IDE,
194 // since enabling coverage reports breaks the method parameter resolution in the IDE
195 // debugger.
196 project.android.buildTypes.debug.testCoverageEnabled =
Sergey Vasilinetsac8e72b2017-04-26 15:55:17 -0700197 !project.hasProperty('android.injected.invoked.from.ide')
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800198
199 // Copy the class files in a jar to be later used to generate code coverage report
200 project.android.testVariants.all { v ->
201 // check if the variant has any source files
202 // and test coverage is enabled
203 if (v.buildType.testCoverageEnabled
204 && v.sourceSets.any { !it.java.sourceFiles.isEmpty() }) {
205 def jarifyTask = project.tasks.create(
206 name: "package${v.name.capitalize()}ClassFilesForCoverageReport",
207 type: Jar) {
208 from v.testedVariant.javaCompile.destinationDir
209 exclude "**/R.class"
210 exclude "**/R\$*.class"
211 exclude "**/BuildConfig.class"
212 destinationDir file(project.distDir)
Aurimas Liutikas76542da2017-06-29 17:00:29 -0700213 archiveName "${project.name}-${v.baseName}-allclasses.jar"
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800214 }
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700215
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800216 jarifyTask.dependsOn v.getJavaCompiler()
Aurimas Liutikas7cf34972017-12-18 09:27:47 -0800217 v.assemble.dependsOn jarifyTask
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800218 }
219 }
220
221 // Enforce NewApi lint check as fatal.
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800222 project.android.lintOptions.fatal 'NewApi'
Aurimas Liutikas6c625702017-09-20 14:35:47 -0700223 lintTask.dependsOn {project.lint}
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800224 }
225
226 if (isAndroidLibrary || isJavaLibrary) {
227 // Add library to the aggregate dependency report.
228 task allDeps(type: DependencyReportTask) {}
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800229
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700230 project.afterEvaluate {
231 Upload uploadTask = (Upload) project.tasks.uploadArchives;
232 uploadTask.repositories.mavenDeployer {
Aurimas Liutikas279780d2017-08-09 14:03:59 -0700233 repository(url: uri("$rootProject.ext.supportRepoOut"))
234 setUniqueVersion(true)
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700235 }
236
237 // Before the upload, make sure the repo is ready.
238 uploadTask.dependsOn rootProject.tasks.prepareRepo
239
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700240 // Make the mainupload depend on this one.
241 mainUpload.dependsOn uploadTask
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800242 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800243 }
244 }
245
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800246 // Copy instrumentation test APKs and app APKs into the dist dir
247 // For test apks, they are uploaded only if we have java test sources.
248 // For regular app apks, they are uploaded only if they have java sources.
249 project.tasks.whenTaskAdded { task ->
250 if (task.name.startsWith("packageDebug")) {
251 def testApk = task.name.contains("AndroidTest")
252 task.doLast {
253 def source = testApk ? project.android.sourceSets.androidTest
254 : project.android.sourceSets.main
Yigit Boyare606d6d2017-09-07 12:37:36 -0700255 def hasKotlinSources = false
256 if (source.hasProperty('kotlin')) {
257 if (!source.kotlin.files.isEmpty()) {
258 hasKotlinSources = true
259 } else {
260 // kotlin files does not show in java sources due to the *.java filter
261 // so we need to check them manually
262 hasKotlinSources = source.java.sourceDirectoryTrees.any {
263 !fileTree(dir: it.dir, include:'**/*.kt').files.isEmpty()
264 }
265 }
266 }
267 def hasSourceCode = !source.java.sourceFiles.isEmpty() || hasKotlinSources
268 if (task.hasProperty("outputDirectory") && (hasSourceCode || !testApk)) {
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800269 copy {
Aurimas Liutikasef3b4922017-06-28 10:44:59 -0700270 from(task.outputDirectory)
271 include '*.apk'
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800272 into(rootProject.ext.testApkDistOut)
273 rename { String fileName ->
Hyundo Moon9938e172017-07-26 20:51:11 +0900274 // Exclude media-compat-test-* modules from existing support library
275 // presubmit tests.
276 if (fileName.contains("media-compat-test")) {
277 fileName.replace("-debug-androidTest", "")
Yigit Boyar96a0fcd2017-08-25 18:18:28 -0700278 } else {
279 // multiple modules may have the same name so prefix the name with
280 // the module's path to ensure it is unique.
281 // e.g. palette-v7-debug-androidTest.apk becomes
282 // support-palette-v7_palette-v7-debug-androidTest.apk
283 "${project.getPath().replace(':', '-').substring(1)}_${fileName}"
Hyundo Moon9938e172017-07-26 20:51:11 +0900284 }
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800285 }
286 }
287 }
288 }
289 }
290 }
291
292 // copy host side test results to DIST
293 project.tasks.whenTaskAdded { task ->
294 if (task instanceof org.gradle.api.tasks.testing.Test) {
295 def junitReport = task.reports.junitXml
296 if (junitReport.enabled) {
297 def zipTask = project.tasks.create(name : "zipResultsOf${task.name.capitalize()}", type : Zip) {
298 destinationDir(testResultsDistDir)
Yigit Boyar278676d2017-03-10 15:29:11 -0800299 // first one is always :, drop it.
300 archiveName("${project.getPath().split(":").join("_").substring(1)}.zip")
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800301 }
302 if (project.rootProject.ext.runningInBuildServer) {
303 task.ignoreFailures = true
304 }
305 task.finalizedBy zipTask
306 task.doFirst {
307 zipTask.from(junitReport.destination)
308 }
309 }
310 }
311 }
312
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800313 project.afterEvaluate { p ->
314 // remove dependency on the test so that we still get coverage even if some tests fail
315 p.tasks.findAll { it instanceof JacocoReportTask }.each { task ->
316 def toBeRemoved = new ArrayList()
317 def dependencyList = task.taskDependencies.values
318 dependencyList.each { dep ->
319 if (dep instanceof String) {
320 def t = tasks.findByName(dep)
321 if (t instanceof DeviceProviderInstrumentTestTask) {
322 toBeRemoved.add(dep)
323 task.mustRunAfter(t)
324 }
325 }
326 }
327 toBeRemoved.each { dep ->
328 dependencyList.remove(dep)
329 }
330 }
331 }
332 }
333}
334
335def setupRelease() {
336 apply from: "${ext.supportRootFolder}/buildSrc/release.gradle"
337}
338
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800339ext.init.enableDoclavaAndJDiff = this.&enableDoclavaAndJDiff
340ext.init.setSdkInLocalPropertiesFile = this.&setSdkInLocalPropertiesFile
341ext.init.setupRepoOutAndBuildNumber = this.&setupRepoOutAndBuildNumber
342ext.init.setupRelease = this.&setupRelease
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700343ext.init.configureSubProjects = this.&configureSubProjects
Yigit Boyare1bbf712017-03-08 13:52:37 -0800344ext.init.configureBuildOnServer = this.&configureBuildOnServer