@@ -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
1211apply from : " $project . rootDir /automation/gradle/versionCode.gradle"
1312
13+ def baseVersionCode = generatedVersionCode
14+
15+ import com.android.build.api.variant.BuildConfigField
1416import com.android.build.api.variant.FilterConfiguration
15- import com.android.build.gradle.internal.tasks.AppPreBuildTask
1617import 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+
1890android {
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- " \t or 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.
0 commit comments