blob: 6769e2a29ca232ede691bb84bb9c55edd3785e12 [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
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
Alan Viverette1b862372017-03-22 11:32:28 -040033ext.init.debugKeystore = file("${supportRoot}/development/keystore/debug.keystore")
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 Liutikas62d3e1d2017-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 Liutikas62d3e1d2017-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 Liutikasd1aad142017-11-28 16:27:27 -080067 ext.buildToolsVersion = '27.0.1'
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070068 final String fullSdkPath = getFullSdkPath();
Yigit Boyar88c16ce2017-02-08 16:06:14 -080069 if (file(fullSdkPath).exists()) {
70 gradle.ext.currentSdk = 26
Alan Viveretteeb9f3322017-03-09 16:29:57 -050071 project.ext.androidJar =
72 files("${fullSdkPath}/platforms/android-${gradle.currentSdk}/android.jar")
73 project.ext.androidSrcJar =
74 file("${fullSdkPath}/platforms/android-${gradle.currentSdk}/android-stubs-src.jar")
Alan Viverette810a1482017-03-20 12:43:43 -040075 project.ext.androidApiTxt = null
Yigit Boyar88c16ce2017-02-08 16:06:14 -080076 File props = file("local.properties")
77 props.write "sdk.dir=${fullSdkPath}"
Aurimas Liutikas3c666002017-03-08 19:30:28 -080078 ext.usingFullSdk = true
Yigit Boyar88c16ce2017-02-08 16:06:14 -080079 } else {
80 gradle.ext.currentSdk = 'current'
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -070081 project.ext.androidJar = files("${repos.prebuiltsRoot}/sdk/current/android.jar")
Alan Viverette810a1482017-03-20 12:43:43 -040082 project.ext.androidSrcJar = null
Aurimas Liutikas9ab3b4c32017-04-19 09:33:27 -070083 project.ext.androidApiTxt = file("${repos.prebuiltsRoot}/sdk/api/26.txt")
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070084 System.setProperty('android.dir', "${supportRootFolder}/../../")
Yigit Boyar88c16ce2017-02-08 16:06:14 -080085 File props = file("local.properties")
86 props.write "android.dir=../../"
Aurimas Liutikas3c666002017-03-08 19:30:28 -080087 ext.usingFullSdk = false
Yigit Boyar88c16ce2017-02-08 16:06:14 -080088 }
89}
90
91def setupRepoOutAndBuildNumber() {
Yigit Boyaref300662017-05-01 11:52:07 -070092 // common support repo folder which works well for prebuilts.
Yigit Boyar88c16ce2017-02-08 16:06:14 -080093 ext.supportRepoOut = ''
Aurimas Liutikas76542da2017-06-29 17:00:29 -070094 ext.buildNumber = "0"
Yigit Boyar88c16ce2017-02-08 16:06:14 -080095 /*
96 * With the build server you are given two env variables.
97 * The OUT_DIR is a temporary directory you can use to put things during the build.
98 * The DIST_DIR is where you want to save things from the build.
99 *
100 * The build server will copy the contents of DIST_DIR to somewhere and make it available.
101 */
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800102 if (ext.runningInBuildServer) {
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800103 buildDir = new File(System.env.OUT_DIR + '/gradle/frameworks/support/build')
104 .getCanonicalFile()
105 project.ext.distDir = new File(System.env.DIST_DIR).getCanonicalFile()
106
107 // the build server does not pass the build number so we infer it from the last folder of
108 // the dist path.
109 ext.buildNumber = project.ext.distDir.getName()
Aurimas Liutikasa0150042017-04-21 09:46:12 -0700110
111 // the build server should always print out full stack traces for any failures.
112 gradle.startParameter.showStacktrace = ShowStacktrace.ALWAYS
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800113 } else {
114 buildDir = file("${ext.supportRootFolder}/../../out/host/gradle/frameworks/support/build")
115 project.ext.distDir = new File("${ext.supportRootFolder}/../../out/dist")
116 }
117 subprojects {
118 // Change buildDir first so that all plugins pick up the new value.
119 project.buildDir = new File("$project.parent.buildDir/../$project.name/build")
120 }
121 ext.supportRepoOut = new File(buildDir, 'support_repo')
122 ext.testApkDistOut = ext.distDir
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800123 ext.testResultsDistDir = new File(distDir, "host-test-reports")
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800124 ext.docsDir = new File(buildDir, 'javadoc')
125}
126
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700127def configureBuildOnServer() {
128 def buildOnServerTask = rootProject.tasks.create("buildOnServer")
129 rootProject.tasks.whenTaskAdded { task ->
Aurimas Liutikas212905f2017-10-09 15:28:28 -0700130 if ("createArchive".equals(task.name) || "distDocs".equals(task.name)) {
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700131 buildOnServerTask.dependsOn task
132 }
133 }
134
135 subprojects {
136 project.tasks.whenTaskAdded { task ->
137 if ("assembleErrorProne".equals(task.name) || "assembleAndroidTest".equals(task.name)) {
138 buildOnServerTask.dependsOn task
139 }
140 }
141 }
Aurimas Liutikas7cf34972017-12-18 09:27:47 -0800142 buildOnServerTask.dependsOn createJacocoAntUberJarTask()
143}
144
145def createJacocoAntUberJarTask() {
146 def myJacoco = project.configurations.create('myJacoco')
147 project.dependencies.add('myJacoco', build_libs.jacoco_ant)
148
149 return project.tasks.create(
150 name: "JacocoAntUberJar",
151 type: Jar) {
152 inputs.files myJacoco
153 from {
154 myJacoco
155 .resolvedConfiguration
156 .resolvedArtifacts.collect{ zipTree(it.getFile()) }} {
157 // exclude all the signatures the jar might have
158 exclude "META-INF/*.SF"
159 exclude "META-INF/*.DSA"
160 exclude "META-INF/*.RSA"
161 }
162 destinationDir file(project.distDir)
163 archiveName "jacocoant.jar"
164 }
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700165}
166
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800167def configureSubProjects() {
168 // lint every library
169 def lintTask = project.tasks.create("lint")
170 subprojects {
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700171 repos.addMavenRepositories(repositories)
172
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800173 // Only modify Android projects.
Aurimas Liutikas6319e4c2017-12-18 21:08:02 -0800174 if (project.name.equals('noto-emoji-compat')) {
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800175 // disable tests and return
176 project.tasks.whenTaskAdded { task ->
177 if (task instanceof org.gradle.api.tasks.testing.Test) {
178 task.enabled = false
179 }
180 }
181 return
182 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800183
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800184 project.ext.currentSdk = gradle.ext.currentSdk
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800185
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800186 project.plugins.whenPluginAdded { plugin ->
187 def isAndroidLibrary = "com.android.build.gradle.LibraryPlugin"
188 .equals(plugin.class.name)
189 def isAndroidApp = "com.android.build.gradle.AppPlugin".equals(plugin.class.name)
190 def isJavaLibrary = "org.gradle.api.plugins.JavaPlugin".equals(plugin.class.name)
191
192 if (isAndroidLibrary || isAndroidApp) {
193 project.android.buildToolsVersion = rootProject.buildToolsVersion
194
195 // Enable code coverage for debug builds only if we are not running inside the IDE,
196 // since enabling coverage reports breaks the method parameter resolution in the IDE
197 // debugger.
198 project.android.buildTypes.debug.testCoverageEnabled =
Sergey Vasilinetsac8e72b2017-04-26 15:55:17 -0700199 !project.hasProperty('android.injected.invoked.from.ide')
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800200
201 // Copy the class files in a jar to be later used to generate code coverage report
202 project.android.testVariants.all { v ->
203 // check if the variant has any source files
204 // and test coverage is enabled
205 if (v.buildType.testCoverageEnabled
206 && v.sourceSets.any { !it.java.sourceFiles.isEmpty() }) {
207 def jarifyTask = project.tasks.create(
208 name: "package${v.name.capitalize()}ClassFilesForCoverageReport",
209 type: Jar) {
210 from v.testedVariant.javaCompile.destinationDir
211 exclude "**/R.class"
212 exclude "**/R\$*.class"
213 exclude "**/BuildConfig.class"
214 destinationDir file(project.distDir)
Aurimas Liutikas76542da2017-06-29 17:00:29 -0700215 archiveName "${project.name}-${v.baseName}-allclasses.jar"
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800216 }
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700217
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800218 jarifyTask.dependsOn v.getJavaCompiler()
Aurimas Liutikas7cf34972017-12-18 09:27:47 -0800219 v.assemble.dependsOn jarifyTask
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800220 }
221 }
222
223 // Enforce NewApi lint check as fatal.
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800224 project.android.lintOptions.fatal 'NewApi'
Aurimas Liutikas6c625702017-09-20 14:35:47 -0700225 lintTask.dependsOn {project.lint}
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800226 }
227
228 if (isAndroidLibrary || isJavaLibrary) {
229 // Add library to the aggregate dependency report.
230 task allDeps(type: DependencyReportTask) {}
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800231
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700232 project.afterEvaluate {
233 Upload uploadTask = (Upload) project.tasks.uploadArchives;
234 uploadTask.repositories.mavenDeployer {
Aurimas Liutikas279780d2017-08-09 14:03:59 -0700235 repository(url: uri("$rootProject.ext.supportRepoOut"))
236 setUniqueVersion(true)
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700237 }
238
239 // Before the upload, make sure the repo is ready.
240 uploadTask.dependsOn rootProject.tasks.prepareRepo
241
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700242 // Make the mainupload depend on this one.
243 mainUpload.dependsOn uploadTask
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800244 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800245 }
246 }
247
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800248 // Copy instrumentation test APKs and app APKs into the dist dir
249 // For test apks, they are uploaded only if we have java test sources.
250 // For regular app apks, they are uploaded only if they have java sources.
251 project.tasks.whenTaskAdded { task ->
252 if (task.name.startsWith("packageDebug")) {
253 def testApk = task.name.contains("AndroidTest")
254 task.doLast {
255 def source = testApk ? project.android.sourceSets.androidTest
256 : project.android.sourceSets.main
Yigit Boyare606d6d2017-09-07 12:37:36 -0700257 def hasKotlinSources = false
258 if (source.hasProperty('kotlin')) {
259 if (!source.kotlin.files.isEmpty()) {
260 hasKotlinSources = true
261 } else {
262 // kotlin files does not show in java sources due to the *.java filter
263 // so we need to check them manually
264 hasKotlinSources = source.java.sourceDirectoryTrees.any {
265 !fileTree(dir: it.dir, include:'**/*.kt').files.isEmpty()
266 }
267 }
268 }
269 def hasSourceCode = !source.java.sourceFiles.isEmpty() || hasKotlinSources
270 if (task.hasProperty("outputDirectory") && (hasSourceCode || !testApk)) {
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800271 copy {
Aurimas Liutikasef3b4922017-06-28 10:44:59 -0700272 from(task.outputDirectory)
273 include '*.apk'
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800274 into(rootProject.ext.testApkDistOut)
275 rename { String fileName ->
Hyundo Moon9938e172017-07-26 20:51:11 +0900276 // Exclude media-compat-test-* modules from existing support library
277 // presubmit tests.
278 if (fileName.contains("media-compat-test")) {
279 fileName.replace("-debug-androidTest", "")
Yigit Boyar96a0fcd2017-08-25 18:18:28 -0700280 } else {
281 // multiple modules may have the same name so prefix the name with
282 // the module's path to ensure it is unique.
283 // e.g. palette-v7-debug-androidTest.apk becomes
284 // support-palette-v7_palette-v7-debug-androidTest.apk
285 "${project.getPath().replace(':', '-').substring(1)}_${fileName}"
Hyundo Moon9938e172017-07-26 20:51:11 +0900286 }
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800287 }
288 }
289 }
290 }
291 }
292 }
293
294 // copy host side test results to DIST
295 project.tasks.whenTaskAdded { task ->
296 if (task instanceof org.gradle.api.tasks.testing.Test) {
297 def junitReport = task.reports.junitXml
298 if (junitReport.enabled) {
299 def zipTask = project.tasks.create(name : "zipResultsOf${task.name.capitalize()}", type : Zip) {
300 destinationDir(testResultsDistDir)
Yigit Boyar278676d2017-03-10 15:29:11 -0800301 // first one is always :, drop it.
302 archiveName("${project.getPath().split(":").join("_").substring(1)}.zip")
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800303 }
304 if (project.rootProject.ext.runningInBuildServer) {
305 task.ignoreFailures = true
306 }
307 task.finalizedBy zipTask
308 task.doFirst {
309 zipTask.from(junitReport.destination)
310 }
311 }
312 }
313 }
314
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800315 project.afterEvaluate { p ->
316 // remove dependency on the test so that we still get coverage even if some tests fail
317 p.tasks.findAll { it instanceof JacocoReportTask }.each { task ->
318 def toBeRemoved = new ArrayList()
319 def dependencyList = task.taskDependencies.values
320 dependencyList.each { dep ->
321 if (dep instanceof String) {
322 def t = tasks.findByName(dep)
323 if (t instanceof DeviceProviderInstrumentTestTask) {
324 toBeRemoved.add(dep)
325 task.mustRunAfter(t)
326 }
327 }
328 }
329 toBeRemoved.each { dep ->
330 dependencyList.remove(dep)
331 }
332 }
333 }
334 }
335}
336
337def setupRelease() {
338 apply from: "${ext.supportRootFolder}/buildSrc/release.gradle"
339}
340
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800341ext.init.enableDoclavaAndJDiff = this.&enableDoclavaAndJDiff
342ext.init.setSdkInLocalPropertiesFile = this.&setSdkInLocalPropertiesFile
343ext.init.setupRepoOutAndBuildNumber = this.&setupRepoOutAndBuildNumber
344ext.init.setupRelease = this.&setupRelease
Aurimas Liutikas38e8f4d92017-06-16 08:31:03 -0700345ext.init.configureSubProjects = this.&configureSubProjects
Yigit Boyare1bbf712017-03-08 13:52:37 -0800346ext.init.configureBuildOnServer = this.&configureBuildOnServer