androidPackageName property

Future<String> get androidPackageName

Implementation

static Future<String> get androidPackageName async {
  final gradleFile = File('${Constants.androidDirPath}/app/build.gradle');
  final ktsFile = File('${Constants.androidDirPath}/app/build.gradle.kts');

  File? targetFile;

  if (gradleFile.existsSync()) {
    targetFile = gradleFile;
  } else if (ktsFile.existsSync()) {
    targetFile = ktsFile;
  } else {
    throw Exception(
      'Neither build.gradle nor build.gradle.kts found in ${Constants.androidDirPath}/app',
    );
  }

  final content = await targetFile.readAsString();

  final regex = RegExp(
    r'''applicationId\s*(=)?\s*['"]([a-zA-Z0-9_.]+)['"]''',
  );
  final match = regex.firstMatch(content);

  if (match == null || match.group(2) == null) {
    throw Exception('applicationId not found in ${targetFile.path}');
  }

  return match.group(2)!;
}