Skip to content

Add support for Gradle Kotlin DSL #140744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Jan 12, 2024

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 force-pushed the feature/kotlin_gradle_kts branch from 0259f18 to 41f3efc Compare December 29, 2023 23:11
@bartekpacia bartekpacia changed the title Start adding support for Gradle Kotlin DSL Add support for Gradle Kotlin DSL Dec 30, 2023
@bartekpacia bartekpacia force-pushed the feature/kotlin_gradle_kts branch from ced7840 to 9ee9337 Compare December 30, 2023 14:41
@github-actions github-actions bot added the d: examples Sample code and demos label Dec 30, 2023
@bartekpacia bartekpacia force-pushed the feature/kotlin_gradle_kts branch from 9ee9337 to 2e1a505 Compare December 30, 2023 14:52
@bartekpacia bartekpacia force-pushed the feature/kotlin_gradle_kts branch from 2e1a505 to 7e48577 Compare December 30, 2023 14:52
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.

@@ -204,20 +204,28 @@ distributionUrl=https\\://services.gradle.org/distributions/gradle-$gradleVersio
/// The Android plugin version is specified in the [build.gradle] file within
/// 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

@bartekpacia bartekpacia force-pushed the feature/kotlin_gradle_kts branch from 32ff639 to 150f8cf Compare January 8, 2024 16:09
// 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.

@bartekpacia bartekpacia force-pushed the feature/kotlin_gradle_kts branch from f249659 to 6b83e98 Compare January 8, 2024 23:13
@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 36861262+QuncCccccc@users.noreply.github.com Revert "Reverts "Run iOS staging tests with Xcode 15.2"" (flutter/flutter#141420)
2024-01-12 engine-flutter-autoroll@skia.org Roll Packages from 0744fe6 to d74d687 (5 revisions) (flutter/flutter#141449)
2024-01-12 tessertaha@gmail.com Fix `FlexibleSpaceBar` centered title position and title color (flutter/flutter#140883)
2024-01-12 whesse@google.com Do not reset framework checkout before running customer tests (flutter/flutter#141013)
2024-01-12 43054281+camsim99@users.noreply.github.com Increase delay for checking integration_ui_keyboard_resize test success (flutter/flutter#141301)
2024-01-12 godofredoc@google.com Add osx_sdk context for mac builds. (flutter/flutter#141422)
2024-01-12 engine-flutter-autoroll@skia.org Roll Flutter Engine from ecdaed76f284 to 44a0a6ee4d39 (18 revisions) (flutter/flutter#141432)
2024-01-12 barpac02@gmail.com Add support for Gradle Kotlin DSL (flutter/flutter#140744)
2024-01-12 36861262+QuncCccccc@users.noreply.github.com 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 15619084+vashworth@users.noreply.github.com Run iOS staging tests with Xcode 15.2 (flutter/flutter#141392)
2024-01-11 tessertaha@gmail.com Fix `ListWheelScrollView` in an `AnimatedContainer` with zero height throw an error (flutter/flutter#141372)
2024-01-11 andrewrkolos@gmail.com make asset_test.dart tests not dependent on context (flutter/flutter#141331)
2024-01-11 57464965+Macacoazul01@users.noreply.github.com Expose 'enable' property to allow the user to disable the SearchBar (flutter/flutter#137388)
2024-01-11 jonahwilliams@google.com 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 camillesimon@google.com,rmistry@google.com,stuartmorgan@google.com 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