blob: 38b41e8bce11a1598ae16d92d233b6baa8578a92 [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 Boyarc62cb532018-02-12 18:09:25 -080020import android.support.license.CheckExternalDependencyLicensesTask
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
Aurimas Liutikas5f3a2772018-01-05 19:07:40 -080034rootProject.ext.versionChecker = new GMavenVersionChecker(rootProject.logger)
Aurimas Liutikas9ab3b4c2017-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"
Aurimas Liutikasa2cbbfa2018-01-25 14:42:41 -080039apply from: "${supportRoot}/buildSrc/unbundled_check.gradle"
40
Yigit Boyar88c16ce2017-02-08 16:06:14 -080041
Sergey Vasilinets16385352017-12-13 16:56:47 -080042def enableDoclavaAndJDiff(p, dacOptions) {
Yigit Boyar88c16ce2017-02-08 16:06:14 -080043 p.configurations {
44 doclava
45 jdiff
46 }
47
48 p.dependencies {
Aurimas Liutikas419f9932017-12-18 12:53:17 -080049 doclava build_libs.doclava
50 // tools.jar required for com.sun.javadoc
51 doclava files(((URLClassLoader) ToolProvider.getSystemToolClassLoader()).getURLs())
52 jdiff build_libs.jdiff
Aurimas Liutikas62d3e1d2017-11-28 15:28:01 -080053 jdiff build_libs.xml_parser_apis
54 jdiff build_libs.xerces_impl
Yigit Boyar88c16ce2017-02-08 16:06:14 -080055 }
Sergey Vasilinets16385352017-12-13 16:56:47 -080056 def allChecks = DiffAndDocs.configureDiffAndDocs(rootProject, supportRootFolder, dacOptions)
57 createArchive.dependsOn(allChecks)
Yigit Boyar88c16ce2017-02-08 16:06:14 -080058}
59
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -070060def getFullSdkPath() {
Aurimas Liutikasa2cbbfa2018-01-25 14:42:41 -080061 if (isUnbundledBuild(ext.supportRootFolder)) {
62 Properties properties = new Properties()
63 File propertiesFile = new File('local.properties')
64 if (propertiesFile.exists()) {
65 propertiesFile.withInputStream {
66 properties.load(it)
67 }
68 }
69 File location = findSdkLocation(properties, supportRootFolder)
70 return location.getAbsolutePath()
71 } else {
72 final String osName = System.getProperty("os.name").toLowerCase();
73 final boolean isMacOsX =
74 osName.contains("mac os x") || osName.contains("darwin") || osName.contains("osx");
75 final String platform = isMacOsX ? 'darwin' : 'linux'
76 return "${repos.prebuiltsRoot}/fullsdk-${platform}"
77 }
78}
79
80/**
81 * Adapted from com.android.build.gradle.internal.SdkHandler
82 */
83public static File findSdkLocation(Properties properties, File rootDir) {
84 String sdkDirProp = properties.getProperty("sdk.dir");
85 if (sdkDirProp != null) {
86 File sdk = new File(sdkDirProp);
87 if (!sdk.isAbsolute()) {
88 sdk = new File(rootDir, sdkDirProp);
89 }
90 return sdk
91 }
92
93 sdkDirProp = properties.getProperty("android.dir");
94 if (sdkDirProp != null) {
95 return new File(rootDir, sdkDirProp);
96 }
97
98 String envVar = System.getenv("ANDROID_HOME");
99 if (envVar != null) {
100 return new File(envVar);
101 }
102
103 String property = System.getProperty("android.home");
104 if (property != null) {
105 return new File(property);
106 }
107 return null;
Aurimas Liutikasbb78f3f2017-06-19 11:30:37 -0700108}
109
110def setSdkInLocalPropertiesFile() {
Aurimas Liutikas3196ef12018-01-02 16:02:50 -0800111 final File fullSdkPath = file(getFullSdkPath())
112 if (fullSdkPath.exists()) {
113 project.ext.fullSdkPath = fullSdkPath
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800114 File props = file("local.properties")
Aurimas Liutikas3196ef12018-01-02 16:02:50 -0800115 props.write "sdk.dir=${fullSdkPath.getAbsolutePath()}"
Aurimas Liutikas3c666002017-03-08 19:30:28 -0800116 ext.usingFullSdk = true
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800117 } else {
Aurimas Liutikas3196ef12018-01-02 16:02:50 -0800118 throw Exception("You are using non ub-supportlib-* checkout. You need to check out "
119 + "ub-supportlib-* to work on support library. See go/supportlib for details.")
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800120 }
121}
122
123def setupRepoOutAndBuildNumber() {
Yigit Boyaref300662017-05-01 11:52:07 -0700124 // common support repo folder which works well for prebuilts.
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800125 ext.supportRepoOut = ''
Aurimas Liutikas76542da2017-06-29 17:00:29 -0700126 ext.buildNumber = "0"
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800127 /*
128 * With the build server you are given two env variables.
129 * The OUT_DIR is a temporary directory you can use to put things during the build.
130 * The DIST_DIR is where you want to save things from the build.
131 *
132 * The build server will copy the contents of DIST_DIR to somewhere and make it available.
133 */
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800134 if (ext.runningInBuildServer) {
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800135 buildDir = new File(System.env.OUT_DIR + '/gradle/frameworks/support/build')
136 .getCanonicalFile()
137 project.ext.distDir = new File(System.env.DIST_DIR).getCanonicalFile()
138
139 // the build server does not pass the build number so we infer it from the last folder of
140 // the dist path.
141 ext.buildNumber = project.ext.distDir.getName()
Aurimas Liutikasa0150042017-04-21 09:46:12 -0700142
143 // the build server should always print out full stack traces for any failures.
144 gradle.startParameter.showStacktrace = ShowStacktrace.ALWAYS
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800145 } else {
146 buildDir = file("${ext.supportRootFolder}/../../out/host/gradle/frameworks/support/build")
147 project.ext.distDir = new File("${ext.supportRootFolder}/../../out/dist")
148 }
149 subprojects {
150 // Change buildDir first so that all plugins pick up the new value.
151 project.buildDir = new File("$project.parent.buildDir/../$project.name/build")
152 }
153 ext.supportRepoOut = new File(buildDir, 'support_repo')
154 ext.testApkDistOut = ext.distDir
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800155 ext.testResultsDistDir = new File(distDir, "host-test-reports")
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800156 ext.docsDir = new File(buildDir, 'javadoc')
157}
158
Aurimas Liutikas38e8f4d2017-06-16 08:31:03 -0700159def configureBuildOnServer() {
160 def buildOnServerTask = rootProject.tasks.create("buildOnServer")
161 rootProject.tasks.whenTaskAdded { task ->
Yigit Boyarc62cb532018-02-12 18:09:25 -0800162 if ("createArchive".equals(task.name)
163 || "distDocs".equals(task.name)
164 || CheckExternalDependencyLicensesTask.ROOT_TASK_NAME.equals(task.name)) {
Aurimas Liutikas38e8f4d2017-06-16 08:31:03 -0700165 buildOnServerTask.dependsOn task
166 }
167 }
168
169 subprojects {
170 project.tasks.whenTaskAdded { task ->
171 if ("assembleErrorProne".equals(task.name) || "assembleAndroidTest".equals(task.name)) {
172 buildOnServerTask.dependsOn task
173 }
174 }
175 }
Aurimas Liutikas7cf34972017-12-18 09:27:47 -0800176 buildOnServerTask.dependsOn createJacocoAntUberJarTask()
177}
178
179def createJacocoAntUberJarTask() {
180 def myJacoco = project.configurations.create('myJacoco')
181 project.dependencies.add('myJacoco', build_libs.jacoco_ant)
182
183 return project.tasks.create(
184 name: "JacocoAntUberJar",
185 type: Jar) {
186 inputs.files myJacoco
187 from {
188 myJacoco
189 .resolvedConfiguration
190 .resolvedArtifacts.collect{ zipTree(it.getFile()) }} {
191 // exclude all the signatures the jar might have
192 exclude "META-INF/*.SF"
193 exclude "META-INF/*.DSA"
194 exclude "META-INF/*.RSA"
195 }
196 destinationDir file(project.distDir)
197 archiveName "jacocoant.jar"
198 }
Aurimas Liutikas38e8f4d2017-06-16 08:31:03 -0700199}
200
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800201def configureSubProjects() {
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800202 subprojects {
Aurimas Liutikas6509a1a2017-05-09 16:25:43 -0700203 repos.addMavenRepositories(repositories)
204
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800205 // Only modify Android projects.
Aurimas Liutikas6319e4c2017-12-18 21:08:02 -0800206 if (project.name.equals('noto-emoji-compat')) {
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800207 // disable tests and return
208 project.tasks.whenTaskAdded { task ->
209 if (task instanceof org.gradle.api.tasks.testing.Test) {
210 task.enabled = false
211 }
212 }
213 return
214 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800215
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800216 project.plugins.whenPluginAdded { plugin ->
217 def isAndroidLibrary = "com.android.build.gradle.LibraryPlugin"
218 .equals(plugin.class.name)
219 def isAndroidApp = "com.android.build.gradle.AppPlugin".equals(plugin.class.name)
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800220
221 if (isAndroidLibrary || isAndroidApp) {
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800222 // Enable code coverage for debug builds only if we are not running inside the IDE,
223 // since enabling coverage reports breaks the method parameter resolution in the IDE
224 // debugger.
225 project.android.buildTypes.debug.testCoverageEnabled =
Sergey Vasilinetsac8e72b2017-04-26 15:55:17 -0700226 !project.hasProperty('android.injected.invoked.from.ide')
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800227
228 // Copy the class files in a jar to be later used to generate code coverage report
229 project.android.testVariants.all { v ->
230 // check if the variant has any source files
231 // and test coverage is enabled
232 if (v.buildType.testCoverageEnabled
233 && v.sourceSets.any { !it.java.sourceFiles.isEmpty() }) {
234 def jarifyTask = project.tasks.create(
235 name: "package${v.name.capitalize()}ClassFilesForCoverageReport",
236 type: Jar) {
237 from v.testedVariant.javaCompile.destinationDir
238 exclude "**/R.class"
239 exclude "**/R\$*.class"
240 exclude "**/BuildConfig.class"
241 destinationDir file(project.distDir)
Aurimas Liutikas76542da2017-06-29 17:00:29 -0700242 archiveName "${project.name}-${v.baseName}-allclasses.jar"
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800243 }
Aurimas Liutikas21b7fce2017-06-23 09:52:08 -0700244
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800245 jarifyTask.dependsOn v.getJavaCompiler()
Aurimas Liutikas7cf34972017-12-18 09:27:47 -0800246 v.assemble.dependsOn jarifyTask
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800247 }
248 }
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800249 }
250 }
251
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800252 // Copy instrumentation test APKs and app APKs into the dist dir
253 // For test apks, they are uploaded only if we have java test sources.
254 // For regular app apks, they are uploaded only if they have java sources.
255 project.tasks.whenTaskAdded { task ->
256 if (task.name.startsWith("packageDebug")) {
257 def testApk = task.name.contains("AndroidTest")
258 task.doLast {
259 def source = testApk ? project.android.sourceSets.androidTest
260 : project.android.sourceSets.main
Yigit Boyare606d6d2017-09-07 12:37:36 -0700261 def hasKotlinSources = false
262 if (source.hasProperty('kotlin')) {
263 if (!source.kotlin.files.isEmpty()) {
264 hasKotlinSources = true
265 } else {
266 // kotlin files does not show in java sources due to the *.java filter
267 // so we need to check them manually
268 hasKotlinSources = source.java.sourceDirectoryTrees.any {
269 !fileTree(dir: it.dir, include:'**/*.kt').files.isEmpty()
270 }
271 }
272 }
273 def hasSourceCode = !source.java.sourceFiles.isEmpty() || hasKotlinSources
274 if (task.hasProperty("outputDirectory") && (hasSourceCode || !testApk)) {
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800275 copy {
Aurimas Liutikasef3b4922017-06-28 10:44:59 -0700276 from(task.outputDirectory)
277 include '*.apk'
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800278 into(rootProject.ext.testApkDistOut)
279 rename { String fileName ->
Hyundo Moon9938e172017-07-26 20:51:11 +0900280 // Exclude media-compat-test-* modules from existing support library
281 // presubmit tests.
282 if (fileName.contains("media-compat-test")) {
283 fileName.replace("-debug-androidTest", "")
Yigit Boyar96a0fcd2017-08-25 18:18:28 -0700284 } else {
285 // multiple modules may have the same name so prefix the name with
286 // the module's path to ensure it is unique.
287 // e.g. palette-v7-debug-androidTest.apk becomes
288 // support-palette-v7_palette-v7-debug-androidTest.apk
289 "${project.getPath().replace(':', '-').substring(1)}_${fileName}"
Hyundo Moon9938e172017-07-26 20:51:11 +0900290 }
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800291 }
292 }
293 }
294 }
295 }
296 }
297
298 // copy host side test results to DIST
299 project.tasks.whenTaskAdded { task ->
300 if (task instanceof org.gradle.api.tasks.testing.Test) {
301 def junitReport = task.reports.junitXml
302 if (junitReport.enabled) {
303 def zipTask = project.tasks.create(name : "zipResultsOf${task.name.capitalize()}", type : Zip) {
304 destinationDir(testResultsDistDir)
Yigit Boyar278676d2017-03-10 15:29:11 -0800305 // first one is always :, drop it.
306 archiveName("${project.getPath().split(":").join("_").substring(1)}.zip")
Yigit Boyar7bfacb72017-03-02 14:27:41 -0800307 }
308 if (project.rootProject.ext.runningInBuildServer) {
309 task.ignoreFailures = true
310 }
311 task.finalizedBy zipTask
312 task.doFirst {
313 zipTask.from(junitReport.destination)
314 }
315 }
316 }
317 }
318
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800319 project.afterEvaluate { p ->
320 // remove dependency on the test so that we still get coverage even if some tests fail
321 p.tasks.findAll { it instanceof JacocoReportTask }.each { task ->
322 def toBeRemoved = new ArrayList()
323 def dependencyList = task.taskDependencies.values
324 dependencyList.each { dep ->
325 if (dep instanceof String) {
326 def t = tasks.findByName(dep)
327 if (t instanceof DeviceProviderInstrumentTestTask) {
328 toBeRemoved.add(dep)
329 task.mustRunAfter(t)
330 }
331 }
332 }
333 toBeRemoved.each { dep ->
334 dependencyList.remove(dep)
335 }
336 }
337 }
338 }
339}
340
341def setupRelease() {
342 apply from: "${ext.supportRootFolder}/buildSrc/release.gradle"
343}
344
Yigit Boyar88c16ce2017-02-08 16:06:14 -0800345ext.init.enableDoclavaAndJDiff = this.&enableDoclavaAndJDiff
346ext.init.setSdkInLocalPropertiesFile = this.&setSdkInLocalPropertiesFile
347ext.init.setupRepoOutAndBuildNumber = this.&setupRepoOutAndBuildNumber
348ext.init.setupRelease = this.&setupRelease
Aurimas Liutikas38e8f4d2017-06-16 08:31:03 -0700349ext.init.configureSubProjects = this.&configureSubProjects
Yigit Boyare1bbf712017-03-08 13:52:37 -0800350ext.init.configureBuildOnServer = this.&configureBuildOnServer