checkSdkVersions method

Future<void> checkSdkVersions()

Check the SDK versions in the app/build.gradle

Implementation

Future<void> checkSdkVersions() async {
  final appBuildGradlePath = FileSystemUtils.findAppBuildGradle(androidDir);
  if (appBuildGradlePath == null) {
    issues.add(
      IssueModel(
        filePath: path.join(androidDir, 'app', 'build.gradle'),
        description: 'App build.gradle file not found',
        currentValue: 'Missing',
        recommendedValue: 'Create the file with proper configuration',
        isCritical: true,
      ),
    );
    return;
  }

  final content = await FileSystemUtils.readFile(appBuildGradlePath);

  // Check compileSdkVersion
  final RegExp compileSdkVersionRegex = RegExp(
    r'compileSdk(?:Version)?\s*[=:]?\s*(\d+)',
  );
  final compileSdkMatch = compileSdkVersionRegex.firstMatch(content);

  if (compileSdkMatch == null) {
    issues.add(
      IssueModel(
        filePath: appBuildGradlePath,
        description: 'compileSdkVersion not found',
        currentValue: 'Unknown',
        recommendedValue: minCompileSdkVersion.toString(),
        isCritical: true,
      ),
    );
  } else {
    final currentCompileSdk =
        int.tryParse(compileSdkMatch.group(1) ?? '0') ?? 0;
    if (currentCompileSdk < minCompileSdkVersion) {
      issues.add(
        IssueModel(
          filePath: appBuildGradlePath,
          description: 'compileSdkVersion is too low',
          currentValue: currentCompileSdk.toString(),
          recommendedValue: minCompileSdkVersion.toString(),
        ),
      );
    }
  }

  // Check minSdkVersion
  final RegExp minSdkVersionRegex = RegExp(
    r'minSdk(?:Version)?\s*[=:]?\s*(\d+)',
  );
  final minSdkMatch = minSdkVersionRegex.firstMatch(content);

  if (minSdkMatch == null) {
    issues.add(
      IssueModel(
        filePath: appBuildGradlePath,
        description: 'minSdkVersion not found',
        currentValue: 'Unknown',
        recommendedValue: minSdkVersion.toString(),
        isCritical: true,
      ),
    );
  } else {
    final currentMinSdk = int.tryParse(minSdkMatch.group(1) ?? '0') ?? 0;
    if (currentMinSdk < minSdkVersion) {
      issues.add(
        IssueModel(
          filePath: appBuildGradlePath,
          description: 'minSdkVersion is too low',
          currentValue: currentMinSdk.toString(),
          recommendedValue: minSdkVersion.toString(),
        ),
      );
    }
  }

  // Check targetSdkVersion
  final RegExp targetSdkVersionRegex = RegExp(
    r'targetSdk(?:Version)?\s*[=:]?\s*(\d+)',
  );
  final targetSdkMatch = targetSdkVersionRegex.firstMatch(content);

  if (targetSdkMatch == null) {
    issues.add(
      IssueModel(
        filePath: appBuildGradlePath,
        description: 'targetSdkVersion not found',
        currentValue: 'Unknown',
        recommendedValue: recommendedTargetSdkVersion.toString(),
        isCritical: true,
      ),
    );
  } else {
    final currentTargetSdk =
        int.tryParse(targetSdkMatch.group(1) ?? '0') ?? 0;
    if (currentTargetSdk < recommendedTargetSdkVersion) {
      issues.add(
        IssueModel(
          filePath: appBuildGradlePath,
          description: 'targetSdkVersion is outdated',
          currentValue: currentTargetSdk.toString(),
          recommendedValue: recommendedTargetSdkVersion.toString(),
        ),
      );
    }
  }

  // Check jvmTarget
  final RegExp jvmTargetRegex = RegExp('jvmTarget\\s*=\\s*["\'](.+?)["\']');
  final jvmTargetMatch = jvmTargetRegex.firstMatch(content);

  if (jvmTargetMatch == null) {
    // This might be fine if the project doesn't explicitly set jvmTarget
    issues.add(
      IssueModel(
        filePath: appBuildGradlePath,
        description: 'jvmTarget not found',
        currentValue: 'Not set',
        recommendedValue: '17', // Recommend Java 17 as a modern version
        isCritical: false,
      ),
    );
  } else {
    final currentJvmTarget = jvmTargetMatch.group(1) ?? '';
    if (_isJvmTargetOutdated(currentJvmTarget)) {
      issues.add(
        IssueModel(
          filePath: appBuildGradlePath,
          description: 'jvmTarget is outdated',
          currentValue: currentJvmTarget,
          recommendedValue: '17', // Recommend Java 17 as a modern version
        ),
      );
    }
  }
}