updateBuildGradle static method
Future<void>
updateBuildGradle(
)
Implementation
static Future<void> updateBuildGradle() async {
File buildGradleFile = File(
'android/app/build.gradle'); // Update the path to your build.gradle file
String keystorePropertiesSnippet = '''
signingConfigs {
release {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
storePassword keystoreProperties['storePassword']
}
}
buildTypes {
release {
signingConfig signingConfigs.release
}
''';
try {
String contents = await buildGradleFile.readAsString();
// Find the position to replace the existing 'release' block in buildTypes
int releaseBlockStart = contents.indexOf('buildTypes {');
int releaseBlockEnd = contents.indexOf('}', releaseBlockStart);
if (releaseBlockStart != -1 && releaseBlockEnd != -1) {
String existingReleaseBlock =
contents.substring(releaseBlockStart, releaseBlockEnd + 1);
String updatedContents = contents.replaceFirst(
existingReleaseBlock, keystorePropertiesSnippet);
// Write the updated contents back to the file
await buildGradleFile.writeAsString(updatedContents);
print('Build.gradle file updated successfully.');
} else {
print(
'Error: Could not find the "buildTypes {" block in build.gradle.');
}
} catch (e) {
print('Error reading/writing build.gradle file: $e');
}
}