Skip to content

Commit 8d133dd

Browse files
mcararejonalmeida
authored andcommitted
Update AGP to 9.0.0-beta03
- Update `android-gradle-plugin` version in version catalog. - Remove `kotlin-android` plugin alias from version catalog and root build file. - Migrate `android.applicationVariants.configureEach` logic in `app/build.gradle` to `androidComponents` API (`beforeVariants` and `onVariants`) to support AGP 9.0. - Use `variant.buildConfigFields.put` instead of `buildConfigField`. - Update version code generation logic to use `variant.outputs` within `onVariants`. - Remove `variantFilter` block in favor of `beforeVariants` disabling. - Migrate `kotlinOptions.allWarningsAsErrors` to `tasks.withType(KotlinCompile).compilerOptions`.
1 parent 48ef5f0 commit 8d133dd

File tree

3 files changed

+89
-136
lines changed

3 files changed

+89
-136
lines changed

app/build.gradle

Lines changed: 75 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,87 @@ plugins {
66
alias libs.plugins.android.application
77
alias libs.plugins.compose.compiler
88
alias libs.plugins.dependency.analysis
9-
alias libs.plugins.kotlin.android
109
}
1110

1211
apply from: "$project.rootDir/automation/gradle/versionCode.gradle"
1312

13+
def baseVersionCode = generatedVersionCode
14+
15+
import com.android.build.api.variant.BuildConfigField
1416
import com.android.build.api.variant.FilterConfiguration
15-
import com.android.build.gradle.internal.tasks.AppPreBuildTask
1617
import groovy.json.JsonOutput
1718

