apply static method
Edits android/app/build.gradle{.kts} in config's project to add the
product flavors. No-op (with a warning) when there are no flavors, no
gradle file (e.g. flutter create was skipped), or the block already
exists. Throws StateError if the file exists but has no android { }
block to edit.
Implementation
static void apply(ProjectConfig config) {
if (!config.hasFlavors) return;
final appDir = p.join(config.projectPath, 'android', 'app');
final kts = File(p.join(appDir, 'build.gradle.kts'));
final groovy = File(p.join(appDir, 'build.gradle'));
final File target;
final bool isKts;
if (kts.existsSync()) {
target = kts;
isKts = true;
} else if (groovy.existsSync()) {
target = groovy;
isKts = false;
} else {
Logger.warn(
'No android/app/build.gradle{.kts} found — skipping Android flavor '
'setup. Run `flutter create .` then re-run, or add flavors manually.',
);
return;
}
final original = target.readAsStringSync();
if (original.contains('productFlavors')) {
Logger.warn(
'android/app/${p.basename(target.path)} already defines '
'productFlavors — leaving it untouched.',
);
return;
}
final title = StringUtils.toTitleCase(config.projectName);
final flavors =
config.flavors.map((f) => FlavorGradle.from(f, title)).toList();
final updated = injectProductFlavors(
original,
isKts: isKts,
flavors: flavors,
);
target.writeAsStringSync(updated);
Logger.info('Added Android product flavors to ${p.basename(target.path)}');
}