referenceKeyStoreInGradle static method

Future<void> referenceKeyStoreInGradle()

Implementation

static Future<void> referenceKeyStoreInGradle() async {
  final File buildGradle = File('android/app/build.gradle');
  String keystorePropertiesSnippet = '''
def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
  keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}
''';

  try {
    String contents = await buildGradle.readAsString();

    // Find the position to insert the keystorePropertiesSnippet before 'android' block
    int androidBlockIndex = contents.indexOf('android {');
    if (androidBlockIndex != -1) {
      String updatedContents = contents.replaceRange(
          androidBlockIndex, androidBlockIndex, keystorePropertiesSnippet);

      // Write the updated contents back to the file
      await buildGradle.writeAsString(updatedContents);
      print('Build.gradle file updated successfully.');
    } else {
      print('Error: Could not find the "android {" block in build.gradle.');
    }
  } catch (e) {
    print('Error reading/writing build.gradle file: $e');
  }
}