@@ -29,8 +29,7 @@ List<AndroidIconTemplate> androidIcons = <AndroidIconTemplate>[
29
29
AndroidIconTemplate (directoryName: 'mipmap-xxxhdpi' , size: 192 ),
30
30
];
31
31
32
- void createDefaultIcons (
33
- Map <String , dynamic > flutterLauncherIconsConfig, String ? flavor) {
32
+ void createDefaultIcons (Map <String , dynamic > flutterLauncherIconsConfig, String ? flavor) {
34
33
printStatus ('Creating default icons Android' );
35
34
final String filePath = getAndroidIconPath (flutterLauncherIconsConfig);
36
35
final Image ? image = decodeImageFile (filePath);
@@ -48,51 +47,42 @@ void createDefaultIcons(
48
47
}
49
48
overwriteAndroidManifestWithNewLauncherIcon (iconName, androidManifestFile);
50
49
} else {
51
- printStatus (
52
- 'Overwriting the default Android launcher icon with a new icon' );
50
+ printStatus ('Overwriting the default Android launcher icon with a new icon' );
53
51
for (AndroidIconTemplate template in androidIcons) {
54
- overwriteExistingIcons (
55
- template, image, constants.androidFileName, flavor);
52
+ overwriteExistingIcons (template, image, constants.androidFileName, flavor);
56
53
}
57
- overwriteAndroidManifestWithNewLauncherIcon (
58
- constants.androidDefaultIconName, androidManifestFile);
54
+ overwriteAndroidManifestWithNewLauncherIcon (constants.androidDefaultIconName, androidManifestFile);
59
55
}
60
56
}
61
57
62
58
/// Ensures that the Android icon name is in the correct format
63
59
bool isAndroidIconNameCorrectFormat (String iconName) {
64
60
// assure the icon only consists of lowercase letters, numbers and underscore
65
61
if (! RegExp (r'^[a-z0-9_]+$' ).hasMatch (iconName)) {
66
- throw const InvalidAndroidIconNameException (
67
- constants.errorIncorrectIconName);
62
+ throw const InvalidAndroidIconNameException (constants.errorIncorrectIconName);
68
63
}
69
64
return true ;
70
65
}
71
66
72
- void createAdaptiveIcons (
73
- Map <String , dynamic > flutterLauncherIconsConfig, String ? flavor) {
67
+ void createAdaptiveIcons (Map <String , dynamic > flutterLauncherIconsConfig, String ? flavor) {
74
68
printStatus ('Creating adaptive icons Android' );
75
69
76
70
// Retrieve the necessary Flutter Launcher Icons configuration from the pubspec.yaml file
77
- final String backgroundConfig =
78
- flutterLauncherIconsConfig['adaptive_icon_background' ];
79
- final String foregroundImagePath =
80
- flutterLauncherIconsConfig['adaptive_icon_foreground' ];
71
+ final String backgroundConfig = flutterLauncherIconsConfig['adaptive_icon_background' ];
72
+ final String foregroundImagePath = flutterLauncherIconsConfig['adaptive_icon_foreground' ];
81
73
final Image ? foregroundImage = decodeImageFile (foregroundImagePath);
82
74
if (foregroundImage == null ) {
83
75
return ;
84
76
}
85
77
86
78
// Create adaptive icon foreground images
87
79
for (AndroidIconTemplate androidIcon in adaptiveForegroundIcons) {
88
- overwriteExistingIcons (androidIcon, foregroundImage,
89
- constants.androidAdaptiveForegroundFileName, flavor);
80
+ overwriteExistingIcons (androidIcon, foregroundImage, constants.androidAdaptiveForegroundFileName, flavor);
90
81
}
91
82
92
83
// Create adaptive icon background
93
84
if (isAdaptiveIconConfigPngFile (backgroundConfig)) {
94
- createAdaptiveBackgrounds (
95
- flutterLauncherIconsConfig, backgroundConfig, flavor);
85
+ createAdaptiveBackgrounds (flutterLauncherIconsConfig, backgroundConfig, flavor);
96
86
} else {
97
87
createAdaptiveIconMipmapXmlFile (flutterLauncherIconsConfig, flavor);
98
88
updateColorsXmlFile (backgroundConfig, flavor);
@@ -113,28 +103,22 @@ void updateColorsXmlFile(String backgroundConfig, String? flavor) {
113
103
updateColorsFile (colorsXml, backgroundConfig);
114
104
} else {
115
105
printStatus ('No colors.xml file found in your Android project' );
116
- printStatus (
117
- 'Creating colors.xml file and adding it to your Android project' );
106
+ printStatus ('Creating colors.xml file and adding it to your Android project' );
118
107
createNewColorsFile (backgroundConfig, flavor);
119
108
}
120
109
}
121
110
122
111
/// Creates the xml file required for the adaptive launcher icon
123
112
/// FILE LOCATED HERE: res/mipmap-anydpi/{icon-name-from-yaml-config}.xml
124
- void createAdaptiveIconMipmapXmlFile (
125
- Map <String , dynamic > flutterLauncherIconsConfig, String ? flavor) {
113
+ void createAdaptiveIconMipmapXmlFile (Map <String , dynamic > flutterLauncherIconsConfig, String ? flavor) {
126
114
if (isCustomAndroidFile (flutterLauncherIconsConfig)) {
127
- File (constants.androidAdaptiveXmlFolder (flavor) +
128
- getNewIconName (flutterLauncherIconsConfig) +
129
- '.xml' )
115
+ File (constants.androidAdaptiveXmlFolder (flavor) + getNewIconName (flutterLauncherIconsConfig) + '.xml' )
130
116
.create (recursive: true )
131
117
.then ((File adaptiveIcon) {
132
118
adaptiveIcon.writeAsString (xml_template.icLauncherXml);
133
119
});
134
120
} else {
135
- File (constants.androidAdaptiveXmlFolder (flavor) +
136
- constants.androidDefaultIconName +
137
- '.xml' )
121
+ File (constants.androidAdaptiveXmlFolder (flavor) + constants.androidDefaultIconName + '.xml' )
138
122
.create (recursive: true )
139
123
.then ((File adaptiveIcon) {
140
124
adaptiveIcon.writeAsString (xml_template.icLauncherXml);
@@ -143,8 +127,8 @@ void createAdaptiveIconMipmapXmlFile(
143
127
}
144
128
145
129
/// creates adaptive background using png image
146
- void createAdaptiveBackgrounds (Map < String , dynamic > yamlConfig,
147
- String adaptiveIconBackgroundImagePath, String ? flavor) {
130
+ void createAdaptiveBackgrounds (
131
+ Map < String , dynamic > yamlConfig, String adaptiveIconBackgroundImagePath, String ? flavor) {
148
132
final String filePath = adaptiveIconBackgroundImagePath;
149
133
final Image ? image = decodeImageFile (filePath);
150
134
if (image == null ) {
@@ -154,24 +138,19 @@ void createAdaptiveBackgrounds(Map<String, dynamic> yamlConfig,
154
138
// creates a png image (ic_adaptive_background.png) for the adaptive icon background in each of the locations
155
139
// it is required
156
140
for (AndroidIconTemplate androidIcon in adaptiveForegroundIcons) {
157
- saveNewImages (androidIcon, image,
158
- constants.androidAdaptiveBackgroundFileName, flavor);
141
+ saveNewImages (androidIcon, image, constants.androidAdaptiveBackgroundFileName, flavor);
159
142
}
160
143
161
144
// Creates the xml file required for the adaptive launcher icon
162
145
// FILE LOCATED HERE: res/mipmap-anydpi/{icon-name-from-yaml-config}.xml
163
146
if (isCustomAndroidFile (yamlConfig)) {
164
- File (constants.androidAdaptiveXmlFolder (flavor) +
165
- getNewIconName (yamlConfig) +
166
- '.xml' )
147
+ File (constants.androidAdaptiveXmlFolder (flavor) + getNewIconName (yamlConfig) + '.xml' )
167
148
.create (recursive: true )
168
149
.then ((File adaptiveIcon) {
169
150
adaptiveIcon.writeAsString (xml_template.icLauncherDrawableBackgroundXml);
170
151
});
171
152
} else {
172
- File (constants.androidAdaptiveXmlFolder (flavor) +
173
- constants.androidDefaultIconName +
174
- '.xml' )
153
+ File (constants.androidAdaptiveXmlFolder (flavor) + constants.androidDefaultIconName + '.xml' )
175
154
.create (recursive: true )
176
155
.then ((File adaptiveIcon) {
177
156
adaptiveIcon.writeAsString (xml_template.icLauncherDrawableBackgroundXml);
@@ -181,9 +160,7 @@ void createAdaptiveBackgrounds(Map<String, dynamic> yamlConfig,
181
160
182
161
/// Creates a colors.xml file if it was missing from android/app/src/main/res/values/colors.xml
183
162
void createNewColorsFile (String backgroundColor, String ? flavor) {
184
- File (constants.androidColorsFile (flavor))
185
- .create (recursive: true )
186
- .then ((File colorsFile) {
163
+ File (constants.androidColorsFile (flavor)).create (recursive: true ).then ((File colorsFile) {
187
164
colorsFile.writeAsString (xml_template.colorsXml).then ((File file) {
188
165
updateColorsFile (colorsFile, backgroundColor);
189
166
});
@@ -208,8 +185,7 @@ void updateColorsFile(File colorsFile, String backgroundColor) {
208
185
209
186
// Add new line if we didn't find an existing value
210
187
if (! foundExisting) {
211
- lines.insert (lines.length - 1 ,
212
- '\t <color name="ic_launcher_background">$backgroundColor </color>' );
188
+ lines.insert (lines.length - 1 , '\t <color name="ic_launcher_background">$backgroundColor </color>' );
213
189
}
214
190
215
191
colorsFile.writeAsStringSync (lines.join ('\n ' ));
@@ -239,10 +215,7 @@ void overwriteExistingIcons(
239
215
String ? flavor,
240
216
) {
241
217
final Image newFile = createResizedImage (template.size, image);
242
- File (constants.androidResFolder (flavor) +
243
- template.directoryName +
244
- '/' +
245
- filename)
218
+ File (constants.androidResFolder (flavor) + template.directoryName + '/' + filename)
246
219
.create (recursive: true )
247
220
.then ((File file) {
248
221
file.writeAsBytesSync (encodePng (newFile));
@@ -252,13 +225,9 @@ void overwriteExistingIcons(
252
225
/// Saves new launcher icons to the project, keeping the old launcher icons.
253
226
/// Note: Do not change interpolation unless you end up with better results
254
227
/// https://github.com/fluttercommunity/flutter_launcher_icons/issues/101#issuecomment-495528733
255
- void saveNewImages (AndroidIconTemplate template, Image image,
256
- String iconFilePath, String ? flavor) {
228
+ void saveNewImages (AndroidIconTemplate template, Image image, String iconFilePath, String ? flavor) {
257
229
final Image newFile = createResizedImage (template.size, image);
258
- File (constants.androidResFolder (flavor) +
259
- template.directoryName +
260
- '/' +
261
- iconFilePath)
230
+ File (constants.androidResFolder (flavor) + template.directoryName + '/' + iconFilePath)
262
231
.create (recursive: true )
263
232
.then ((File file) {
264
233
file.writeAsBytesSync (encodePng (newFile));
@@ -269,19 +238,15 @@ void saveNewImages(AndroidIconTemplate template, Image image,
269
238
/// with the new icon name (only if it has changed)
270
239
///
271
240
/// Note: default iconName = "ic_launcher"
272
- Future <void > overwriteAndroidManifestWithNewLauncherIcon (
273
- String iconName, File androidManifestFile) async {
241
+ Future <void > overwriteAndroidManifestWithNewLauncherIcon (String iconName, File androidManifestFile) async {
274
242
// we do not use `file.readAsLinesSync()` here because that always gets rid of the last empty newline
275
- final List <String > oldManifestLines =
276
- (await androidManifestFile.readAsString ()).split ('\n ' );
277
- final List <String > transformedLines =
278
- transformAndroidManifestWithNewLauncherIcon (oldManifestLines, iconName);
243
+ final List <String > oldManifestLines = (await androidManifestFile.readAsString ()).split ('\n ' );
244
+ final List <String > transformedLines = transformAndroidManifestWithNewLauncherIcon (oldManifestLines, iconName);
279
245
await androidManifestFile.writeAsString (transformedLines.join ('\n ' ));
280
246
}
281
247
282
248
/// Updates only the line containing android:icon with the specified iconName
283
- List <String > transformAndroidManifestWithNewLauncherIcon (
284
- List <String > oldManifestLines, String iconName) {
249
+ List <String > transformAndroidManifestWithNewLauncherIcon (List <String > oldManifestLines, String iconName) {
285
250
return oldManifestLines.map ((String line) {
286
251
if (line.contains ('android:icon' )) {
287
252
// Using RegExp replace the value of android:icon to point to the new icon
@@ -291,8 +256,7 @@ List<String> transformAndroidManifestWithNewLauncherIcon(
291
256
// repeat as often as wanted with no quote at start: [^"]*(\"[^"]*)*
292
257
// escaping the slash to place in string: [^"]*(\\"[^"]*)*"
293
258
// result: any string which does only include escaped quotes
294
- return line.replaceAll (RegExp (r'android:icon="[^"]*(\\"[^"]*)*"' ),
295
- 'android:icon="@mipmap/$iconName "' );
259
+ return line.replaceAll (RegExp (r'android:icon="[^"]*(\\"[^"]*)*"' ), 'android:icon="@mipmap/$iconName "' );
296
260
} else {
297
261
return line;
298
262
}
0 commit comments