setupAndroidFlavors function

Future<void> setupAndroidFlavors(
  1. String projectPath,
  2. List<String> flavors
)

Implementation

Future<void> setupAndroidFlavors(
    String projectPath, List<String> flavors) async {

  final ktsFile =
  File(p.join(projectPath, 'android', 'app', 'build.gradle.kts'));

  final groovyFile =
  File(p.join(projectPath, 'android', 'app', 'build.gradle'));

  File? gradleFile;

  if (ktsFile.existsSync()) {
    gradleFile = ktsFile;
  } else if (groovyFile.existsSync()) {
    gradleFile = groovyFile;
  } else {
    print('⚠ No Android build.gradle file found.');
    return;
  }

  var content = gradleFile.readAsStringSync();

  if (content.contains("productFlavors")) {
    print("⚠ Flavors already configured.");
  } else {

    final isKts = gradleFile.path.endsWith('.kts');

    final flavorBlock = isKts
        ? '''
    flavorDimensions += "env"

    productFlavors {
${flavors.map((f) => '''
        create("$f") {
            dimension = "env"
            ${f == "dev" ? 'applicationIdSuffix = ".dev"\n            versionNameSuffix = "-dev"' : ''}
        }
''').join()}
    }
'''
        : '''
    flavorDimensions "env"

    productFlavors {
${flavors.map((f) => '''
        $f {
            dimension "env"
            ${f == "dev" ? 'applicationIdSuffix ".dev"\n            versionNameSuffix "-dev"' : ''}
        }
''').join()}
    }
''';

    final androidIndex = content.indexOf("android {");

    if (androidIndex == -1) {
      print("⚠ Could not find android block.");
      return;
    }

    final insertIndex = content.indexOf("{", androidIndex) + 1;

    content = content.substring(0, insertIndex) +
        "\n$flavorBlock\n" +
        content.substring(insertIndex);

    gradleFile.writeAsStringSync(content);

    print("✅ Android flavors configured successfully.");
  }

//  await _injectAndroidSigning(projectPath);
}