1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 // Substitute a local GeckoView AAR into a consuming Gradle project.
7 // To use this, in a consuming Gradle project's `/build.gradle` add a stanza like:
9 // ext.topsrcdir = '/absolute/path/to/mozilla-central'; apply from: "${ext.topsrcdir}/substitute-local-geckoview.gradle"
11 // The object directory will be determined using `mach environment` and will agree with `./mach
12 // gradle` and Android Studio. Or, specify the exact object directory with a stanza like:
14 // ext.topsrcdir = '/absolute/path/to/mozilla-central'
15 // ext.topobjdir = '/absolute/path/to/objdir'
16 // apply from: "${ext.topsrcdir}/substitute-local-geckoview.gradle"
18 // Substitution works with artifact and non-artifact builds.
20 // If you get errors about .jar files not being found, ensure that the consuming
21 // application is using a recent Android-Gradle plugin (say, 3.4+). There were
22 // issues with Jetifier, and issues with .jar vs. .aar extensions, in older
25 import groovy.json.JsonSlurper
28 logger.lifecycle("[substitute-local-geckoview] ${message}")
32 logger.warn("[substitute-local-geckoview] Warning: ${message}")
35 if (!project.ext.has('topsrcdir')) {
36 throw new GradleException("ext.topsrcdir must be specified to substitute for a local GeckoView")
40 * Loads the mozconfig and returns any variables derived from it, avoiding side effects.
42 * This method is relatively slow because it calls mach, which starts a python interpreter, will
43 * becomes very slow if called for numerous subprojects. Therefore, it should only be called once
47 apply from: "${topsrcdir}/mobile/android/gradle/mach_env.gradle"
49 // Cribbed from https://hg.mozilla.org/mozilla-central/file/tip/settings.gradle. When run in
50 // topobjdir, `mach environment` correctly finds the mozconfig corresponding to that object
52 def commandLine = ["${topsrcdir}/mach", "environment", "--format", "json", "--verbose"]
53 def proc = commandLine.execute(
55 new File(ext.has('topobjdir') ? ext.get('topobjdir') : topsrcdir))
56 def standardOutput = new ByteArrayOutputStream()
57 proc.consumeProcessOutput(standardOutput, standardOutput)
60 // Only show the output if something went wrong.
61 if (proc.exitValue() != 0) {
62 throw new GradleException("Process '${commandLine}' finished with non-zero exit value ${proc.exitValue()}:\n\n${standardOutput.toString()}")
65 def slurper = new JsonSlurper()
66 def mozconfig = slurper.parseText(standardOutput.toString())
68 if (topsrcdir != mozconfig.topsrcdir) {
69 throw new GradleException("Specified topsrcdir ('${topsrcdir}') is not mozconfig topsrcdir ('${mozconfig.topsrcdir}')")
73 if (ext.has('topobjdir')) {
74 topobjdir = ext.topobjdir
76 topobjdir = mozconfig.topobjdir
77 log("Found topobjdir ${topobjdir} from topsrcdir ${topsrcdir}")
80 if (mozconfig.substs.MOZ_BUILD_APP != 'mobile/android') {
81 throw new GradleException("Building with Gradle is only supported for GeckoView, i.e., MOZ_BUILD_APP == 'mobile/android'.")
84 log("Will substitute GeckoView (geckoview-{nightly,beta}) with local GeckoView (geckoview-default) from ${topobjdir}/gradle/build/mobile/android/geckoview/maven")
86 if (!mozconfig.substs.COMPILE_ENVIRONMENT) {
87 log("To update the local GeckoView, run `./mach gradle geckoview:publishWithGeckoBinariesDebugPublicationToMavenRepository` in ${topsrcdir}")
89 log("To update the local GeckoView, run `./mach build binaries && ./mach gradle geckoview:publishWithGeckoBinariesDebugPublicationToMavenRepository` in ${topsrcdir}")
92 return [mozconfig, topobjdir]
95 // This script is expected to be called for every subproject in the build (in ac, this is over 100)
96 // but loadMozconfig should only be called once per build (see the javadoc) so we store the output
97 // of that call as a global variable and re-use it when this script is called again.
98 def LOAD_MOZCONFIG_CACHE = "substitute-local-geckoview-mozconfig-cache"
99 if (!rootProject.ext.has(LOAD_MOZCONFIG_CACHE)) {
100 rootProject.ext.set(LOAD_MOZCONFIG_CACHE, loadMozconfig())
102 def (mozconfig, topobjdir) = rootProject.ext.get(LOAD_MOZCONFIG_CACHE)
106 name "Local GeckoView Maven repository"
107 url "${topobjdir}/gradle/maven"
111 configurations.all { config ->
112 // Like `geckoview-nightly` for a multi-architecture fat AAR or
113 // `geckoview-nightly-armeabi-v7a` for an architecture-specific AAR.
114 def geckoviewModules = [
116 'geckoview-nightly-armeabi-v7a',
117 'geckoview-nightly-arm64-v8a',
118 'geckoview-nightly-x86',
119 'geckoview-nightly-x86_64',
121 'geckoview-beta-armeabi-v7a',
122 'geckoview-beta-arm64-v8a',
123 'geckoview-beta-x86',
124 'geckoview-beta-x86_64',
127 def geckoviewOmniModules = [
128 'geckoview-nightly-omni',
129 'geckoview-nightly-omni-armeabi-v7a',
130 'geckoview-nightly-omni-arm64-v8a',
131 'geckoview-nightly-omni-x86',
132 'geckoview-nightly-omni-x86_64',
133 'geckoview-beta-omni',
134 'geckoview-beta-omni-armeabi-v7a',
135 'geckoview-beta-omni-arm64-v8a',
136 'geckoview-beta-omni-x86',
137 'geckoview-beta-omni-x86_64',
140 if (config.isCanBeResolved()) {
141 config.resolutionStrategy { strategy ->
142 dependencySubstitution {
144 // We could restrict based on target architecture, but there doesn't seem to
145 // be much advantage to doing so right now.
147 if (!(dependency.requested instanceof ModuleComponentSelector)) {
148 // We can only substitute for a module: we're never going to substitute
153 def group = dependency.requested.group
154 def module = dependency.requested.module
155 if (group == 'org.mozilla.geckoview'
156 && (geckoviewModules.contains(module) || geckoviewOmniModules.contains(module))) {
158 def isLite = mozconfig.substs.MOZ_ANDROID_GECKOVIEW_LITE
161 name = 'geckoview-default'
163 name = 'geckoview-default-omni'
166 if (geckoviewModules.contains(module) && !isLite) {
167 warn("Substituting a geckoview omni build into a lite dependency. Add ac_add_options --enable-geckoview-lite to ${mozconfig.mozconfig.path} to fix this.")
168 } else if (geckoviewOmniModules.contains(module) && isLite) {
169 // Substituting lite into omni is unlikely to work at
170 // all so we just error out here.
171 throw new GradleException("Substituting a geckoview lite build into an omni dependency. Remove ac_add_options --enable-geckoview-lite in ${mozconfig.mozconfig.path} to fix this.")
174 log("Substituting ${group}:${dependency.requested.module} with local GeckoView ${group}:${name} in ${config}")
176 dependency.useTarget([group: group, name: name, version: '+'])
178 // We substitute with a dynamic version ('+'). It seems that Gradle
179 // discovers the underlying AAR is out of date correctly based on file
180 // timestamp already, but let's try to avoid some class of cache
181 // invalidation error while we're here.
182 strategy.cacheDynamicVersionsFor 0, 'seconds'
183 strategy.cacheChangingModulesFor 0, 'seconds'