Skip to content

Commit 8b6cf39

Browse files
committed
fix: support both build.gradle and build.gradle.kts file formats #305
1 parent eb5c413 commit 8b6cf39

File tree

2 files changed

+43
-21
lines changed

2 files changed

+43
-21
lines changed

Diff for: packages/flutterfire_cli/lib/src/firebase/firebase_android_writes.dart

+27-18
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@ import '../common/utils.dart';
77
import '../flutter_app.dart';
88
import 'firebase_options.dart';
99

10-
// https://regex101.com/r/Lj93lx/1
10+
// https://regex101.com/r/Lj93lx/2
1111
final _androidBuildGradleRegex = RegExp(
12-
r'dependencies\s*\{',
12+
r'dependencies\s*[\{]',
1313
multiLine: true,
1414
);
15-
// https://regex101.com/r/OZnO1j/1
15+
// https://regex101.com/r/OZnO1j/2
1616
final _androidAppBuildGradleRegex = RegExp(
17-
r'''(?:(^[\s]*?apply[\s]+plugin\:[\s]+['"]{1}com\.android\.application['"]{1})|(^[\s]*?id[\s]+["']com\.android\.application["']))''',
17+
r'''(?:(^[\s]*?apply[\s]+plugin\:[\s]+['"]{1}com\.android\.application['"]{1})|(^[\s]*?id[\s]+["']com\.android\.application["'])|plugins\s*\{\s*id\(['"]{1}com\.android\.application['"]{1}\))''',
1818
multiLine: true,
1919
);
20-
// https://regex101.com/r/ndlYVL/1
20+
// https://regex101.com/r/ndlYVL/2
2121
final _androidBuildGradleGoogleServicesRegex = RegExp(
22-
r'''((?<indentation>^[\s]*?)classpath\s?['"]{1}com\.google\.gms:google-services:.*?['"]{1}\s*?$)''',
22+
r'''((?<indentation>^[\s]*?)(?:classpath\s?['"]{1}com\.google\.gms:google-services:.*?['"]{1}\s*?$|id\(['"]{1}com\.google\.gms\.google-services['"]{1}\)))''',
2323
multiLine: true,
2424
);
25-
// https://regex101.com/r/pP1k6i/1
25+
// https://regex101.com/r/pP1k6i/2
2626
final _androidAppBuildGradleGoogleServicesRegex = RegExp(
27-
r'''(?:(^[\s]*?apply[\s]+plugin\:[\s]+['"]{1}com\.google\.gms\.google-services['"]{1})|(^[\s]*?id[\s]+['"]com\.google\.gms\.google-services['"]))''',
27+
r'''(?:(^[\s]*?apply[\s]+plugin\:[\s]+['"]{1}com\.google\.gms\.google-services['"]{1})|(^[\s]*?id[\s]+['"]com\.google\.gms\.google-services['"])|plugins\s*\{\s*id\(['"]{1}com\.google\.gms\.google-services['"]{1}\))''',
2828
multiLine: true,
2929
);
3030

@@ -57,10 +57,11 @@ String _applyGradleSettingsDependency(
5757
String version, {
5858
bool flutterfireComments = false,
5959
}) {
60-
if (flutterfireComments) {
61-
return '\n $_flutterFireConfigCommentStart\n id "$dependency" version "$version" apply false\n $_flutterFireConfigCommentEnd';
62-
}
63-
return '\n id "$dependency" version "$version" apply false';
60+
final kotlinDslContent = flutterfireComments
61+
? '\n $_flutterFireConfigCommentStart\n id("$dependency") version "$version" apply false\n $_flutterFireConfigCommentEnd'
62+
: '\n id("$dependency") version "$version" apply false';
63+
64+
return kotlinDslContent;
6465
}
6566

6667
enum BuildGradleConfiguration {
@@ -182,28 +183,36 @@ Future<void> gradleContentUpdates(
182183
final androidBuildGradleFile = File(
183184
path.join(
184185
flutterApp.androidDirectory.path,
185-
'build.gradle',
186+
'build.gradle.kts',
186187
),
187-
);
188+
).existsSync()
189+
? File(path.join(flutterApp.androidDirectory.path, 'build.gradle.kts'))
190+
: File(path.join(flutterApp.androidDirectory.path, 'build.gradle'));
191+
188192
final androidBuildGradleFileContents =
189193
androidBuildGradleFile.readAsStringSync();
190194

191195
final androidAppBuildGradleFile = File(
192196
path.join(
193197
flutterApp.androidDirectory.path,
194198
'app',
195-
'build.gradle',
199+
'build.gradle.kts',
196200
),
197-
);
201+
).existsSync()
202+
? File(path.join(flutterApp.androidDirectory.path, 'app', 'build.gradle.kts'))
203+
: File(path.join(flutterApp.androidDirectory.path, 'app', 'build.gradle'));
204+
198205
final androidAppBuildGradleFileContents =
199206
androidAppBuildGradleFile.readAsStringSync();
200207

201208
final androidGradleSettingsFile = File(
202209
path.join(
203210
flutterApp.androidDirectory.path,
204-
'settings.gradle',
211+
'settings.gradle.kts',
205212
),
206-
);
213+
).existsSync()
214+
? File(path.join(flutterApp.androidDirectory.path, 'settings.gradle.kts'))
215+
: File(path.join(flutterApp.androidDirectory.path, 'settings.gradle'));
207216

208217
final androidGradleSettingsFileContents =
209218
androidGradleSettingsFile.readAsStringSync();

Diff for: packages/flutterfire_cli/lib/src/flutter_app.dart

+16-3
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,22 @@ class FlutterApp {
123123
Directory(package.path),
124124
),
125125
);
126-
if (appGradleFile.existsSync()) {
127-
final fileContents = appGradleFile.readAsStringSync();
128-
// Captures old and new method for setting applicationId in app/build.gradle file:
126+
127+
final appGradleKtsFile = File(
128+
'${androidAppBuildGradlePathForAppDirectory(
129+
Directory(package.path),
130+
)}.kts',
131+
);
132+
133+
String? fileContents;
134+
if (appGradleKtsFile.existsSync()) {
135+
fileContents = appGradleKtsFile.readAsStringSync();
136+
} else if (appGradleFile.existsSync()) {
137+
fileContents = appGradleFile.readAsStringSync();
138+
}
139+
140+
if (fileContents != null) {
141+
// Captures old and new method for setting applicationId in both app/build.gradle and app/build.gradle.kt file:
129142
// https://regex101.com/r/d9i4G6/1
130143
final appIdRegex = RegExp(
131144
r'''applicationId\s*(?:=)?\s*['"](?<applicationId>([A-Za-z]{1}[A-Za-z\d_]*\.)+[A-Za-z][A-Za-z\d_]*)['"]''',

0 commit comments

Comments
 (0)