changeManifestLabelToResource method

Future<void> changeManifestLabelToResource()

Changes the label attribute in AndroidManifest.xml to use a resource.

Implementation

Future<void> changeManifestLabelToResource() async {
  final manifestFile = File(androidManifestPath);

  if (!await manifestFile.exists()) {
    logger.e('AndroidManifest.xml file not found.');
    return;
  }

  final manifestLines = await manifestFile.readAsLines();

  for (int i = 0; i < manifestLines.length; i++) {
    final line = manifestLines[i];
    if (line.contains('android:label=')) {
      // Check if the android:label attribute contains a string value.
      final labelIndex = line.indexOf('android:label=');
      final quoteStartIndex = line.indexOf('"', labelIndex);
      final quoteEndIndex = line.indexOf('"', quoteStartIndex + 1);

      if (quoteStartIndex != -1 && quoteEndIndex != -1) {
        final currentValue =
            line.substring(quoteStartIndex + 1, quoteEndIndex);
        const newValue = '@string/app_name';

        if (currentValue != newValue) {
          // Replace the android:label value with @string/name.
          manifestLines[i] = line.replaceFirst(currentValue, newValue);
          await manifestFile.writeAsString(manifestLines.join('\n'));
          logger.i('AndroidManifest.xml label updated to use @string/name');
          return;
        }
      }
    }
  }
}