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 = 26
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'
90 classpath 'org.mozilla.apilint:apilint:0.1.5'
91 classpath 'com.android.tools.build:gradle:3.1.4'
92 classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.8.2'
93 classpath 'org.apache.commons:commons-exec:1.3'
94 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
98 // A stream that processes bytes line by line, prepending a tag before sending
99 // each line to Gradle's logging.
100 class TaggedLogOutputStream extends org.apache.commons.exec.LogOutputStream {
104 TaggedLogOutputStream(tag, logger) {
109 void processLine(String line, int level) {
110 logger.lifecycle("${this.tag} ${line}")
114 ext.geckoBinariesOnlyIf = { task ->
115 // Never for official builds.
116 if (mozconfig.substs.MOZILLA_OFFICIAL) {
117 rootProject.logger.lifecycle("Skipping task ${task.path} because: MOZILLA_OFFICIAL")
121 // Multi-l10n builds set `AB_CD=multi`, which isn't a valid locale. This
124 // |mach build| > |mach gradle| >
125 // |mach build mobile/android/base/generated_android_code_and_resources| >
126 // AndroidManifest.xml > strings.xml > multi/brand.dtd
128 // dependency chain to fail, since multi isn't a real locale. To avoid
129 // this, if Gradle is invoked with AB_CD=multi, we don't invoke Make at all.
130 if ('multi' == System.env.AB_CD) {
131 rootProject.logger.lifecycle("Skipping task ${task.path} because: AB_CD=multi")
135 // Single-locale l10n repacks set `IS_LANGUAGE_REPACK=1` and handle resource
136 // and code generation themselves.
137 if ('1' == System.env.IS_LANGUAGE_REPACK) {
138 rootProject.logger.lifecycle("Skipping task ${task.path} because: IS_LANGUAGE_REPACK")
142 rootProject.logger.lifecycle("Executing task ${task.path}")
146 task machBuildGeneratedAndroidCodeAndResources(type: Exec) {
147 onlyIf rootProject.ext.geckoBinariesOnlyIf
149 workingDir "${topsrcdir}"
151 commandLine mozconfig.substs.PYTHON
152 args "${topsrcdir}/mach"
154 args 'mobile/android/base/generated_android_code_and_resources'
156 // Add `-v` if we're running under `--info` (or `--debug`).
157 if (project.logger.isEnabled(LogLevel.INFO)) {
161 // `path` is like `:machBuildGeneratedAndroidCodeAndResources`.
162 standardOutput = new TaggedLogOutputStream("${path}>", logger)
163 errorOutput = standardOutput
166 // Why |mach build mobile/android/base/...| and |mach build faster|? |mach
167 // build faster| generates dependentlibs.list, which in turn depends on compiled
168 // code. That causes a circular dependency between Java compilation/JNI wrapper
169 // generation/native code compilation. So we have the special target for
170 // Android-specific generated code, and the |mach build faster| target for all
171 // the stuff that goes into the omnijar.
172 task machBuildFaster(type: Exec) {
173 onlyIf rootProject.ext.geckoBinariesOnlyIf
175 workingDir "${topsrcdir}"
177 commandLine mozconfig.substs.PYTHON
178 args "${topsrcdir}/mach"
182 // Add `-v` if we're running under `--info` (or `--debug`).
183 if (project.logger.isEnabled(LogLevel.INFO)) {
187 // `path` is like `:machBuildFaster`.
188 standardOutput = new TaggedLogOutputStream("${path}>", logger)
189 errorOutput = standardOutput
192 def createMachStagePackageTask(name) {
193 return task(name, type: Exec) {
194 onlyIf rootProject.ext.geckoBinariesOnlyIf
196 dependsOn rootProject.machBuildFaster
198 // We'd prefer to take these from the :omnijar project directly, but
199 // it's awkward to reach across projects at evaluation time, so we
200 // duplicate the list here.
201 inputs.dir "${topsrcdir}/mobile/android/chrome"
202 inputs.dir "${topsrcdir}/mobile/android/components"
203 inputs.dir "${topsrcdir}/mobile/android/locales"
204 inputs.dir "${topsrcdir}/mobile/android/modules"
205 inputs.dir "${topsrcdir}/mobile/android/themes"
206 inputs.dir "${topsrcdir}/toolkit"
208 workingDir "${topobjdir}"
210 // We'd prefer this to be a `mach` invocation, but `mach build
211 // mobile/android/installer/stage-package` doesn't work as expected.
212 commandLine mozconfig.substs.GMAKE
214 args "${topobjdir}/mobile/android/installer"
217 outputs.file "${topobjdir}/dist/fennec/assets/${mozconfig.substs.ANDROID_CPU_ARCH}/libxul.so"
218 outputs.file "${topobjdir}/dist/fennec/lib/${mozconfig.substs.ANDROID_CPU_ARCH}/libmozglue.so"
220 // `path` is like `:machStagePackage`.
221 standardOutput = new TaggedLogOutputStream("${path}>", logger)
222 errorOutput = standardOutput
226 createMachStagePackageTask("machStagePackageForFennec").with {
227 outputs.file "${topobjdir}/dist/fennec/assets/omni.ja"
230 createMachStagePackageTask("machStagePackageForGeckoview").with {
231 args 'MOZ_GECKOVIEW_JAR=1'
232 outputs.file "${topobjdir}/dist/geckoview/assets/omni.ja"
233 // Avoid races between stage-package invocations.
234 mustRunAfter tasks["machStagePackageForFennec"]
238 subprojects { project ->
239 if (project.name != 'thirdparty') {
240 tasks.withType(JavaCompile) {
241 // Add compiler args for all code except third-party code.
242 options.compilerArgs += [
243 // Turn on all warnings, except...
245 // Deprecation, because we do use deprecated API for compatibility.
246 "-Xlint:-deprecation",
247 // Serial, because we don't use Java serialization.
249 // Classfile, because javac has a bug with MethodParameters attributes
250 // with Java 7. https://bugs.openjdk.java.net/browse/JDK-8190452
252 // Turn all remaining warnings into errors,
253 // unless marked by @SuppressWarnings.
256 if (project.name == 'app') {
257 tasks.withType(JavaCompile) {
258 // Turn off classfile warnings because upon updating to play services 15.0.0
259 // a warning is being thrown from play-services-base which fails the build
260 // (com/google/android/gms/common/api/GoogleApiClient.class):
261 // warning: Cannot find annotation method 'value()' in type 'GuardedBy':
262 // class file for javax.annotation.concurrent.GuardedBy not found
263 options.compilerArgs += ["-Xlint:-classfile"]
268 if (!hasProperty('android')) {
271 android.applicationVariants.all {
272 preBuild.dependsOn rootProject.machBuildGeneratedAndroidCodeAndResources
274 android.libraryVariants.all {
275 preBuild.dependsOn rootProject.machBuildGeneratedAndroidCodeAndResources
284 languageLevel = '1.8'
288 // Object directories take a huge amount of time for IntelliJ to index.
289 // Exclude them. Convention is that object directories start with obj.
290 // IntelliJ is clever and will not exclude the parts of the object
291 // directory that are referenced, if there are any. In practice,
292 // indexing the entirety of the tree is taking too long, so exclude all
294 def topsrcdirURI = file(topsrcdir).toURI()
295 excludeDirs += files(file(topsrcdir)
296 .listFiles({it.isDirectory()} as FileFilter)
297 .collect({topsrcdirURI.relativize(it.toURI()).toString()}) // Relative paths.
298 .findAll({!it.equals('mobile/')}))
300 // If topobjdir is below topsrcdir, hide only some portions of that tree.
301 def topobjdirURI = file(topobjdir).toURI()
302 if (!topsrcdirURI.relativize(topobjdirURI).isAbsolute()) {
303 excludeDirs -= file(topobjdir)
304 excludeDirs += files(file(topobjdir).listFiles())
305 excludeDirs -= file("${topobjdir}/gradle")
308 if (!mozconfig.substs.MOZ_INSTALL_TRACKING) {
309 excludeDirs += file("${topsrcdir}/mobile/android/thirdparty/com/adjust")
314 task wrapper(type: Wrapper) {