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 compileSdkVersion = 28
19 targetSdkVersion = tryInt(mozconfig.substs.ANDROID_TARGET_SDK)
20 minSdkVersion = tryInt(mozconfig.substs.MOZ_ANDROID_MIN_SDK_VERSION)
21 manifestPlaceholders = [
22 ANDROID_PACKAGE_NAME: mozconfig.substs.ANDROID_PACKAGE_NAME,
23 ANDROID_TARGET_SDK: mozconfig.substs.ANDROID_TARGET_SDK,
24 MOZ_ANDROID_MIN_SDK_VERSION: mozconfig.substs.MOZ_ANDROID_MIN_SDK_VERSION,
25 MOZ_ANDROID_SHARED_ID: "${mozconfig.substs.ANDROID_PACKAGE_NAME}.sharedID",
30 gradle.mozconfig.substs.GRADLE_MAVEN_REPOSITORIES.each { repository ->
35 // For in tree dependencies.
36 // TODO: remove this once
37 // https://issuetracker.google.com/issues/120951637 is resolved. See
40 url "file://${gradle.mozconfig.topsrcdir}/mobile/android/gradle/m2repo"
44 task downloadDependencies() {
45 description 'Download all dependencies to the Gradle cache'
47 configurations.each { configuration ->
48 if (configuration.canBeResolved) {
49 configuration.allDependencies.each { dependency ->
51 configuration.files(dependency)
53 println("Could not resolve ${configuration.name} -> ${dependency.name}")
54 println(" > ${e.message}")
56 println(" >> ${e.cause}")
58 println(" >> ${e.cause.cause}")
70 buildDir "${topobjdir}/gradle/build"
74 gradle.mozconfig.substs.GRADLE_MAVEN_REPOSITORIES.each { repository ->
81 ext.kotlin_version = '1.2.41'
82 ext.support_library_version = '26.1.0'
83 ext.jacoco_version = '0.8.1'
85 if (gradle.mozconfig.substs.MOZ_ANDROID_GOOGLE_PLAY_SERVICES) {
86 ext.google_play_services_version = '15.0.1'
87 ext.google_play_services_cast_version = '16.0.0'
91 classpath 'org.mozilla.apilint:apilint:0.1.7'
92 classpath 'com.android.tools.build:gradle:3.1.4'
93 classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.8.2'
94 classpath 'org.apache.commons:commons-exec:1.3'
95 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
99 // A stream that processes bytes line by line, prepending a tag before sending
100 // each line to Gradle's logging.
101 class TaggedLogOutputStream extends org.apache.commons.exec.LogOutputStream {
105 TaggedLogOutputStream(tag, logger) {
110 void processLine(String line, int level) {
111 logger.lifecycle("${this.tag} ${line}")
115 ext.geckoBinariesOnlyIf = { task ->
116 // Never for official builds.
117 if (mozconfig.substs.MOZILLA_OFFICIAL) {
118 rootProject.logger.lifecycle("Skipping task ${task.path} because: MOZILLA_OFFICIAL")
122 // Multi-l10n builds set `AB_CD=multi`, which isn't a valid locale. This
125 // |mach build| > |mach gradle| >
126 // |mach build mobile/android/base/generated_android_code_and_resources| >
127 // AndroidManifest.xml > strings.xml > multi/brand.dtd
129 // dependency chain to fail, since multi isn't a real locale. To avoid
130 // this, if Gradle is invoked with AB_CD=multi, we don't invoke Make at all.
131 if ('multi' == System.env.AB_CD) {
132 rootProject.logger.lifecycle("Skipping task ${task.path} because: AB_CD=multi")
136 // Single-locale l10n repacks set `IS_LANGUAGE_REPACK=1` and handle resource
137 // and code generation themselves.
138 if ('1' == System.env.IS_LANGUAGE_REPACK) {
139 rootProject.logger.lifecycle("Skipping task ${task.path} because: IS_LANGUAGE_REPACK")
143 rootProject.logger.lifecycle("Executing task ${task.path}")
147 task machBuildGeneratedAndroidCodeAndResources(type: Exec) {
148 onlyIf rootProject.ext.geckoBinariesOnlyIf
150 workingDir "${topsrcdir}"
152 commandLine mozconfig.substs.PYTHON
153 args "${topsrcdir}/mach"
155 args 'mobile/android/base/generated_android_code_and_resources'
157 // Add `-v` if we're running under `--info` (or `--debug`).
158 if (project.logger.isEnabled(LogLevel.INFO)) {
162 // `path` is like `:machBuildGeneratedAndroidCodeAndResources`.
163 standardOutput = new TaggedLogOutputStream("${path}>", logger)
164 errorOutput = standardOutput
167 // Why |mach build mobile/android/base/...| and |mach build faster|? |mach
168 // build faster| generates dependentlibs.list, which in turn depends on compiled
169 // code. That causes a circular dependency between Java compilation/JNI wrapper
170 // generation/native code compilation. So we have the special target for
171 // Android-specific generated code, and the |mach build faster| target for all
172 // the stuff that goes into the omnijar.
173 task machBuildFaster(type: Exec) {
174 onlyIf rootProject.ext.geckoBinariesOnlyIf
176 workingDir "${topsrcdir}"
178 commandLine mozconfig.substs.PYTHON
179 args "${topsrcdir}/mach"
183 // Add `-v` if we're running under `--info` (or `--debug`).
184 if (project.logger.isEnabled(LogLevel.INFO)) {
188 // `path` is like `:machBuildFaster`.
189 standardOutput = new TaggedLogOutputStream("${path}>", logger)
190 errorOutput = standardOutput
193 def createMachStagePackageTask(name) {
194 return task(name, type: Exec) {
195 onlyIf rootProject.ext.geckoBinariesOnlyIf
197 dependsOn rootProject.machBuildFaster
199 // We'd prefer to take these from the :omnijar project directly, but
200 // it's awkward to reach across projects at evaluation time, so we
201 // duplicate the list here.
202 inputs.dir "${topsrcdir}/mobile/android/chrome"
203 inputs.dir "${topsrcdir}/mobile/android/components"
204 inputs.dir "${topsrcdir}/mobile/android/locales"
205 inputs.dir "${topsrcdir}/mobile/android/modules"
206 inputs.dir "${topsrcdir}/mobile/android/themes"
207 inputs.dir "${topsrcdir}/toolkit"
209 workingDir "${topobjdir}"
211 // We'd prefer this to be a `mach` invocation, but `mach build
212 // mobile/android/installer/stage-package` doesn't work as expected.
213 commandLine mozconfig.substs.GMAKE
215 args "${topobjdir}/mobile/android/installer"
218 outputs.file "${topobjdir}/dist/fennec/assets/${mozconfig.substs.ANDROID_CPU_ARCH}/libxul.so"
219 outputs.file "${topobjdir}/dist/fennec/lib/${mozconfig.substs.ANDROID_CPU_ARCH}/libmozglue.so"
221 // `path` is like `:machStagePackage`.
222 standardOutput = new TaggedLogOutputStream("${path}>", logger)
223 errorOutput = standardOutput
227 createMachStagePackageTask("machStagePackageForFennec").with {
228 outputs.file "${topobjdir}/dist/fennec/assets/omni.ja"
231 createMachStagePackageTask("machStagePackageForGeckoview").with {
232 args 'MOZ_GECKOVIEW_JAR=1'
233 outputs.file "${topobjdir}/dist/geckoview/assets/omni.ja"
234 // Avoid races between stage-package invocations.
235 mustRunAfter tasks["machStagePackageForFennec"]
239 subprojects { project ->
240 if (project.name != 'thirdparty') {
241 tasks.withType(JavaCompile) {
242 // Add compiler args for all code except third-party code.
243 options.compilerArgs += [
244 // Turn on all warnings, except...
246 // Deprecation, because we do use deprecated API for compatibility.
247 "-Xlint:-deprecation",
248 // Serial, because we don't use Java serialization.
250 // Classfile, because javac has a bug with MethodParameters attributes
251 // with Java 7. https://bugs.openjdk.java.net/browse/JDK-8190452
253 // Turn all remaining warnings into errors,
254 // unless marked by @SuppressWarnings.
257 if (project.name == 'app') {
258 tasks.withType(JavaCompile) {
259 // Turn off classfile warnings because upon updating to play services 15.0.0
260 // a warning is being thrown from play-services-base which fails the build
261 // (com/google/android/gms/common/api/GoogleApiClient.class):
262 // warning: Cannot find annotation method 'value()' in type 'GuardedBy':
263 // class file for javax.annotation.concurrent.GuardedBy not found
264 options.compilerArgs += ["-Xlint:-classfile"]
269 if (!hasProperty('android')) {
272 android.applicationVariants.all {
273 preBuild.dependsOn rootProject.machBuildGeneratedAndroidCodeAndResources
275 android.libraryVariants.all {
276 preBuild.dependsOn rootProject.machBuildGeneratedAndroidCodeAndResources
285 languageLevel = '1.8'
289 // Object directories take a huge amount of time for IntelliJ to index.
290 // Exclude them. Convention is that object directories start with obj.
291 // IntelliJ is clever and will not exclude the parts of the object
292 // directory that are referenced, if there are any. In practice,
293 // indexing the entirety of the tree is taking too long, so exclude all
295 def topsrcdirURI = file(topsrcdir).toURI()
296 excludeDirs += files(file(topsrcdir)
297 .listFiles({it.isDirectory()} as FileFilter)
298 .collect({topsrcdirURI.relativize(it.toURI()).toString()}) // Relative paths.
299 .findAll({!it.equals('mobile/')}))
301 // If topobjdir is below topsrcdir, hide only some portions of that tree.
302 def topobjdirURI = file(topobjdir).toURI()
303 if (!topsrcdirURI.relativize(topobjdirURI).isAbsolute()) {
304 excludeDirs -= file(topobjdir)
305 excludeDirs += files(file(topobjdir).listFiles())
306 excludeDirs -= file("${topobjdir}/gradle")
309 if (!mozconfig.substs.MOZ_INSTALL_TRACKING) {
310 excludeDirs += file("${topsrcdir}/mobile/android/thirdparty/com/adjust")
315 task wrapper(type: Wrapper) {