1 def tryInt = { string ->
5 if (string.isInteger()) {
6 return string as Integer
12 // Expose the per-object-directory configuration to all projects.
14 mozconfig = gradle.mozconfig
15 topsrcdir = gradle.mozconfig.topsrcdir
16 topobjdir = gradle.mozconfig.topobjdir
18 buildToolsVersion = mozconfig.substs.ANDROID_BUILD_TOOLS_VERSION
19 compileSdkVersion = tryInt(mozconfig.substs.ANDROID_TARGET_SDK)
20 targetSdkVersion = tryInt(mozconfig.substs.ANDROID_TARGET_SDK)
21 minSdkVersion = tryInt(mozconfig.substs.MOZ_ANDROID_MIN_SDK_VERSION)
22 manifestPlaceholders = [
23 ANDROID_PACKAGE_NAME: mozconfig.substs.ANDROID_PACKAGE_NAME,
24 ANDROID_TARGET_SDK: mozconfig.substs.ANDROID_TARGET_SDK,
25 MOZ_ANDROID_MIN_SDK_VERSION: mozconfig.substs.MOZ_ANDROID_MIN_SDK_VERSION,
30 gradle.mozconfig.substs.GRADLE_MAVEN_REPOSITORIES.each { repository ->
37 task downloadDependencies() {
38 description 'Download all dependencies to the Gradle cache'
40 configurations.each { configuration ->
41 if (configuration.canBeResolved) {
42 configuration.allDependencies.each { dependency ->
44 configuration.files(dependency)
46 println("Could not resolve ${configuration.name} -> ${dependency.name}")
47 println(" > ${e.message}")
49 println(" >> ${e.cause}")
51 println(" >> ${e.cause.cause}")
63 buildDir "${topobjdir}/gradle/build"
67 gradle.mozconfig.substs.GRADLE_MAVEN_REPOSITORIES.each { repository ->
74 ext.kotlin_version = '1.3.41'
77 classpath 'org.mozilla.apilint:apilint:0.3.1'
78 classpath 'com.android.tools.build:gradle:3.4.2'
79 classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.8.2'
80 classpath 'org.apache.commons:commons-exec:1.3'
81 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
85 // A stream that processes bytes line by line, prepending a tag before sending
86 // each line to Gradle's logging.
87 class TaggedLogOutputStream extends org.apache.commons.exec.LogOutputStream {
91 TaggedLogOutputStream(tag, logger) {
96 void processLine(String line, int level) {
97 logger.lifecycle("${this.tag} ${line}")
101 ext.geckoBinariesOnlyIf = { task ->
102 // Never when Gradle was invoked within `mach build`.
103 if ('1' == System.env.GRADLE_INVOKED_WITHIN_MACH_BUILD) {
104 rootProject.logger.lifecycle("Skipping task ${task.path} because: within `mach build`")
108 // Never for official builds.
109 if (mozconfig.substs.MOZILLA_OFFICIAL) {
110 rootProject.logger.lifecycle("Skipping task ${task.path} because: MOZILLA_OFFICIAL")
114 // Multi-l10n builds set `AB_CD=multi`, which isn't a valid locale. To
115 // avoid failures, if Gradle is invoked with AB_CD=multi, we don't invoke
117 if ('multi' == System.env.AB_CD) {
118 rootProject.logger.lifecycle("Skipping task ${task.path} because: AB_CD=multi")
122 // Single-locale l10n repacks set `IS_LANGUAGE_REPACK=1` and handle resource
123 // and code generation themselves.
124 if ('1' == System.env.IS_LANGUAGE_REPACK) {
125 rootProject.logger.lifecycle("Skipping task ${task.path} because: IS_LANGUAGE_REPACK")
129 rootProject.logger.lifecycle("Executing task ${task.path}")
133 class MachExec extends Exec {
135 // Bug 1543982: When invoking `mach build` recursively, the outer `mach
136 // build` itself modifies the environment, causing configure to run
137 // again. This tries to restore the environment that the outer `mach
138 // build` was invoked in. See the comment in
139 // $topsrcdir/settings.gradle.
140 project.ext.mozconfig.mozconfig.env.unmodified.each { k, v -> environment.remove(k) }
141 environment project.ext.mozconfig.orig_mozconfig.env.unmodified
145 task machBuildFaster(type: MachExec) {
146 onlyIf rootProject.ext.geckoBinariesOnlyIf
148 workingDir "${topsrcdir}"
150 commandLine mozconfig.substs.PYTHON3
151 args "${topsrcdir}/mach"
155 // Add `-v` if we're running under `--info` (or `--debug`).
156 if (project.logger.isEnabled(LogLevel.INFO)) {
160 // `path` is like `:machBuildFaster`.
161 standardOutput = new TaggedLogOutputStream("${path}>", logger)
162 errorOutput = standardOutput
165 task machStagePackage(type: MachExec) {
166 onlyIf rootProject.ext.geckoBinariesOnlyIf
168 dependsOn rootProject.machBuildFaster
170 workingDir "${topobjdir}"
172 // We'd prefer this to be a `mach` invocation, but `mach build
173 // mobile/android/installer/stage-package` doesn't work as expected.
174 commandLine mozconfig.substs.GMAKE
176 args "${topobjdir}/mobile/android/installer"
179 outputs.file "${topobjdir}/dist/geckoview/assets/omni.ja"
181 outputs.file "${topobjdir}/dist/geckoview/assets/${mozconfig.substs.ANDROID_CPU_ARCH}/libxul.so"
182 outputs.file "${topobjdir}/dist/geckoview/lib/${mozconfig.substs.ANDROID_CPU_ARCH}/libmozglue.so"
184 // Force running `stage-package`.
185 outputs.upToDateWhen { false }
187 // `path` is like `:machStagePackage`.
188 standardOutput = new TaggedLogOutputStream("${path}>", logger)
189 errorOutput = standardOutput
193 subprojects { project ->
194 tasks.withType(JavaCompile) {
195 // Add compiler args for all code except third-party code.
196 options.compilerArgs += [
197 // Turn on all warnings, except...
199 // Deprecation, because we do use deprecated API for compatibility.
200 "-Xlint:-deprecation",
201 // Serial, because we don't use Java serialization.
203 // Classfile, because javac has a bug with MethodParameters attributes
204 // with Java 7. https://bugs.openjdk.java.net/browse/JDK-8190452
206 // Turn all remaining warnings into errors,
207 // unless marked by @SuppressWarnings.
217 languageLevel = '1.8'
221 // Object directories take a huge amount of time for IntelliJ to index.
222 // Exclude them. Convention is that object directories start with obj.
223 // IntelliJ is clever and will not exclude the parts of the object
224 // directory that are referenced, if there are any. In practice,
225 // indexing the entirety of the tree is taking too long, so exclude all
227 def topsrcdirURI = file(topsrcdir).toURI()
228 excludeDirs += files(file(topsrcdir)
229 .listFiles({it.isDirectory()} as FileFilter)
230 .collect({topsrcdirURI.relativize(it.toURI()).toString()}) // Relative paths.
231 .findAll({!it.equals('mobile/')}))
233 // If topobjdir is below topsrcdir, hide only some portions of that tree.
234 def topobjdirURI = file(topobjdir).toURI()
235 if (!topsrcdirURI.relativize(topobjdirURI).isAbsolute()) {
236 excludeDirs -= file(topobjdir)
237 excludeDirs += files(file(topobjdir).listFiles())
238 excludeDirs -= file("${topobjdir}/gradle")