extractAndroidPackageName function
Extract Android package name from build.gradle or build.gradle.kts
Implementation
Future<String?> extractAndroidPackageName() async {
// Try Kotlin DSL first (newer Flutter versions)
File buildGradleFile = File('android/app/build.gradle.kts');
bool isKotlinDsl = true;
if (!buildGradleFile.existsSync()) {
// Fallback to Groovy (older Flutter versions)
buildGradleFile = File('android/app/build.gradle');
isKotlinDsl = false;
if (!buildGradleFile.existsSync()) {
print(
'${ColorsText.yellow}Warning: android/app/build.gradle or build.gradle.kts not found${ColorsText.reset}');
return null;
}
}
final content = await buildGradleFile.readAsString();
// Kotlin DSL uses: applicationId = "com.example.app"
// Groovy uses: applicationId "com.example.app" or applicationId 'com.example.app'
RegExp regex;
if (isKotlinDsl) {
regex = RegExp(r'applicationId\s*=\s*["' + "'" + r'](.+?)["' + "'" + r']');
} else {
regex = RegExp(r'applicationId\s+["' + "'" + r'](.+?)["' + "'" + r']');
}
final match = regex.firstMatch(content);
if (match != null && match.groupCount >= 1) {
return match.group(1);
}
print(
'${ColorsText.yellow}Warning: Could not find applicationId in ${isKotlinDsl ? 'build.gradle.kts' : 'build.gradle'}${ColorsText.reset}');
return null;
}