blob: ff93bdc9aa8cd0a621f6e120f5408db616dd3ae1 [file] [log] [blame]
Yigit Boyarea5d9b22016-03-08 13:25:26 -08001import com.android.build.gradle.internal.coverage.JacocoReportTask
2import com.android.build.gradle.internal.tasks.DeviceProviderInstrumentTestTask
3
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -08004buildscript {
5 repositories {
6 maven { url '../../prebuilts/gradle-plugin' }
7 maven { url '../../prebuilts/tools/common/m2/repository' }
8 maven { url '../../prebuilts/tools/common/m2/internal' }
Yigit Boyarc9750a12016-01-06 17:28:55 -08009 maven { url "../../prebuilts/maven_repo/android" }
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -080010 }
11 dependencies {
Xavier Ducrohet0df81c72016-05-11 16:35:45 -070012 classpath 'com.android.tools.build:gradle:2.2.0-alpha1'
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -080013 }
14}
15
Chris Flatta48e18c2016-05-20 18:12:26 -070016ext.supportVersion = '24.0.0'
17ext.extraVersion = 33
Xavier Ducrohet020e4322014-03-18 16:41:30 -070018ext.supportRepoOut = ''
Yigit Boyarc9750a12016-01-06 17:28:55 -080019ext.buildToolsVersion = '23.0.2'
Xavier Ducrohetfa385272014-11-14 13:12:09 -080020ext.buildNumber = Integer.toString(ext.extraVersion)
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -080021
Xavier Ducrohet020e4322014-03-18 16:41:30 -070022/*
23 * With the build server you are given two env variables.
24 * The OUT_DIR is a temporary directory you can use to put things during the build.
25 * The DIST_DIR is where you want to save things from the build.
26 *
27 * The build server will copy the contents of DIST_DIR to somewhere and make it available.
28 */
29if (System.env.DIST_DIR != null && System.env.OUT_DIR != null) {
Xavier Ducrohet4e04b7a2014-10-17 18:02:33 -070030 buildDir = new File(System.env.OUT_DIR + '/gradle/frameworks/support/build').getCanonicalFile()
31 project.ext.distDir = new File(System.env.DIST_DIR).getCanonicalFile()
Xavier Ducrohetfa385272014-11-14 13:12:09 -080032
33 // the build server does not pass the build number so we infer it from the last folder of the dist path.
34 ext.buildNumber = project.ext.distDir.getName()
Xavier Ducrohet020e4322014-03-18 16:41:30 -070035} else {
36 buildDir = file('../../out/host/gradle/frameworks/support/build')
37 project.ext.distDir = file('../../out/dist')
38}
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -080039
Xavier Ducrohet020e4322014-03-18 16:41:30 -070040ext.supportRepoOut = new File(buildDir, 'support_repo')
Yigit Boyarf18d9752015-12-01 13:45:28 -080041ext.testApkDistOut = ext.distDir
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -080042
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -070043// Main task called by the build server.
44task(createArchive) << {
Xavier Ducrohet020e4322014-03-18 16:41:30 -070045}
46
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -070047
48// upload anchor for subprojects to upload their artifacts
49// to the local repo.
50task(mainUpload) << {
51}
52
53// repository creation task
54task createRepository(type: Zip, dependsOn: mainUpload) {
Xavier Ducrohet020e4322014-03-18 16:41:30 -070055 from project.ext.supportRepoOut
56 destinationDir project.ext.distDir
Xavier Ducrohet9dc44802014-03-20 14:15:16 -070057 into 'm2repository'
Xavier Ducrohetfa385272014-11-14 13:12:09 -080058 baseName = String.format("sdk-repo-linux-m2repository-%s", project.ext.buildNumber)
Xavier Ducrohet020e4322014-03-18 16:41:30 -070059}
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -070060createArchive.dependsOn createRepository
Xavier Ducrohet020e4322014-03-18 16:41:30 -070061
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -070062// prepare repository with older versions
Xavier Ducrohet64fe2322014-06-16 17:59:34 -070063task unzipRepo(type: Copy) {
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -080064 from "$rootDir/../../prebuilts/maven_repo/android"
Xavier Ducrohet855a9222014-01-02 19:00:43 -080065 into project.ext.supportRepoOut
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -080066}
67
Xavier Ducrohet64fe2322014-06-16 17:59:34 -070068unzipRepo.doFirst {
Xavier Ducrohet020e4322014-03-18 16:41:30 -070069 project.ext.supportRepoOut.deleteDir()
70 project.ext.supportRepoOut.mkdirs()
71}
72
Xavier Ducrohet64fe2322014-06-16 17:59:34 -070073// anchor for prepare repo. This is post unzip + sourceProp.
74task(prepareRepo) << {
75}
76
Yigit Boyar3986e042016-02-08 18:31:38 -080077
78import android.support.build.ApiModule
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -070079import com.google.common.io.Files
80import com.google.common.base.Charsets
81
82task(createXml) << {
83 def repoArchive = createRepository.archivePath
84 def repoArchiveName = createRepository.archiveName
85 def size = repoArchive.length()
86 def sha1 = getSha1(repoArchive)
87
88 def xml =
89"<sdk:sdk-addon xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:sdk=\"http://schemas.android.com/sdk/android/addon/6\">\n\
90 <sdk:extra>\n\
91 <sdk:revision>\n\
92 <sdk:major>${project.ext.extraVersion}</sdk:major>\n\
93 </sdk:revision>\n\
94 <sdk:vendor-display>Android</sdk:vendor-display>\n\
95 <sdk:vendor-id>android</sdk:vendor-id>\n\
96 <sdk:name-display>Local Maven repository for Support Libraries</sdk:name-display>\n\
97 <sdk:path>m2repository</sdk:path>\n\
98 <sdk:archives>\n\
Xavier Ducrohetc16b62d2014-12-09 12:37:45 -080099 <sdk:archive>\n\
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -0700100 <sdk:size>${size}</sdk:size>\n\
101 <sdk:checksum type=\"sha1\">${sha1}</sdk:checksum>\n\
102 <sdk:url>${repoArchiveName}</sdk:url>\n\
103 </sdk:archive>\n\
104 </sdk:archives>\n\
105 </sdk:extra>\n\
106</sdk:sdk-addon>"
107
108 Files.write(xml, new File(project.ext.distDir, 'repo-extras.xml'), Charsets.UTF_8)
109}
110createArchive.dependsOn createXml
111
Xavier Ducrohet64fe2322014-06-16 17:59:34 -0700112task(createSourceProp) << {
113 def sourceProp =
114"Extra.VendorDisplay=Android\n\
115Extra.Path=m2repository\n\
116Archive.Arch=ANY\n\
117Extra.NameDisplay=Android Support Repository\n\
118Archive.Os=ANY\n\
Alan Viverette5ae24d62016-04-06 16:17:13 -0400119Pkg.Desc=Local Maven repository for Support Libraries\n\
Xavier Ducrohet64fe2322014-06-16 17:59:34 -0700120Pkg.Revision=${project.ext.extraVersion}.0.0\n\
121Extra.VendorId=android"
122
123 Files.write(sourceProp, new File(project.ext.supportRepoOut, 'source.properties'), Charsets.UTF_8)
124}
125createSourceProp.dependsOn unzipRepo
126prepareRepo.dependsOn createSourceProp
127
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -0700128import com.google.common.hash.HashCode
129import com.google.common.hash.HashFunction
130import com.google.common.hash.Hashing
Chris Banes96f1e912015-03-05 20:04:05 +0000131import java.nio.charset.Charset
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -0700132
133def getSha1(File inputFile) {
134 HashFunction hashFunction = Hashing.sha1()
Chris Banes96f1e912015-03-05 20:04:05 +0000135 HashCode hashCode = hashFunction.hashString(inputFile.getAbsolutePath(), Charset.forName("UTF-8"))
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -0700136 return hashCode.toString()
137}
138
Yigit Boyar3986e042016-02-08 18:31:38 -0800139def createApiSourceSets(Project subProject, List<ApiModule> apiModules) {
140 subProject.ext._apiModules = apiModules
141 subProject.ext.allSS = []
142 if (gradle.ext.studioCompat.enableApiModules) {
143 // nothing to do, they are all modules
144 return
145 }
146 // create a jar task for the api specific internal implementations
147 def internalJar = subProject.tasks.create(name: "internalJar", type: Jar) {
148 baseName "internal_impl"
149 }
150 apiModules.each { ApiModule apiModule ->
151 apiModule.sourceSet = createApiSourceset(subProject, apiModule.folderName, apiModule.folderName,
152 apiModule.apiForSourceSet.toString(), apiModule.prev == null ? null : apiModule.prev.sourceSet)
153 subProject.ext.allSS.add(apiModule.sourceSet)
154 }
155 subProject.android.libraryVariants.all { variant ->
156 variant.javaCompile.dependsOn internalJar
157 }
158}
159
160def createApiSourceset(Project subProject, String name, String folder, String apiLevel,
161 SourceSet previousSource) {
162 def sourceSet = subProject.sourceSets.create(name)
163 sourceSet.java.srcDirs = [folder]
164
165 def configName = sourceSet.getCompileConfigurationName()
166
167 subProject.getDependencies().add(configName, getAndroidPrebuilt(apiLevel))
168 if (previousSource != null) {
169 setupDependencies(subProject, configName, previousSource)
170 }
171 subProject.ext.allSS.add(sourceSet)
172 subProject.tasks.internalJar.from sourceSet.output
173 return sourceSet
174}
175
176def setApiModuleDependencies(Project subProject, DependencyHandler handler, List extraDeps) {
177 if (gradle.ext.studioCompat.enableApiModules) {
178 subProject.android.enforceUniquePackageName=false
179 // add dependency on the latest module
180 handler.compile(project(subProject.ext._apiModules.last().moduleName))
181 } else {
182 handler.compile(files(subProject.tasks.internalJar.archivePath))
183 def firstModule = subProject.ext._apiModules[0]
184 extraDeps.each { dep ->
185 handler."${firstModule.folderName}Compile"(project(dep))
186 handler.compile(project(dep))
187 }
188
189 }
190}
191
192def setupDependencies(Project subProject, String configName, SourceSet previousSourceSet) {
193 subProject.getDependencies().add(configName, previousSourceSet.output)
194 subProject.getDependencies().add(configName, previousSourceSet.compileClasspath)
195}
196
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -0800197subprojects {
198 // Change buildDir first so that all plugins pick up the new value.
Xavier Ducrohet616b95d2014-02-12 09:09:43 -0800199 project.buildDir = project.file("$project.parent.buildDir/../$project.name/build")
Yigit Boyar3986e042016-02-08 18:31:38 -0800200 // current SDK is set in studioCompat.gradle
201 project.ext.currentSdk = gradle.ext.currentSdk
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -0800202 apply plugin: 'maven'
Yigit Boyar3986e042016-02-08 18:31:38 -0800203 project.ext.createApiSourceSets = this.&createApiSourceset
204 project.ext.setApiModuleDependencies = this.&setApiModuleDependencies
205
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -0800206
Xavier Ducrohet855a9222014-01-02 19:00:43 -0800207 version = rootProject.ext.supportVersion
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -0800208 group = 'com.android.support'
209
Yigit Boyarbe7a54a2015-04-07 13:23:50 -0700210 repositories {
211 maven { url "${project.parent.projectDir}/../../prebuilts/tools/common/m2/repository" }
212 maven { url "${project.parent.projectDir}/../../prebuilts/tools/common/m2/internal" }
213 maven { url "${project.parent.projectDir}/../../prebuilts/maven_repo/android" }
214 }
215
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -0800216 task release(type: Upload) {
217 configuration = configurations.archives
218 repositories {
219 mavenDeployer {
Xavier Ducrohet855a9222014-01-02 19:00:43 -0800220 repository(url: uri("$rootProject.ext.supportRepoOut"))
Justin Klaassen533239e2016-01-09 09:16:23 -0800221
222 // Disable unique names for SNAPSHOTS so they can be updated in place.
223 setUniqueVersion(false)
Justin Klaassend99d7742016-01-21 15:33:57 -0800224 doLast {
225 // Remove any invalid maven-metadata.xml files that may have been created
226 // for SNAPSHOT versions that are *not* uniquely versioned.
227 pom*.each { pom ->
228 if (pom.version.endsWith('-SNAPSHOT')) {
229 final File artifactDir = new File(rootProject.ext.supportRepoOut,
230 pom.groupId.replace('.', '/')
231 + '/' + pom.artifactId
232 + '/' + pom.version)
233 delete fileTree(dir: artifactDir, include: 'maven-metadata.xml*')
234 }
235 }
236 }
Justin Klaassen533239e2016-01-09 09:16:23 -0800237 }
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -0800238 }
239 }
Xavier Ducrohet020e4322014-03-18 16:41:30 -0700240
Xavier Ducrohete4cf5a92015-03-06 17:17:30 -0800241 def deployer = release.repositories.mavenDeployer
242 deployer.pom*.whenConfigured { pom ->
243 pom.dependencies.findAll {dep -> dep.groupId == 'com.android.support' && dep.artifactId != 'support-annotations' }*.type = 'aar'
244 }
245
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -0700246 // before the upload, make sure the repo is ready.
Xavier Ducrohet020e4322014-03-18 16:41:30 -0700247 release.dependsOn rootProject.tasks.prepareRepo
Xavier Ducrohet9220b5b2014-03-21 15:30:01 -0700248 // make the mainupload depend on this one.
Xavier Ducrohet020e4322014-03-18 16:41:30 -0700249 mainUpload.dependsOn release
Jeff Davidson84faec52014-06-18 09:10:36 -0700250
251 project.plugins.whenPluginAdded { plugin ->
252 if ("com.android.build.gradle.LibraryPlugin".equals(plugin.class.name)) {
253 project.android.buildToolsVersion = rootProject.buildToolsVersion
Yigit Boyard8d42d52016-04-12 18:20:18 -0700254 // enable code coverage for debug builds only if we are not running inside the IDE
255 // enabling coverage reports breaks the method parameter resolution in the IDE debugger
256 project.android.buildTypes.debug.testCoverageEnabled = !hasProperty('android.injected.invoked.from.ide')
Jeff Davidson84faec52014-06-18 09:10:36 -0700257 }
258 }
Chris Banesdaea0692015-12-29 12:48:24 +0000259
260 // Copy instrumentation test APK into the dist dir
261 project.afterEvaluate {
262 def assembleTestTask = project.tasks.findByPath('assembleAndroidTest')
263 if (assembleTestTask != null) {
264 assembleTestTask.doLast {
265 // If the project actually has some instrumentation tests, copy its APK
266 if (!project.android.sourceSets.androidTest.java.sourceFiles.isEmpty()) {
267 def pkgTask = project.tasks.findByPath('packageDebugAndroidTest')
268 copy {
269 from(pkgTask.outputFile)
270 into(rootProject.ext.testApkDistOut)
271 }
272 }
273 }
274 }
275 }
Yigit Boyarea5d9b22016-03-08 13:25:26 -0800276
277 project.afterEvaluate { p ->
278 // remove dependency on the test so that we still get coverage even if some tests fail
279 p.tasks.findAll { it instanceof JacocoReportTask}.each { task ->
280 def toBeRemoved = new ArrayList()
281 def dependencyList = task.taskDependencies.values
282 dependencyList.each { dep ->
283 if (dep instanceof String) {
284 def t = tasks.findByName(dep)
285 if (t instanceof DeviceProviderInstrumentTestTask) {
286 toBeRemoved.add(dep)
287 task.mustRunAfter(t)
288 }
289 }
290 }
291 toBeRemoved.each { dep ->
292 dependencyList.remove(dep)
293 }
294 }
295 }
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -0800296}
297
Chris Banes9ae4ee82015-09-11 10:32:15 +1000298project.gradle.buildFinished { buildResult ->
299 if (buildResult.getFailure() != null) {
300 println()
301 println 'Build failed. Possible causes include:'
302 println ' 1) Bad codes'
303 println ' 2) Out of date prebuilts in prebuilts/sdk'
Chris Banes9e2e8032015-09-16 10:15:37 +0100304 println ' 3) Need to update the compileSdkVersion in a library\'s build.gradle'
Chris Banes9ae4ee82015-09-11 10:32:15 +1000305 println()
306 }
307}
308
Xavier Ducrohet86fb8ef2013-02-22 15:04:37 -0800309FileCollection getAndroidPrebuilt(String apiLevel) {
310 files("$rootDir/../../prebuilts/sdk/$apiLevel/android.jar")
Tor Norbye47c59fb2015-04-08 14:42:16 -0700311}