Skip to content

Conversation

@bartekpacia
Copy link
Member

This PR resolves #140548. It's based on my work in #118067.

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I signed the CLA.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

@github-actions github-actions bot added the tool Affects the "flutter" command-line tool. See also t: labels. label Dec 29, 2023
@bartekpacia bartekpacia marked this pull request as ready for review December 29, 2023 23:10
@bartekpacia bartekpacia changed the title Start adding support for Gradle Kotlin DSL Add support for Gradle Kotlin DSL Dec 30, 2023
@github-actions github-actions bot added the d: examples Sample code and demos label Dec 30, 2023
Copy link
Contributor

@reidbaker reidbaker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this looks really good. My biggest concerns are related. One is about files being ignored currently that we start reading that could/would cause a build failure. The second is about users that are migrating to kotlin and how we can support them having some build.gradle and some build.gradle.kts code at the same time.

return buildGradle.exists() || buildGradleKts.exists()
}

private File settingsGradleFile() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider a doc on this function that says we prefer the settings.gradle.kts over settings.gradle.

Also I think this might cause an issue for customers updating since they could have a settings.gradle.kts file that is being ignored and maybe is malformed and a flutter update would cause their build to break. Let me talk this over with @christopherfujino and the android team.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the doc comment.

Re: your concern – it is possible that a situation like that might happen, but I think this would be very rare case. Of course I have no data to back it up - I'm according to my experience and "gut feeling". Like, why would some developer end up with both settings.gradle.kts and settings.gradle?

Personally I wouldn't care about it (with my current state of knowledge).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry but we have competing guts, I think it is likely to happen for customers who are mid migration and they wont realize that there are different behaviors. Unlike build.gradle and build.gradle.kts I dont think settings.gradle and settings.gradle.kts are mutually exclusive.

Either way our code should be clear on what we expect.

Copy link
Member Author

@bartekpacia bartekpacia Jan 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I get you. Thanks for competing with my gut.

I tested Gradle's behavior when both settings.gradle.kts and settings.gradle exist:

build.gradle.kts

rootProject.name = "gradle-example-kts"
logger.quiet("Hello, the project name is: ${rootProject.name}")

build.gradle

rootProject.name = "gradle-example"
logger.quiet("Hello, the project name is: ${rootProject.name}")

Gradle completely ignores settings.gradle.kts:

$ gradle projects
Hello, the project name is: gradle-example

> Task :projects

------------------------------------------------------------
Root project 'gradle-example'
------------------------------------------------------------

Root project 'gradle-example'
No sub-projects

To see a list of the tasks of a project, run gradle <project-path>:tasks
For example, try running gradle :tasks

BUILD SUCCESSFUL in 391ms
1 actionable task: 1 executed

So I think we should follow Gradle's behavior (use Groovy even when Kotlin exists). I'd also print a warning (using logger.error()) since this seems unintuitive to me).

Tested on Gradle 8.5

Copy link
Member Author

@bartekpacia bartekpacia Jan 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same thing happens for build.gradle.kts and build.gradle:

build.gradle.kts

tasks.register("hello-kotlin") {
    group = "fun"
    description = "Says hello from Kotlin"
    doLast {
        logger.quiet("Hello from Gradle KTS")
    }
}

build.gradle

tasks.register("hello-groovy") {
    group = "fun"
    description = "Says hello from Groovy"
    doLast {
        logger.quiet("Hello from Gradle Groovy")
    }
}

Running (some irrelevant output omitted):

$ gradle tasks
Fun tasks
---------
hello-groovy - Says hello from Groovy

Tested on Gradle 8.5

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for testing, It does make sense that we would try to mirror gradles default behavior and I do think this is good evidence that we DO NOT prefer the kotlin file over the groovy file. I do think that a hard failure could still make sense but I think I have asked you to churn on this enough and we can change the behavior later if we made the wrong call.

