Source code

Revision control

Copy as Markdown

Other Tools

import org.gradle.api.services.BuildServiceParameters
import org.tomlj.Toml
import org.tomlj.TomlParseResult
import org.tomlj.TomlTable
buildscript {
repositories {
gradle.mozconfig.substs.GRADLE_MAVEN_REPOSITORIES.each { repository ->
maven {
url = repository
if (gradle.mozconfig.substs.ALLOW_INSECURE_GRADLE_REPOSITORIES) {
allowInsecureProtocol = true
}
}
}
}
dependencies {
classpath libs.android.gradle.plugin
classpath libs.tomlj
// Used in mobile/android/fenix/app/build.gradle
classpath libs.androidx.benchmark.gradle
classpath libs.androidx.navigation.safeargs
classpath libs.osslicenses.plugin
classpath libs.mozilla.glean.gradle.plugin
}
}
plugins {
id 'ApkSizePlugin'
id "mozac.ConfigPlugin"
id 'org.mozilla.conventions.mach-tasks'
alias(libs.plugins.android.library) apply false
alias(libs.plugins.dependency.analysis)
alias(libs.plugins.detekt)
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.kotlin.compose) apply false
alias(libs.plugins.ksp)
alias(libs.plugins.spotless)
}
def tryInt = { string ->
if (string == null) {
return string
}
if (string.isInteger()) {
return string as Integer
}
return string
}
abstract class VerifyGleanVersionTask extends DefaultTask {
@InputFile
final RegularFileProperty source = project.objects.fileProperty().convention(project.layout.projectDirectory.file("Cargo.lock"))
@Input
String expectedVersion = project.ext.gleanVersion
@OutputFile
final RegularFileProperty outputFile = project.objects.fileProperty()
@TaskAction
void verifyGleanVersion() {
def foundVersion = getRustVersionFor(source.get().asFile, "glean")
if (expectedVersion != foundVersion) {
throw new GradleException("Mismatched Glean version, expected: '${expectedVersion}'," +
" found '${foundVersion}'")
} else {
logger.lifecycle("verifyGleanVersion> expected version matches found version '${foundVersion}'")
}
outputFile.get().asFile.text = "glean-${foundVersion}"
}
// Parses the Cargo.lock and returns the version for the given package name.
static String getRustVersionFor(file, packageName) {
String version = null;
TomlParseResult result = Toml.parse(file.getText());
for (object in result.getArray("package").toList()) {
def table = (TomlTable) object
if (table.getString("name") == packageName) {
if (version != null) {
throw new GradleException("Multiple versions for '${packageName}' found." +
" Ensure '${packageName}' is only included once.")
}
version = table.getString("version")
}
}
return version
}
}
tasks.register("verifyGleanVersion", VerifyGleanVersionTask) {
outputFile.convention(layout.buildDirectory.file("glean/verifyGleanVersion.marker"))
}
allprojects {
// Expose the per-object-directory configuration to all projects.
ext {
mozconfig = gradle.mozconfig
topsrcdir = gradle.mozconfig.topsrcdir
topobjdir = gradle.mozconfig.topobjdir
gleanVersion = libs.versions.glean.get() // Verification done in verifyGleanVersion task
artifactSuffix = getArtifactSuffix()
versionName = getVersionName()
versionCode = computeVersionCode()
versionNumber = getVersionNumber()
buildId = getBuildId()
buildToolsVersion = mozconfig.substs.ANDROID_BUILD_TOOLS_VERSION
compileSdkMajorVersion = tryInt(mozconfig.substs.ANDROID_COMPILE_SDK_MAJOR)
compileSdkMinorVersion = tryInt(mozconfig.substs.ANDROID_COMPILE_SDK_MINOR)
minSdkVersion = tryInt(mozconfig.substs.MOZ_ANDROID_MIN_SDK_VERSION)
targetSdkVersion = tryInt(mozconfig.substs.ANDROID_TARGET_SDK)
manifestPlaceholders = [
ANDROID_TARGET_SDK: mozconfig.substs.ANDROID_TARGET_SDK,
MOZ_ANDROID_MIN_SDK_VERSION: mozconfig.substs.MOZ_ANDROID_MIN_SDK_VERSION,
]
}
afterEvaluate {
if (it.hasProperty('android')) {
android {
buildToolsVersion gradle.mozconfig.substs.ANDROID_BUILD_TOOLS_VERSION
testOptions {
unitTests.includeAndroidResources = true
}
}
}
}
// Use the semanticdb-javac and semanticdb-kotlinc plugins to generate semanticdb files for Searchfox
if (mozconfig.substs.ENABLE_MOZSEARCH_PLUGIN || mozconfig.substs.DOWNLOAD_ALL_GRADLE_DEPENDENCIES) {
def targetRoot = new File(topobjdir, "mozsearch_java_index")
afterEvaluate {
def addDependencyToConfigurationIfExists = { configurationName, dependency ->
def configuration = configurations.findByName(configurationName)
if (configuration != null) {
dependencies.add(configurationName, dependency)
}
}
addDependencyToConfigurationIfExists("compileOnly", libs.semanticdb.java)
addDependencyToConfigurationIfExists("testCompileOnly", libs.semanticdb.java)
addDependencyToConfigurationIfExists("androidTestCompileOnly", libs.semanticdb.java)
addDependencyToConfigurationIfExists("kotlinCompilerPluginClasspath", libs.semanticdb.kotlin)
}
tasks.withType(JavaCompile) {
options.compilerArgs += [
"-Xplugin:semanticdb -sourceroot:${topsrcdir} -targetroot:${targetRoot}",
]
}
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
compilerOptions.freeCompilerArgs.addAll([
"-P", "plugin:semanticdb-kotlinc:sourceroot=${topsrcdir}".toString(),
"-P", "plugin:semanticdb-kotlinc:targetroot=${targetRoot}".toString(),
])
}
}
}
// Non-official versions are like "61.0a1" or "61.0b1", where "a1" and "b1"
// are the milestone.
// This simply strips that off, leaving "61.0" in this example.
def getAppVersionWithoutMilestone() {
return project.ext.mozconfig.substs.MOZ_APP_VERSION.replaceFirst(/[ab][0-9]/, "")
}
// This converts MOZ_APP_VERSION into an integer
// version code.
//
// We take something like 58.1.2a1 and come out with 5800102
// This gives us 3 digits for the major number, and 2 digits
// each for the minor and build number. Beta and Release
//
// This must be synchronized with _compute_gecko_version(...) in /taskcluster/gecko_taskgraph/transforms/task.py
def computeVersionCode() {
String appVersion = getAppVersionWithoutMilestone()
// Split on the dot delimiter, e.g. 58.1.1a1 -> ["58, "1", "1a1"]
String[] parts = appVersion.split('\\.')
assert parts.size() == 2 || parts.size() == 3
// Major
int code = Integer.parseInt(parts[0]) * 100000
// Minor
code += Integer.parseInt(parts[1]) * 100
// Build
if (parts.size() == 3) {
code += Integer.parseInt(parts[2])
}
return code;
}
def getVersionName() {
return "${mozconfig.substs.MOZ_APP_VERSION}-${mozconfig.substs.MOZ_UPDATE_CHANNEL}"
}
// Mimic Python: open(os.path.join(buildconfig.topobjdir, 'buildid.h')).readline().split()[2]
def getBuildId() {
return file("${topobjdir}/buildid.h").getText('utf-8').split()[2]
}
def getVersionNumber() {
def appVersion = getAppVersionWithoutMilestone()
def parts = appVersion.split('\\.')
def version = parts[0] + "." + parts[1] + "." + getBuildId()
def substs = project.ext.mozconfig.substs
if (!substs.MOZILLA_OFFICIAL && !substs.MOZ_ANDROID_FAT_AAR_ARCHITECTURES) {
// Use -SNAPSHOT versions locally to enable the local GeckoView substitution flow.
version += "-SNAPSHOT"
}
return version
}
def getArtifactSuffix() {
def substs = project.ext.mozconfig.substs
def suffix = ""
// Release artifacts don't specify the channel, for the sake of simplicity.
if (substs.MOZ_UPDATE_CHANNEL != 'release') {
suffix += "-${mozconfig.substs.MOZ_UPDATE_CHANNEL}"
}
return suffix
}
afterEvaluate {
subprojects { project ->
tasks.withType(JavaCompile) {
// Add compiler args for all code except third-party code.
options.compilerArgs += [
// Turn on all warnings, except...
"-Xlint:all",
// Deprecation, because we do use deprecated API for compatibility.
"-Xlint:-deprecation",
// Serial, because we don't use Java serialization.
"-Xlint:-serial",
// Classfile, because javac has a bug with MethodParameters attributes
"-Xlint:-classfile"]
// In GeckoView java projects only, turn all remaining warnings
// into errors unless marked by @SuppressWarnings.
def projectName = project.getName()
if (projectName.startsWith('geckoview')
|| projectName == 'annotations'
|| projectName == 'exoplayer2'
|| projectName == 'messaging_example'
|| projectName == 'port_messaging_example'
|| projectName == 'test_runner'
) {
options.compilerArgs += [
"-Werror"
]
}
}
project.configurations.configureEach {
// Dependencies can't depend on a different major version of Glean than A-C itself.
resolutionStrategy.eachDependency { details ->
if (details.requested.group == 'org.mozilla.telemetry'
&& details.requested.name.contains('glean') ) {
def requested = details.requested.version.tokenize(".")
def defined = project.ext.gleanVersion.tokenize(".")
// Check the major version
if (requested[0] != defined[0]) {
throw new AssertionError("Cannot resolve to a single Glean version. Requested: ${details.requested.version}, A-C uses: ${libs.mozilla.glean}")
} else {
// Enforce that all (transitive) dependencies are using the defined Glean version
details.useVersion project.ext.gleanVersion
}
}
}
resolutionStrategy.capabilitiesResolution.withCapability("org.mozilla.telemetry:glean-native") {
def toBeSelected = candidates.find { it.id instanceof ModuleComponentIdentifier && it.id.module.contains('geckoview') }
if (toBeSelected != null) {
select(toBeSelected)
}
because 'use GeckoView Glean instead of standalone Glean'
}
}
}
}
subprojects { project ->
// Perform spotless lint in GeckoView projects only.
// Sync with spotless_projects in mobile/android/geckoview/gradle.configure.
def projectName = project.getName()
if (projectName.startsWith('geckoview')
|| projectName == 'annotations'
|| projectName == 'exoplayer2'
|| projectName == 'messaging_example'
|| projectName == 'port_messaging_example'
|| projectName == 'test_runner'
) {
apply plugin: "com.diffplug.spotless"
spotless {
lineEndings = com.diffplug.spotless.LineEnding.UNIX
java {
target project.fileTree(project.projectDir) {
include '**/*.java'
exclude '**/thirdparty/**'
}
googleJavaFormat(libs.versions.google.java.format.get())
}
kotlin {
target project.fileTree(project.projectDir) {
include '**/*.kt'
exclude '**/thirdparty/**'
}
ktlint("${libs.versions.ktlint.get()}").setEditorConfigPath("${topsrcdir}/mobile/android/geckoview/.editorconfig")
}
}
// explicitly depending on Spotless plugin dependencies.
project.configurations {
googleJavaFormat
}
project.dependencies {
googleJavaFormat libs.google.java.format
}
}
afterEvaluate {
// Our vendored copy of exoplayer2 hits build failures when targeting Java 17.
// Given our intent to remove it in the near future, just leave it alone here.
if (it.hasProperty('android') && projectName != 'exoplayer2') {
kotlin {
jvmToolchain(config.jvmTargetCompatibility)
}
}
if (it.hasProperty('android')
&& (project.projectDir.absolutePath.contains("android-components")
|| projectName == "fenix"
|| projectName == "focus-android")
) {
dependencies {
lintChecks project.project(':components:tooling-lint')
}
android {
// Copied from subbproject's build.gradle
lint {
baseline = file("${project.projectDir}/lint-baseline.xml")
}
}
}
}
project.configurations.configureEach {
resolutionStrategy.capabilitiesResolution.withCapability("org.mozilla.telemetry:glean-native") {
def toBeSelected = candidates.find {
it.id instanceof ProjectComponentIdentifier && it.id.projectName.contains('geckoview')
}
if (toBeSelected != null) {
select(toBeSelected)
}
because 'use GeckoView Glean instead of standalone Glean'
}
}
}
// These are convenience helpers to run lints over all the subprojects
def mobileProjects = [":fenix", ":focus-android", ":android-components"]
tasks.register("lint") {
group = "verification"
description = "Runs lint on all mobile projects."
mobileProjects.each { dependsOn "${it}:lint" }
}
tasks.register("ktlint") {
group = "verification"
description = "Runs ktlint on all mobile projects."
mobileProjects.each { dependsOn "${it}:ktlint" }
}
tasks.register("ktlintFormat") {
group = "formatting"
description = "Runs ktlintFormat on all mobile projects."
mobileProjects.each { dependsOn "${it}:ktlintFormat" }
}
tasks.named("detekt").configure {
// NOTE: We are replacing the default 'detekt' task.
description = "Runs detekt on all mobile projects."
mobileProjects.each { dependsOn "${it}:detekt" }
}
tasks.register("lint-a-c") {
group = "verification"
description = "(Deprecated) alias for :android-components:lint"
dependsOn ":android-components:lint"
}