19+
androidComponents {
20+
beforeVariants(selector().withBuildType("release")) { variantBuilder ->
21+
variantBuilder.enable = false
22+
}
23+
24+
onVariants(selector().all()) { variant ->
25+
// -------------------------------------------------------------------------
26+
// 1. Sentry Token
27+
// -------------------------------------------------------------------------
28+
String sentryToken = "null"
29+
try {
30+
File tokenFile = file("${rootDir}/.sentry_token")
31+
if (tokenFile.exists()) {
32+
sentryToken = '"' + tokenFile.text.trim() + '"'
33+
println("(Added Sentry token from file)")
34+
}
35+
} catch (Exception ignored) { }
36+
37+
variant.buildConfigFields.put("SENTRY_TOKEN",
38+
new BuildConfigField("String", sentryToken, "Sentry Token"))
39+
40+
// -------------------------------------------------------------------------
41+
// 2. Crash Reporting & Telemetry
42+
// -------------------------------------------------------------------------
43+
boolean crashReporting = project.hasProperty("crashReportEnabled") && project.property("crashReportEnabled") == "true"
44+
variant.buildConfigFields.put("CRASH_REPORTING_ENABLED",
45+
new BuildConfigField("boolean", crashReporting.toString(), "Crash Reporting"))
46+
47+
boolean telemetry = project.hasProperty("telemetry") && project.property("telemetry") == "true"
48+
variant.buildConfigFields.put("TELEMETRY_ENABLED",
49+
new BuildConfigField("boolean", telemetry.toString(), "Telemetry"))
50+
51+
// -------------------------------------------------------------------------
52+
// 3. Official Build Flag
53+
// -------------------------------------------------------------------------
54+
boolean official = project.hasProperty("official") || gradle.hasProperty("localProperties.official")
55+
variant.buildConfigFields.put("MOZILLA_OFFICIAL",
56+
new BuildConfigField("Boolean", official.toString(), "Official Build"))
57+
58+
// -------------------------------------------------------------------------
59+
// 4. Version Codes (Only if IS_RELEASED is true)
60+
// -------------------------------------------------------------------------
61+
// Note: In the new API, checking buildConfigFields defined in build types is harder.
62+
// We check the build type name directly instead.
63+
if (variant.buildType == "nightly") {
64+
// Logic adapted for new API
65+
def baseCode = baseVersionCode
66+
if (baseCode % 2 != 0) baseCode += 1 // Ensure even
67+
68+
variant.outputs.each { output ->
69+
def abiFilter = output.filters.find { it.filterType == FilterConfiguration.FilterType.ABI } // Note: Accessing specific ABI names in AGP 9 via 'filters' can be tricky
70+
def abiName = abiFilter?.identifier // This returns "x86", "arm64-v8a", etc.
71+
72+
// Simplified mapping for demonstration:
73+
def addedCode = 0
74+
if (project.hasProperty("aab")) {
75+
addedCode = 1
76+
} else if (abiName != null) {
77+
// FIX: Check the abiName identifier instead of output.name
78+
if (abiName == "x86_64") addedCode = 6
79+
else if (abiName == "x86") addedCode = 4
80+
else if (abiName == "arm64-v8a") addedCode = 2
81+
}
82+
83+
output.versionCode.set(baseCode + addedCode)
84+
output.versionName.set(Config.releaseVersionName(project))
85+
}
86+
}
87+
}
88+
}
89+
1890
android {
1991
defaultConfig {
2092
applicationId "org.mozilla.reference.browser"
@@ -35,7 +107,7 @@ android {
35107

36108
def releaseTemplate = {
37109
minifyEnabled true
38-
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
110+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
39111
matchingFallbacks = ['release'] // Use on the "release" build type in dependencies (AARs)
40112

41113
if (gradle.hasProperty("localProperties.autosignReleaseWithDebugKey")) {
@@ -61,12 +133,6 @@ android {
61133
}
62134
}
63135

64-
variantFilter { // There's a "release" build type that exists by default that we don't use (it's replaced by "nightly" and "beta")
65-
if (buildType.name == 'release') {
66-
setIgnore true
67-
}
68-
}
69-
70136
testOptions {
71137
execution = 'ANDROIDX_TEST_ORCHESTRATOR'
72138
animationsDisabled = true
@@ -95,127 +161,6 @@ kotlin {
95161
jvmToolchain(Config.jvmTargetCompatibility)
96162
}
97163

98-
def baseVersionCode = generatedVersionCode
99-
100-
android.applicationVariants.configureEach { variant ->
101-
102-
// -------------------------------------------------------------------------------------------------
103-
// Sentry: Read token from local file if it exists (Only release builds)
104-
// -------------------------------------------------------------------------------------------------
105-
print("Sentry token: "+ variant.name)
106-
try {
107-
def token = new File("${rootDir}/.sentry_token").text.trim()
108-
buildConfigField 'String', 'SENTRY_TOKEN', '"' + token + '"'
109-
println "(Added from .sentry_token file)"
110-
} catch (FileNotFoundException ignored) {
111-
buildConfigField 'String', 'SENTRY_TOKEN', 'null'
112-
println(" :( ")
113-
}
114-
115-
// -------------------------------------------------------------------------------------------------
116-
// Activating crash reports with command line parameter.
117-
// -------------------------------------------------------------------------------------------------
118-
if (project.hasProperty("crashReportEnabled") && project.property("crashReportEnabled") == "true") {
119-
buildConfigField 'boolean', 'CRASH_REPORTING_ENABLED', 'true'
120-
} else {
121-
buildConfigField 'boolean', 'CRASH_REPORTING_ENABLED', 'false'
122-
}
123-
124-
// -------------------------------------------------------------------------------------------------
125-
// Activating telemetry with command line paramter.
126-
// -------------------------------------------------------------------------------------------------
127-
128-
if (project.hasProperty("telemetry") && project.property("telemetry") == "true") {
129-
buildConfigField 'boolean', 'TELEMETRY_ENABLED', 'true'
130-
} else {
131-
buildConfigField 'boolean', 'TELEMETRY_ENABLED', 'false'
132-
}
133-
134-
// -------------------------------------------------------------------------------------------------
135-
// Generating version codes for Google Play
136-
// -------------------------------------------------------------------------------------------------
137-
if (variant.buildType.buildConfigFields['IS_RELEASED']?.value) {
138-
// The Google Play Store does not allow multiple APKs for the same app that all have the
139-
// same version code. Therefore we need to have different version codes for our ARM and x86
140-
// builds. See https://developer.android.com/studio/publish/versioning
141-
142-
// Our x86 builds need a higher version code to avoid installing ARM builds on an x86 device
143-
// with ARM compatibility mode.
144-
145-
// AAB builds need a version code that is distinct from any APK builds. Since AAB and APK
146-
// builds may run in parallel, AAB and APK version codes might be based on the same
147-
// (minute granularity) time of day. To avoid conflicts, we ensure the minute portion
148-
// of the version code is even for APKs and odd for AABs.
149-
150-
def versionName = Config.releaseVersionName(project)
151-
152-
variant.outputs.each { output ->
153-
def abi = output.getFilter(FilterConfiguration.FilterType.ABI.name())
154-
def aab = project.hasProperty("aab")
155-
156-
// ensure baseVersionCode is an even number
157-
if (baseVersionCode % 2) {
158-
baseVersionCode = baseVersionCode + 1
159-
}
160-
161-
def versionCodeOverride = baseVersionCode
162-
163-
if (aab) {
164-
// AAB version code is odd
165-
versionCodeOverride = baseVersionCode + 1
166-
println("versionCode for AAB = $versionCodeOverride")
167-
} else {
168-
// APK version codes are even
169-
if (abi == "x86_64") {
170-
versionCodeOverride = baseVersionCode + 6
171-
} else if (abi == "x86") {
172-
versionCodeOverride = baseVersionCode + 4
173-
} else if (abi == "arm64-v8a") {
174-
versionCodeOverride = baseVersionCode + 2
175-
} else if (abi == "armeabi-v7a") {
176-
versionCodeOverride = baseVersionCode
177-
}
178-
println("versionCode for $abi = $versionCodeOverride")
179-
}
180-
181-
output.versionNameOverride = versionName
182-
output.versionCodeOverride = versionCodeOverride
183-
}
184-
185-
// If this is a release build, validate that "versionName" is set
186-
tasks.withType(AppPreBuildTask).configureEach { prebuildTask ->
187-
// You can't add a closure to a variant, so we need to look for an early variant-specific type
188-
// of task (AppPreBuildTask is the first) and filter to make sure we're looking at the task for
189-
// this variant that we're currently configuring
190-
if (prebuildTask.variantName != variant.name) {
191-
return
192-
}
193-
194-
// Append to the task so the first thing it does is run our validation
195-
prebuildTask.doFirst {
196-
if (!project.hasProperty('versionName')) {
197-
throw new RuntimeException("Release builds require the 'versionName' property to be set.\n" +
198-
"If you're using an IDE, set your build variant to be a \"debug\" type.\n" +
199-
"If you're using the command-line, either build a debug variant instead ('./gradlew assembleDebug')\n" +
200-
"\tor continue building the release build and set the \"versionName\" property ('./gradlew -PversionName=<...> assembleNightly').")
201-
// TODO when Android Studio 3.5.0 is prevalent, we can set the "debug" build type as the default
202-
// https://issuetracker.google.com/issues/36988145#comment59
203-
}
204-
}
205-
}
206-
}
207-
208-
// -------------------------------------------------------------------------------------------------
209-
// BuildConfig: Set flag for official builds; similar to MOZILLA_OFFICIAL in mozilla-central.
210-
// -------------------------------------------------------------------------------------------------
211-
212-
if (project.hasProperty("official") || gradle.hasProperty("localProperties.official")) {
213-
buildConfigField 'Boolean', 'MOZILLA_OFFICIAL', 'true'
214-
} else {
215-
buildConfigField 'Boolean', 'MOZILLA_OFFICIAL', 'false'
216-
}
217-
}
218-
219164
// Select the Glean from GeckoView.
220165
// `service-sync-logins` requires Glean, which pulls in glean-native,
221166
// but that's also provided by geckoview-omni, so now we need to select which one to use.

build.gradle

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
import io.gitlab.arturbosch.detekt.Detekt
4+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
45

56
buildscript {
7+
dependencies {
8+
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin") {
9+
version { strictly(libs.versions.kotlin.get()) }
10+
}
11+
}
12+
613
repositories {
714
if (project.hasProperty("googleRepo")) {
815
maven {
@@ -31,7 +38,6 @@ plugins {
3138
alias libs.plugins.compose.compiler apply false
3239
alias libs.plugins.dependency.analysis
3340
alias libs.plugins.detekt
34-
alias libs.plugins.kotlin.android apply false
3541
}
3642

3743
allprojects {
@@ -88,9 +94,6 @@ subprojects {
8894
}
8995

9096
android {
91-
kotlinOptions {
92-
kotlinOptions.allWarningsAsErrors = true
93-
}
9497

9598
testOptions {
9699
unitTests {
@@ -104,6 +107,12 @@ subprojects {
104107
}
105108
}
106109

110+
tasks.withType(KotlinCompile).configureEach {
111+
compilerOptions {
112+
allWarningsAsErrors = true
113+
}
114+
}
115+
107116
if (project.hasProperty("coverage") && project.name != "support-test") {
108117
tasks.withType(Test).configureEach {
109118
jacoco.includeNoLocationClasses = true

gradle/libs.versions.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
android-components = "147.0.20251203092053"
44

55
# AGP
6-
android-gradle-plugin = "8.13.1"
6+
android-gradle-plugin = "9.0.0-beta03"
77

88
# Kotlin
99
kotlin = "2.2.21"
@@ -193,4 +193,3 @@ android-application = { id = "com.android.application", version.ref = "android-g
193193
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
194194
dependency-analysis = { id = "com.autonomousapps.dependency-analysis", version.ref = "dependency-analysis" }
195195
detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt" }
196-
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }

0 commit comments

Comments
 (0)