/
android.dart
458 lines (414 loc) · 14.6 KB
/
android.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
part of icons_launcher_cli;
/// Android icons template
final androidIcons = <AndroidMipMapIconTemplate>[
AndroidMipMapIconTemplate(directoryName: 'mipmap-mdpi', size: 48),
AndroidMipMapIconTemplate(directoryName: 'mipmap-hdpi', size: 72),
AndroidMipMapIconTemplate(directoryName: 'mipmap-xhdpi', size: 96),
AndroidMipMapIconTemplate(directoryName: 'mipmap-xxhdpi', size: 144),
AndroidMipMapIconTemplate(directoryName: 'mipmap-xxxhdpi', size: 192),
];
/// Android adaptive icons template
final adaptiveIcons = <AndroidMipMapIconTemplate>[
AndroidMipMapIconTemplate(directoryName: 'mipmap-mdpi', size: 108),
AndroidMipMapIconTemplate(directoryName: 'mipmap-hdpi', size: 162),
AndroidMipMapIconTemplate(directoryName: 'mipmap-xhdpi', size: 216),
AndroidMipMapIconTemplate(directoryName: 'mipmap-xxhdpi', size: 324),
AndroidMipMapIconTemplate(directoryName: 'mipmap-xxxhdpi', size: 432),
];
/// Start create Android icons
void _createAndroidIcons({required String imagePath}) {
CliLogger.info('Creating Android icons...');
final image = Icon.loadFile(imagePath);
if (image == null) {
CliLogger.error('The file $imagePath could not be read.',
level: CliLoggerLevel.two);
exit(1);
}
for (final template in androidIcons) {
_saveImageAndroid(template, image, ANDROID_ICON_FILE_NAME);
}
CliLogger.success(
'Generated launcher icon images',
level: CliLoggerLevel.two,
);
_createPlayStoreIcon(image);
_updateAndroidManifestIconLauncher();
}
/// Save the image to the Android directory
void _saveImageAndroid(
AndroidMipMapIconTemplate template,
Icon image,
String fileName,
) {
// When the flavor value is not specified we will place all the data inside the main directory.
// However if the flavor value is specified, we need to place the data in the correct directory.
// Default: android/app/src/main/res/
// With a flavor: android/app/src/[flavor name]/res/
final filePath =
'${_flavorHelper.androidResFolder}${template.directoryName}/$fileName';
image.saveResizedPng(template.size, filePath);
}
/// Create the play store icon
void _createPlayStoreIcon(Icon image) {
final template = AndroidMipMapIconTemplate(
directoryName: _flavorHelper.androidResFolder,
size: 512,
);
image.saveResizedPng(template.size,
'${template.directoryName}/$ANDROID_PLAYSTORE_ICON_FILE_NAME');
}
/// Update the Android manifest with the new launcher icon
void _updateAndroidManifestIconLauncher() {
final androidManifestFile = File(ANDROID_MANIFEST_FILE);
final androidManifestString = androidManifestFile.readAsStringSync();
final manifestLines = androidManifestString.split('\n');
final androidManifestUpdated = manifestLines.map((String line) {
if (line.contains('android:icon')) {
// Using RegExp replace the value of android:icon to point to the new icon
// anything but a quote of any length: [^"]*
// an escaped quote: \\" (escape slash, because it exists regex)
// quote, no quote / quote with things behind : \"[^"]*
// repeat as often as wanted with no quote at start: [^"]*(\"[^"]*)*
// escaping the slash to place in string: [^"]*(\\"[^"]*)*"
// result: any string which does only include escaped quotes
return line.replaceAll(RegExp(r'android:icon="[^"]*(\\"[^"]*)*"'),
'android:icon="@mipmap/$ANDROID_ICON_NAME"');
} else {
return line;
}
}).toList();
androidManifestFile.writeAsStringSync(androidManifestUpdated.join('\n'));
CliLogger.success(
'Updated android manifest launcher icon',
level: CliLoggerLevel.two,
);
}
/// Start android adaptive icons
void _createAndroidAdaptiveIcon({
required String background,
required String foreground,
String? round,
}) {
String message = 'Creating Android adaptive icons...';
if (round != null) {
message = 'Creating Android adaptive icons with round...';
}
CliLogger.info(message);
_createAdaptiveForeground(adaptiveIcons, foreground);
_createAdaptiveBackground(adaptiveIcons, background);
if (round != null) {
_createAdaptiveRound(androidIcons, round, isValidHexaCode(background));
} else {
_removeAndroidManifestIconLauncherRound();
}
}
/// Create the adaptive background
void _createAdaptiveBackground(
List<AndroidMipMapIconTemplate> adaptiveIcons,
String background,
) {
// Check background is hexa color or image
if (isValidHexaCode(background)) {
_handleColorsXmlFile(background);
_createIcLauncherColorXmlFile();
} else {
try {
final backgroundImage = Icon.loadFile(background);
if (backgroundImage == null) {
CliLogger.error(
'The file $background could not be read.',
level: CliLoggerLevel.two,
);
exit(1);
}
for (final template in adaptiveIcons) {
_saveImageAndroid(
template,
backgroundImage,
ANDROID_ADAPTIVE_BACKGROUND_ICON_FILE_NAME,
);
}
CliLogger.success(
'Generated adaptive background images',
level: CliLoggerLevel.two,
);
_createIcLauncherMipMapXmlFile();
} catch (e) {
CliLogger.error(
'Incorrect `$background` of `adaptive_background_color` or `adaptive_background_image`',
level: CliLoggerLevel.two,
);
exit(1);
}
}
}
/// Create the adaptive foreground
void _createAdaptiveForeground(
List<AndroidMipMapIconTemplate> adaptiveIcons,
String foreground,
) {
final foregroundImage = Icon.loadFile(foreground);
if (foregroundImage == null) {
CliLogger.error(
'The file $foreground could not be read.',
level: CliLoggerLevel.two,
);
exit(1);
}
for (final template in adaptiveIcons) {
_saveImageAndroid(
template,
foregroundImage,
ANDROID_ADAPTIVE_FOREGROUND_ICON_FILE_NAME,
);
}
CliLogger.success(
'Generated adaptive foreground images',
level: CliLoggerLevel.two,
);
}
/// Create the adaptive round
void _createAdaptiveRound(
List<AndroidMipMapIconTemplate> adaptiveIcons,
String round,
bool backgroundIsColor,
) {
final roundImage = Icon.loadFile(round);
if (roundImage == null) {
CliLogger.error(
'The file $round could not be read.',
level: CliLoggerLevel.two,
);
exit(1);
}
for (final template in adaptiveIcons) {
_saveImageAndroid(
template,
roundImage,
ANDROID_ADAPTIVE_ROUND_ICON_FILE_NAME,
);
}
CliLogger.success(
'Generated adaptive round images',
level: CliLoggerLevel.two,
);
_createIcLauncherRoundMipMapXmlFile(backgroundIsColor);
_createAndroidManifestIconLauncherRound();
}
/// Handle colors.xml file
void _handleColorsXmlFile(String backgroundColor) {
final File colorsXml =
File('${_flavorHelper.androidResFolder}$ANDROID_COLOR_FILE');
if (colorsXml.existsSync()) {
CliLogger.success(
'Updated colors.xml with color `$backgroundColor`',
level: CliLoggerLevel.two,
);
_updateColorsFile(colorsXml, backgroundColor);
} else {
CliLogger.success(
'Created colors.xml with color `$backgroundColor`',
level: CliLoggerLevel.two,
);
_createColorsFile(backgroundColor);
}
}
/// Create colors.xml file
void _createColorsFile(String backgroundColor) {
String color = backgroundColor;
if ((color.length == 4 || color.length == 7) && color.length != 9) {
color = '#FF' + backgroundColor.replaceAll('#', '');
} else {
CliLogger.error(
'Incorrect `$backgroundColor` of `adaptive_background_color`',
level: CliLoggerLevel.two,
);
exit(1);
}
final file = File('${_flavorHelper.androidResFolder}$ANDROID_COLOR_FILE');
file.createSync(recursive: true);
file.writeAsStringSync(getColorXmlContent(color));
}
// Update colors.xml file
void _updateColorsFile(File colorsXml, String backgroundColor) {
String color = backgroundColor;
if ((color.length == 4 || color.length == 7) && color.length != 9) {
color = '#FF' + backgroundColor.replaceAll('#', '');
} else {
CliLogger.error(
'Incorrect `$backgroundColor` of `adaptive_background_color`',
level: CliLoggerLevel.two,
);
exit(1);
}
final List<String> lines = colorsXml.readAsLinesSync();
bool foundExisting = false;
for (int x = 0; x < lines.length; x++) {
String line = lines[x];
if (line.contains('name="ic_launcher_background"')) {
foundExisting = true;
// replace anything between tags which does not contain another tag
line = line.replaceAll(RegExp(r'>([^><]*)<'), '>${color.toUpperCase()}<');
lines[x] = line;
break;
}
}
if (!foundExisting) {
lines.insert(lines.length - 1,
'\t<color name="ic_launcher_background">${color.toUpperCase()}</color>');
}
colorsXml.writeAsStringSync(lines.join('\n'));
}
/// Create ic_launcher_color.xml file
void _createIcLauncherColorXmlFile() {
final icLauncherXml = File(
'${_flavorHelper.androidResFolder}$ANDROID_ADAPTIVE_XML_DIR/$ANDROID_ADAPTIVE_XML_FILE_NAME');
icLauncherXml.createSync(recursive: true);
icLauncherXml.writeAsStringSync(IC_LAUNCHER_BACKGROUND_COLOR_XML);
CliLogger.success(
'Created `$ANDROID_ADAPTIVE_XML_FILE_NAME`',
level: CliLoggerLevel.two,
);
}
/// Create ic luncher xml file
void _createIcLauncherMipMapXmlFile() {
final icLauncherXml = File(
'${_flavorHelper.androidResFolder}$ANDROID_ADAPTIVE_XML_DIR/$ANDROID_ADAPTIVE_XML_FILE_NAME');
icLauncherXml.createSync(recursive: true);
icLauncherXml.writeAsStringSync(IC_LAUNCHER_MIP_MAP_XML);
CliLogger.success(
'Created `$ANDROID_ADAPTIVE_XML_FILE_NAME`',
level: CliLoggerLevel.two,
);
}
// void _createIcLauncherRoundColorXmlFile() {
// final icLauncherXml = File(
// '${_flavorHelper.androidResFolder}$ANDROID_ADAPTIVE_XML_DIR/$ANDROID_ADAPTIVE_ROUND_XML_FILE_NAME');
// icLauncherXml.createSync(recursive: true);
// icLauncherXml.writeAsStringSync(IC_LAUNCHER_ROUND_BACKGROUND_COLOR_XML);
// CliLogger.success(
// 'Created `$ANDROID_ADAPTIVE_ROUND_XML_FILE_NAME`',
// level: CliLoggerLevel.two,
// );
// }
/// Create ic_launcher_round.xml file
void _createIcLauncherRoundMipMapXmlFile(bool backgroundIsColor) {
final icLauncherXml = File(
'${_flavorHelper.androidResFolder}$ANDROID_ADAPTIVE_XML_DIR/$ANDROID_ADAPTIVE_ROUND_XML_FILE_NAME');
icLauncherXml.createSync(recursive: true);
icLauncherXml.writeAsStringSync(backgroundIsColor
? IC_LAUNCHER_ROUND_BACKGROUND_COLOR_XML
: IC_LAUNCHER_ROUND_MIP_MAP_XML);
CliLogger.success(
'Created `$ANDROID_ADAPTIVE_ROUND_XML_FILE_NAME`',
level: CliLoggerLevel.two,
);
}
/// Create android manifest icon launcher round
void _createAndroidManifestIconLauncherRound() {
final androidManifestFile = File(ANDROID_MANIFEST_FILE);
final androidManifestString = androidManifestFile.readAsStringSync();
final manifestLines = androidManifestString.split('\n');
final index =
manifestLines.indexWhere((line) => line.contains('android:roundIcon'));
if (index != -1) {
final lineUpdated = manifestLines.elementAt(index).replaceAll(
RegExp(r'android:roundIcon="[^"]*(\\"[^"]*)*"'),
'android:roundIcon="@mipmap/$ANDROID_ADAPTIVE_ROUND_ICON_NAME"',
);
manifestLines.replaceRange(index, index + 1, [lineUpdated]);
androidManifestFile.writeAsStringSync(manifestLines.join('\n'));
CliLogger.success(
'Updated `android:roundIcon` to manifest',
level: CliLoggerLevel.two,
);
} else {
final index =
manifestLines.indexWhere((line) => line.contains('android:icon'));
if (index != -1) {
final lineUpdated = manifestLines
.elementAt(index)
.replaceAll(RegExp(r'android:icon="[^"]*(\\"[^"]*)*"'), '''
android:icon="@mipmap/$ANDROID_ICON_NAME"
\t\tandroid:roundIcon="@mipmap/$ANDROID_ADAPTIVE_ROUND_ICON_NAME"''');
manifestLines.replaceRange(index, index + 1, [lineUpdated]);
androidManifestFile.writeAsStringSync(manifestLines.join('\n'));
CliLogger.success(
'Created `android:roundIcon` to manifest',
level: CliLoggerLevel.two,
);
}
}
}
/// Remove android manifest icon launcher round
void _removeAndroidManifestIconLauncherRound() {
final androidManifestFile = File(ANDROID_MANIFEST_FILE);
final androidManifestString = androidManifestFile.readAsStringSync();
final manifestLines = androidManifestString.split('\n');
final index =
manifestLines.indexWhere((line) => line.contains('android:roundIcon'));
if (index != -1) {
final lineUpdated = manifestLines
.elementAt(index)
.replaceAll(RegExp(r'android:roundIcon="[^"]*(\\"[^"]*)*"'), '');
manifestLines.replaceRange(index, index + 1, [lineUpdated]);
androidManifestFile.writeAsStringSync(manifestLines.join('\n'));
CliLogger.success('Removed `android:roundIcon` from manifest',
level: CliLoggerLevel.two);
}
}
/// Retrieves the minSdk value
int _minSdk() {
String? minSdkValue;
final File androidGradleFile = File(ANDROID_GRADLE_FILE);
final androidLines = androidGradleFile.readAsLinesSync();
//! First try -> app/build.gradle file
const androidLineKey = 'minSdkVersion';
minSdkValue = _getLineValueNumber(androidLines, androidLineKey);
//! Second try -> local.properties
if (minSdkValue == null) {
final localLines = File(ANDROID_LOCAL_PROPERTIES).readAsLinesSync();
const localKey = 'flutter.minSdkVersion=';
minSdkValue = _getLineValueNumber(localLines, localKey);
}
//! Third try -> flutter.gradle file (default flutter sdk)
if (minSdkValue == null) {
final gradleFile = '${_flutterSdk()}$FLUTTER_SDK_GRADLE_FILE';
final List<String> flutterLines = File(gradleFile).readAsLinesSync();
const flutterLineKey = 'minSdkVersion =';
minSdkValue = _getLineValueNumber(flutterLines, flutterLineKey);
}
return int.tryParse(minSdkValue ?? '0') ?? 0;
}
/// Retrieves the flutter sdk path
String _flutterSdk() {
final lines = File(ANDROID_LOCAL_PROPERTIES).readAsLinesSync();
const key = 'flutter.sdk=';
for (String line in lines) {
if (line.contains(key)) {
if (line.contains('//') && line.indexOf('//') < line.indexOf(key)) {
// This line is commented
continue;
}
// Remove the key and return the flutter sdk path
return line.replaceAll(key, '').trim();
}
}
return '';
}
/// Retrieves the string number only
String? _getLineValueNumber(List<String> lines, String key) {
for (String line in lines) {
if (line.contains(key)) {
if (line.contains('//') && line.indexOf('//') < line.indexOf(key)) {
// This line is commented
continue;
}
final value = line.replaceAll(RegExp(r'[^\d]'), '').trim();
if (value.isNotEmpty) {
return value;
}
}
}
return null;
}