/// the project's Android directory.
String getGradleVersionForAndroidPlugin(Directory directory, Logger logger) {
final File buildFile = directory.childFile('build.gradle');
const String buildFileName = 'build.gradle/build.gradle.kts';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think either this variable should be removed and the logger strings below should have this inline or the logger strings should stay the same using the file object and rely on the if statement below to use set buildfile correctly

Copy link
Member Author

@bartekpacia bartekpacia Jan 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went for a variable because that string occured in 3 logger.printTrace() statements and remembered rule-of-3.

I'll go with this:

the logger strings should stay the same using the file object and rely on the if statement below to use set buildfile correctly

I would also add logger.error() so that users who (for whatever weird reason) don't have a build.gradle/build.gradle.kts file will see red text when flutter building. Should this case even be supported? I doubt flutter app build works without these buildfiles.

Wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote some of this code originally and I it is more likely that we have a bug in our code than that the users code does not match the conditions below.

Specifically the regular expressions assume a groovy style syntax. Can you file an issue and put it in a comment here that this code needs to be updated with regular expressions and tests that cover the kotlin case in addition to groovy?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I filed #141410 can you add a reference to that bug in this method?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you mean specifically the _buildAndroidGradlePluginRegExp RegExp that is used in this method (getGradleVersionForAndroidPlugin()), then it works in both Groovy and Kotlin.

File get appGradleFile => hostAppGradleRoot.childDirectory('app')
.childFile('build.gradle');
///
/// It can be written in Groovy (build.gradle) or Kotlin (build.gradle.kts).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include in the dart doc the behavior if both build.gradle and build.gradle.kts are present.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in d30b4da

// found in the LICENSE file.

// This file is currently NOT auto generated.
// DO NOT update it by running by dev/tools/bin/generate_gradle_lockfiles.dart.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a confusing comment. running that script won't update this anyway, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or will it, and is the user supposed to run that script then revert the change to this file? If so, I would recommend we update the script instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or will it, and is the user supposed to run that script then revert the change to this file?

That's right.

I created #140115 because changing the generate_gradle_lockfiles.dart script and running it is a quite massive change (see that PR's "lines changed"). That's why this comment exists.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in that case lets make the comment something closer to "This file is auto updated by dev/tools/bin/generate_gradle_lockfiles.dart, do not merge changes to this file. See #140115"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 90c07d3

@reidbaker
Copy link
Contributor

I talked to the android team and here is what they said

I would want an error to be raised that we don't know whether or not build.gradle or build.gradle.kts should be used and for the user to be prompted to specify the specific file that should be used on the command line

Alternatively, a giant warning being printed with a clear indication of which file we are using and how the user could override that behaviour if desired

I am trying to find documentation for what gradle does if you have both build.gradle and build.gradle.kts. I believe it errors. If I cant find documentation then I will just try it and update here.

I think we should lean towards mirroring the behavior of gradle, The flutter android team thinks we should stop and error out or have a configuration option and default behavior (which I understand is significantly more work to land the pr).

@bartekpacia
Copy link
Member Author

I would want an error to be raised that we don't know whether or not build.gradle or build.gradle.kts should be used and for the user to be prompted to specify the specific file that should be used on the command line

I agree. I think that doing sth like logger.error("duplicate gradle files detected") when Gradle file exists in both Kotlin and Groovy is enough for this PR. I can create an issue if we decide to tack on that problem later on.

Copy link
Contributor

@reidbaker reidbaker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of more comments but this is close to being ready to land.

// found in the LICENSE file.

// This file is currently NOT auto generated.
// DO NOT update it by running by dev/tools/bin/generate_gradle_lockfiles.dart.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in that case lets make the comment something closer to "This file is auto updated by dev/tools/bin/generate_gradle_lockfiles.dart, do not merge changes to this file. See #140115"

/// the project's Android directory.
String getGradleVersionForAndroidPlugin(Directory directory, Logger logger) {
final File buildFile = directory.childFile('build.gradle');
const String buildFileName = 'build.gradle/build.gradle.kts';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote some of this code originally and I it is more likely that we have a bug in our code than that the users code does not match the conditions below.

Specifically the regular expressions assume a groovy style syntax. Can you file an issue and put it in a comment here that this code needs to be updated with regular expressions and tests that cover the kotlin case in addition to groovy?

final File buildGroovy = hostAppGradleRoot.childFile('build.gradle');
final File buildKotlin = hostAppGradleRoot.childFile('build.gradle.kts');

if (buildGroovy.existsSync() && buildKotlin.existsSync()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@johnmccutchan @christopherfujino FYI new tool exit when a user has both kotlin and gradle build files.

Copy link
Member Author

@bartekpacia bartekpacia Jan 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, not anymore (see 99929d7). Too many test failures resulted that would be out of scope of this PR. I'll file an issue to keep track of it and link to that issue from the code comment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on that link and the code left in this pr my comment is still true. You removed the tool exit when neither was present. Which, if it cause test failures, I can understand removing. However we should not get test failures by enforcing that both build.gradle and build.gradle.kts should not exist at the same time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I misunderstood your comment.

I agree with everything you said in the above comment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I think I am still misunderstanding your comment. You are saying that after this change we DO tool exit if the user has both? Or DO NOT? (This line looks like we're silently preferring groovy.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made some changes after I wrote the comment above. Sorry again for the confusion!

Currently, we DO NOT crash/exit the tool when both build.gradle and build.gradle.kts exist. Instead, we use build.gradle and ignore build.gradle.kts (and the same goes for settings.gradle). This is consistent with Gradle's behavior.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify, when I said

I think I am still misunderstanding your comment

I was referring to @reidbaker's first comment.

@reidbaker
Copy link
Contributor

I would want an error to be raised that we don't know whether or not build.gradle or build.gradle.kts should be used and for the user to be prompted to specify the specific file that should be used on the command line

I agree. I think that doing sth like logger.error("duplicate gradle files detected") when Gradle file exists in both Kotlin and Groovy is enough for this PR. I can create an issue if we decide to tack on that problem later on.

That sounds reasonable to me.

@bartekpacia bartekpacia added the autosubmit Merge PR when tree becomes green via auto submit App label Jan 11, 2024
@auto-submit auto-submit bot merged commit 0a1af8a into flutter:master Jan 12, 2024
@bartekpacia bartekpacia deleted the feature/kotlin_gradle_kts branch January 12, 2024 02:20
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jan 12, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jan 12, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jan 12, 2024
auto-submit bot pushed a commit to flutter/packages that referenced this pull request Jan 12, 2024
flutter/flutter@9f2e681...7dc856a

2024-01-12 [email protected] Revert "Reverts "Run iOS staging tests with Xcode 15.2"" (flutter/flutter#141420)
2024-01-12 [email protected] Roll Packages from 0744fe6 to d74d687 (5 revisions) (flutter/flutter#141449)
2024-01-12 [email protected] Fix `FlexibleSpaceBar` centered title position and title color (flutter/flutter#140883)
2024-01-12 [email protected] Do not reset framework checkout before running customer tests (flutter/flutter#141013)
2024-01-12 [email protected] Increase delay for checking integration_ui_keyboard_resize test success (flutter/flutter#141301)
2024-01-12 [email protected] Add osx_sdk context for mac builds. (flutter/flutter#141422)
2024-01-12 [email protected] Roll Flutter Engine from ecdaed76f284 to 44a0a6ee4d39 (18 revisions) (flutter/flutter#141432)
2024-01-12 [email protected] Add support for Gradle Kotlin DSL (flutter/flutter#140744)
2024-01-12 [email protected] Fix typo (flutter/flutter#141426)
2024-01-11 98614782+auto-submit[bot]@users.noreply.github.com Reverts "Run iOS staging tests with Xcode 15.2" (flutter/flutter#141412)
2024-01-11 [email protected] Run iOS staging tests with Xcode 15.2 (flutter/flutter#141392)
2024-01-11 [email protected] Fix `ListWheelScrollView` in an `AnimatedContainer` with zero height throw an error (flutter/flutter#141372)
2024-01-11 [email protected] make asset_test.dart tests not dependent on context (flutter/flutter#141331)
2024-01-11 [email protected] Expose 'enable' property to allow the user to disable the SearchBar (flutter/flutter#137388)
2024-01-11 [email protected] Add impeller key to skia gold client, Turn on a framework test shard that will run unit tests with --enable-impeller (flutter/flutter#141341)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC [email protected],[email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
camsim99 added a commit to camsim99/flutter that referenced this pull request Jan 25, 2024
auto-submit bot pushed a commit that referenced this pull request Jan 29, 2024
…142464)

This reverts commit f5ac225, i.e. #137115.

This is a continuation of #142266 that was redone based on feedback to make this easier to revert in the future. The exact steps I took to create this revert:

1. Revert commit noted above
2. Fix merge conflicts, that notably involved reverting some changes in #140744 ~and #141417 (fixed my merge to avoid the second PR from being affected)
3. Delete `packages/flutter_tools/test/integration.shard/android_plugin_skip_unsupported_test.dart` as this was added in the commit noted above

cc @Gustl22 since I couldn't tag as a reviewer
camsim99 added a commit to camsim99/flutter that referenced this pull request Jan 29, 2024
)" (flutter#142464)

This reverts commit f5ac225, i.e. flutter#137115.

This is a continuation of flutter#142266 that was redone based on feedback to make this easier to revert in the future. The exact steps I took to create this revert:

1. Revert commit noted above
2. Fix merge conflicts, that notably involved reverting some changes in flutter#140744 ~and flutter#141417 (fixed my merge to avoid the second PR from being affected)
3. Delete `packages/flutter_tools/test/integration.shard/android_plugin_skip_unsupported_test.dart` as this was added in the commit noted above

cc @Gustl22 since I couldn't tag as a reviewer
@bartekpacia bartekpacia restored the feature/kotlin_gradle_kts branch February 1, 2024 21:35
@bartekpacia bartekpacia deleted the feature/kotlin_gradle_kts branch February 1, 2024 21:43
auto-submit bot pushed a commit that referenced this pull request Feb 2, 2024
This PR attempts to:
- reland #140744
- reland #141541 (which is also in #142300 - I will close it once this PR is merged)
dumazy added a commit to dumazy/flutter that referenced this pull request Feb 7, 2024
* master: (45 commits)
  Reverts "Update gradle lockfiles template" (flutter#142889)
  Update gradle lockfiles template (flutter#140115)
  Roll Flutter Engine from 20742e37e54e to f34c658b9600 (1 revision) (flutter#142876)
  Roll Flutter Engine from 23763db72272 to 20742e37e54e (1 revision) (flutter#142850)
  Roll Flutter Engine from fee02145da8c to 23763db72272 (3 revisions) (flutter#142848)
  Roll Flutter Engine from 9869d47a2736 to fee02145da8c (2 revisions) (flutter#142847)
  Roll Flutter Engine from 78c63d3c2c68 to 9869d47a2736 (1 revision) (flutter#142842)
  Roll Flutter Engine from 266d5d0b5588 to 78c63d3c2c68 (1 revision) (flutter#142836)
  Bump github/codeql-action from 3.23.2 to 3.24.0 (flutter#142839)
  Bump codecov/codecov-action from 3.1.6 to 4.0.1 (flutter#142838)
  Update TextSelectionOverlay (flutter#142463)
  Roll Flutter Engine from e29263212bfd to 266d5d0b5588 (5 revisions) (flutter#142832)
  Fix CupertinoTextSelectionToolbar clipping (flutter#138195)
  Reland "Add support for Gradle Kotlin DSL (flutter#140744)" (flutter#142752)
  Support navigation during a Cupertino back gesture (flutter#142248)
  Avoid depending on files from build_system/targets other than from top level entrypoints in flutter_tools. (flutter#142760)
  Roll Packages from 5b48c44 to d37fb0a (14 revisions) (flutter#142812)
  Add a link the different possible Android virtual device configs (flutter#142765)
  Allow all iOS tests to use either iOS 16 or 17 (flutter#142714)
  Roll Flutter Engine from b35153d00b2e to e29263212bfd (2 revisions) (flutter#142799)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App d: examples Sample code and demos tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support build.gradle.kts in addition to build.gradle

4 participants