flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
166.13k stars 27.47k forks source link

Flutter tool should fail gracefully when neither build.gradle and build.gradle.kts exist #141180

Open bartekpacia opened 10 months ago

bartekpacia commented 10 months ago

Summary

Issue https://github.com/flutter/flutter/issues/140548 requests that Flutter tool start supporting Gradle KTS (that is settings.gradle.kts and build.gradle.kts files), in addition to settings.gradle and build.gradle which use Groovy.

PR https://github.com/flutter/flutter/pull/140744 implements this feature, but does not implement all checks, such as failing when e.g. neither build.gradle.kts and build.gradle exist.

We didn't do that in PR #140744 because we wanted to keep it lean, and that change caused test failures that would result in many out of scope changes.

Changes to be made

Example of code that should be added to packages/flutter_tools/lib/src/project.dart:

/// Gets top-level Gradle build file.
/// See https://developer.android.com/build#top-level.
///
/// The file must exist and it must be written in either Groovy (build.gradle)
/// or Kotlin (build.gradle.kts).
File get hostAppGradleFile {
  final File buildGroovy = hostAppGradleRoot.childFile('build.gradle');
  final File buildKotlin = hostAppGradleRoot.childFile('build.gradle.kts');

  if (buildGroovy.existsSync() && buildKotlin.existsSync()) {
    throwToolExit('Both build.gradle and build.gradle.kts exist. Only once can be used.');
  }

  if (buildKotlin.existsSync()) {
    return buildKotlin;
  }

  if (buildGroovy.existsSync()) {
    return buildGroovy;
  }

  throwToolExit("Neither build.gradle nor build.gradle.kts exist. This is an error");
}
huycozy commented 9 months ago

From primary triage: I think this issue is suited to team tool so I'm adding it back. Please update if it's not correct.