updateAndroidVersion static method
Updates the version in the Android build.gradle file. @returns {bool} True if the file was updated, false otherwise.
Implementation
static bool updateAndroidVersion(String versionName, String buildNumber) {
var gradleFilePath = p.join(
Directory.current.path,
'android',
'app',
'build.gradle',
);
var gradleFile = File(gradleFilePath);
if (!gradleFile.existsSync()) {
gradleFilePath = p.join(
Directory.current.path,
'android',
'app',
'build.gradle.kts',
);
gradleFile = File(gradleFilePath);
if (!gradleFile.existsSync()) {
return false; // File not found
}
}
try {
var content = gradleFile.readAsStringSync();
var originalContent = content;
// Replace versionName
final nameRegex = RegExp(r'versionName\s+"[^"]+"');
if (nameRegex.hasMatch(content)) {
content = content.replaceFirst(nameRegex, 'versionName "$versionName"');
}
// Replace versionCode
final codeRegex = RegExp(r'versionCode\s+\d+');
if (codeRegex.hasMatch(content)) {
content = content.replaceFirst(codeRegex, 'versionCode $buildNumber');
}
if (content != originalContent) {
gradleFile.writeAsStringSync(content);
return true;
}
return false; // No changes made
} on FileSystemException {
return false; // Error reading/writing file
}
}