addContentToBuildGradle static method

Future<void> addContentToBuildGradle(
  1. String projectPath
)

Implementation

static Future<void> addContentToBuildGradle(String projectPath) async {
  String? filePath;
  if (File(projectPath + _fileGradlePath).existsSync()) {
    filePath = projectPath + _fileGradlePath;
  } else {
    filePath = projectPath + _fileGradleKtsPath;
  }

  final file = File(filePath);
  String content = await file.readAsString();

  final keystorePropertiesBlock = '''
def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
  keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}
  ''';

  content = content.replaceFirst('android {', '$keystorePropertiesBlock \nandroid {');

  content = content.replaceFirst(
    'signingConfig = signingConfigs.getByName("debug")',
    'signingConfig = signingConfigs.release'
  );

  final signingConfigsBlock = '''
signingConfigs {
      release {
          keyAlias = keystoreProperties['keyAlias']
          keyPassword = keystoreProperties['keyPassword']
          storeFile = keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
          storePassword = keystoreProperties['storePassword']
      }
  }
  ''';

  content = content.replaceFirst('buildTypes {', '$signingConfigsBlock \n    buildTypes {');

  await file.writeAsString(content);
}