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")
39 // Cribbed from https://hg.mozilla.org/mozilla-central/file/tip/settings.gradle. When run in
40 // topobjdir, `mach environment` correctly finds the mozconfig corresponding to that object
42 def commandLine = ["${topsrcdir}/mach", "environment", "--format", "json", "--verbose"]
43 def proc = commandLine.execute(null, new File(ext.has('topobjdir') ? ext.get('topobjdir') : topsrcdir))
44 def standardOutput = new ByteArrayOutputStream()
45 proc.consumeProcessOutput(standardOutput, standardOutput)
48 // Only show the output if something went wrong.
49 if (proc.exitValue() != 0) {
50 throw new GradleException("Process '${commandLine}' finished with non-zero exit value ${proc.exitValue()}:\n\n${standardOutput.toString()}")
53 def slurper = new JsonSlurper()
54 def mozconfig = slurper.parseText(standardOutput.toString())
56 if (topsrcdir != mozconfig.topsrcdir) {
57 throw new GradleException("Specified topsrcdir ('${topsrcdir}') is not mozconfig topsrcdir ('${mozconfig.topsrcdir}')")
60 if (!ext.has('topobjdir')) {
61 ext.topobjdir = mozconfig.topobjdir
62 log("Found topobjdir ${topobjdir} from topsrcdir ${topsrcdir}")
65 if (mozconfig.substs.MOZ_BUILD_APP != 'mobile/android') {
66 throw new GradleException("Building with Gradle is only supported for GeckoView, i.e., MOZ_BUILD_APP == 'mobile/android'.")
69 log("Will substitute GeckoView (geckoview-{nightly,beta}) with local GeckoView (geckoview-default) from ${topobjdir}/gradle/build/mobile/android/geckoview/maven")
71 if (!mozconfig.substs.COMPILE_ENVIRONMENT) {
72 log("To update the local GeckoView, run `./mach gradle geckoview:publishWithGeckoBinariesDebugPublicationToMavenRepository` in ${topsrcdir}")
74 log("To update the local GeckoView, run `./mach build binaries && ./mach gradle geckoview:publishWithGeckoBinariesDebugPublicationToMavenRepository` in ${topsrcdir}")
79 name "Local GeckoView Maven repository"
80 url "${topobjdir}/gradle/build/mobile/android/geckoview/maven"
84 configurations.all { config ->
85 // Like `geckoview-nightly` for a multi-architecture fat AAR or
86 // `geckoview-nightly-armeabi-v7a` for an architecture-specific AAR.
87 def geckoviewModules = [
89 'geckoview-nightly-armeabi-v7a',
90 'geckoview-nightly-arm64-v8a',
91 'geckoview-nightly-x86',
92 'geckoview-nightly-x86_64',
94 'geckoview-beta-armeabi-v7a',
95 'geckoview-beta-arm64-v8a',
97 'geckoview-beta-x86_64',
100 def geckoviewOmniModules = [
101 'geckoview-nightly-omni',
102 'geckoview-nightly-omni-armeabi-v7a',
103 'geckoview-nightly-omni-arm64-v8a',
104 'geckoview-nightly-omni-x86',
105 'geckoview-nightly-omni-x86_64',
106 'geckoview-beta-omni',
107 'geckoview-beta-omni-armeabi-v7a',
108 'geckoview-beta-omni-arm64-v8a',
109 'geckoview-beta-omni-x86',
110 'geckoview-beta-omni-x86_64',
113 if (config.isCanBeResolved()) {
114 config.resolutionStrategy { strategy ->
115 dependencySubstitution {
117 // We could restrict based on target architecture, but there doesn't seem to
118 // be much advantage to doing so right now.
120 if (!(dependency.requested instanceof ModuleComponentSelector)) {
121 // We can only substitute for a module: we're never going to substitute
126 def group = dependency.requested.group
127 def module = dependency.requested.module
128 if (group == 'org.mozilla.geckoview'
129 && (geckoviewModules.contains(module) || geckoviewOmniModules.contains(module))) {
131 def isLite = mozconfig.substs.MOZ_ANDROID_GECKOVIEW_LITE
134 name = 'geckoview-default'
136 name = 'geckoview-default-omni'
139 if (geckoviewModules.contains(module) && !isLite) {
140 warn("Substituting a geckoview omni build into a lite dependency. Add ac_add_options --enable-geckoview-lite to ${mozconfig.mozconfig.path} to fix this.")
141 } else if (geckoviewOmniModules.contains(module) && isLite) {
142 // Substituting lite into omni is unlikely to work at
143 // all so we just error out here.
144 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.")
147 log("Substituting ${group}:${dependency.requested.module} with local GeckoView ${group}:${name} in ${config}")
149 dependency.useTarget([group: group, name: name, version: '+'])
151 // We substitute with a dynamic version ('+'). It seems that Gradle
152 // discovers the underlying AAR is out of date correctly based on file
153 // timestamp already, but let's try to avoid some class of cache
154 // invalidation error while we're here.
155 strategy.cacheDynamicVersionsFor 0, 'seconds'
156 strategy.cacheChangingModulesFor 0, 'seconds'