Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support host android apps with kts gradle files for add to app #156502

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 22 commits into from
Oct 16, 2024

Conversation

gmackall
Copy link
Member

@gmackall gmackall commented Oct 9, 2024

Allows applying of include_flutter.groovy via the apply from: syntax, which allows using a host app that is using the Gradle Kotlin DSL (the default these days when creating an Android app in AS).

Explanation: The include_flutter.groovy script is currently not able to be called by Kotlin gradle files, because it is intended to be invoked with the following lines:

setBinding(new Binding([gradle: this]))                                // new
evaluate(new File(                                                     // new
    settingsDir.parentFile,                                            // new
    'flutter_module/.android/include_flutter.groovy'                   // new
))

setBinding isn't part of the Kotlin gradle DSL, and there isn't (that I can find) an easy Kotlin equivalent. If this binding isn't set, the reference to gradle in include_flutter.groovy is wrong, which breaks the script.

This PR modifies include_flutter.groovy to also support being invoked through the standard way of invoking a script via the Gradle Groovy/Kotlin DSLs, which is apply from: (or it's slightly different Kotlin syntax). The start of the script identifies which of the two approaches is being used by checking if the binding is set, and then initializes some variables differently depending on the case.

If we land this, I believe we should update the example Gradle files for both the kts and groovy cases to prefer the apply from syntax as I think this is the syntax most developers would be more familiar with already seeing in their Gradle files.

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@github-actions github-actions bot added the tool Affects the "flutter" command-line tool. See also t: labels. label Oct 9, 2024
@gmackall gmackall marked this pull request as ready for review October 11, 2024 22:27
@gmackall gmackall changed the title [wip] Support host apps with kts gradle files Support host apps with kts gradle files Oct 11, 2024
@flutter-dashboard

This comment was marked as outdated.

@gmackall gmackall changed the title Support host apps with kts gradle files Support host android apps with kts gradle files for add to app Oct 11, 2024
@gmackall gmackall requested review from bartekpacia and a team October 11, 2024 22:28
@gmackall
Copy link
Member Author

gmackall commented Oct 11, 2024

cc @craiglabenz No action needed, just that this is the latest on attempting to support host apps using kotlin gradle files. If I can land this it should be pretty simple to get a host app working with kotlin gradle files, just a couple line change that we can go over

Copy link
Member

@bartekpacia bartekpacia left a comment

Choose a reason for hiding this comment

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

That's a very nice change!

LGTM

@gmackall
Copy link
Member Author

converting to draft till I get the new test passing locally

@gmackall gmackall marked this pull request as draft October 14, 2024 23:31
@gmackall gmackall marked this pull request as ready for review October 15, 2024 19:36
@gmackall gmackall requested a review from bartekpacia October 15, 2024 19:36
@gmackall
Copy link
Member Author

Should be ready for review! Ended up making a number of changes for the testing:

  1. Made a new directory pure_android_host_apps within dev/integration_tests to house all the android apps we use in integration testing. I think I moved all of the existing ones there, hopefully I didn't miss any
  2. Added another host android app, created with the latest templates that Android Studio uses, and which uses the Kotlin DSL for its gradle files
  3. Mostly copied the existing test that tested we could depend on a Flutter module as an AAR in a host Android app, to add a new test that tests we can do the same with that Flutter module as source (corresponding to this option)

@gmackall
Copy link
Member Author

I also isolated the changes that needed to be made to the new host app (that uses Kotlin gradle files) in a single commit, so here this is the diff that users will need to make:
1f1e92a

Copy link
Member

@bartekpacia bartekpacia left a comment

Choose a reason for hiding this comment

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

I don't see anything wrong. LGTM

Really happy to see this change being made.

Comment on lines 9 to 34
// Set of Material typography styles to start with
val Typography = Typography(
bodyLarge = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Normal,
fontSize = 16.sp,
lineHeight = 24.sp,
letterSpacing = 0.5.sp
)
/* Other default text styles to override
titleLarge = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Normal,
fontSize = 22.sp,
lineHeight = 28.sp,
letterSpacing = 0.sp
),
labelSmall = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Medium,
fontSize = 11.sp,
lineHeight = 16.sp,
letterSpacing = 0.5.sp
)
*/
)
Copy link
Member

Choose a reason for hiding this comment

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

can be removed?

Copy link
Member Author

Choose a reason for hiding this comment

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

This was part of the output of the AS app templates, removed

Comment on lines 2 to 10
def flutterProjectRoot = null
if (!getBinding().getVariables().containsKey("gradle")) {
gradle = this
flutterProjectRoot = gradle.buildscript.getSourceFile().getParentFile().getParentFile().absolutePath
} else {
gradle = getBinding().getVariables().get("gradle")
def scriptFile = getClass().protectionDomain.codeSource.location.toURI()
flutterProjectRoot = new File(scriptFile).parentFile.parentFile.absolutePath
}
Copy link
Member

@bartekpacia bartekpacia Oct 15, 2024

Choose a reason for hiding this comment

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

I think a comment here would be nice, describing when each case happens.

basically this from OP:

setBinding isn't part of the Kotlin gradle DSL, and there isn't (that I can find) an easy Kotlin equivalent. If this binding isn't set, the reference to gradle in include_flutter.groovy is wrong, which breaks the script.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea, added!

@gmackall
Copy link
Member Author

Trying to figure out why the ktlint output is different on ci than locally - I can't seem to get it to properly exclude the pure_android_host_apps dir on CI, will land once I can get analysis passing

@@ -54,14 +54,14 @@ const String oneMajorVersionHigherJavaVersion = '23';
//
// Supported here means supported by the tooling for
// flutter analyze --suggestions and does not imply broader flutter support.
const String maxKnownAndSupportedGradleVersion = '8.7';
const String maxKnownAndSupportedGradleVersion = '8.10';
Copy link
Member

Choose a reason for hiding this comment

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

Are point releases significant here? I.e. will using 8.10.2 result in some warning?

Copy link
Member Author

Choose a reason for hiding this comment

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

Whoops! That was on the wrong branch, should be on #157032. Thanks for pointing it out!

Copy link
Member Author

@gmackall gmackall Oct 16, 2024

Choose a reason for hiding this comment

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

I think the answer to the question by the way is that yes they are significant based on the current implementation, and I'll want to figure out how we handle patch versions for that PR because I don't think the current implementation really expresses what we want it to.

I suspect that the correct way to handle them is to basically make the max known and supported 8.10.99 for example, because I don't think we will want to update that value for each patch version, and it's probably fine to just automatically assume new patch versions for a known minor version are supported.

But that is a question for that other PR!

@gmackall gmackall added the autosubmit Merge PR when tree becomes green via auto submit App label Oct 16, 2024
@auto-submit auto-submit bot merged commit d1d9954 into flutter:master Oct 16, 2024
137 checks passed
Lurchfresser pushed a commit to Lurchfresser/flutter that referenced this pull request Oct 17, 2024
…tter#156502)

Allows applying of `include_flutter.groovy` via the `apply from:` syntax, which allows using a host app that is using the Gradle Kotlin DSL (the default these days when creating an Android app in AS).

Explanation: The `include_flutter.groovy` script is currently not able to be called by Kotlin gradle files, because it is [intended to be invoked with the following lines](https://docs.flutter.dev/add-to-app/android/project-setup#depend-on-the-modules-source-code):
```
setBinding(new Binding([gradle: this]))                                // new
evaluate(new File(                                                     // new
    settingsDir.parentFile,                                            // new
    'flutter_module/.android/include_flutter.groovy'                   // new
))
```

`setBinding` isn't part of the Kotlin gradle DSL, and there isn't (that I can find) an easy Kotlin equivalent. If this binding isn't set, the reference to `gradle` in `include_flutter.groovy` is wrong, which breaks the script.

This PR modifies `include_flutter.groovy` to also support being invoked through the standard way of invoking a script via the Gradle Groovy/Kotlin DSLs, which is `apply from:` (or it's slightly different Kotlin syntax). The start of the script identifies which of the two approaches is being used by checking if the binding is set, and then initializes some variables differently depending on the case.

If we land this, I believe we should update the example Gradle files for both the `kts` and `groovy` cases to prefer the `apply from` syntax as I think this is the syntax most developers would be more familiar with already seeing in their Gradle files.
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 17, 2024
@gmackall gmackall added the cp: stable cherry pick this pull request to stable release candidate branch label Oct 30, 2024
@flutteractionsbot
Copy link

Failed to create CP due to merge conflicts.
You will need to create the PR manually. See the cherrypick wiki for more info.

gmackall added a commit to gmackall/flutter that referenced this pull request Oct 30, 2024
…tter#156502)

Allows applying of `include_flutter.groovy` via the `apply from:` syntax, which allows using a host app that is using the Gradle Kotlin DSL (the default these days when creating an Android app in AS).

Explanation: The `include_flutter.groovy` script is currently not able to be called by Kotlin gradle files, because it is [intended to be invoked with the following lines](https://docs.flutter.dev/add-to-app/android/project-setup#depend-on-the-modules-source-code):
```
setBinding(new Binding([gradle: this]))                                // new
evaluate(new File(                                                     // new
    settingsDir.parentFile,                                            // new
    'flutter_module/.android/include_flutter.groovy'                   // new
))
```

`setBinding` isn't part of the Kotlin gradle DSL, and there isn't (that I can find) an easy Kotlin equivalent. If this binding isn't set, the reference to `gradle` in `include_flutter.groovy` is wrong, which breaks the script.

This PR modifies `include_flutter.groovy` to also support being invoked through the standard way of invoking a script via the Gradle Groovy/Kotlin DSLs, which is `apply from:` (or it's slightly different Kotlin syntax). The start of the script identifies which of the two approaches is being used by checking if the binding is set, and then initializes some variables differently depending on the case.

If we land this, I believe we should update the example Gradle files for both the `kts` and `groovy` cases to prefer the `apply from` syntax as I think this is the syntax most developers would be more familiar with already seeing in their Gradle files.
@gmackall gmackall added cp: beta cherry pick this pull request to beta release candidate branch and removed cp: stable cherry pick this pull request to stable release candidate branch labels Oct 30, 2024
flutteractionsbot pushed a commit to flutteractionsbot/flutter that referenced this pull request Oct 30, 2024
…tter#156502)

Allows applying of `include_flutter.groovy` via the `apply from:` syntax, which allows using a host app that is using the Gradle Kotlin DSL (the default these days when creating an Android app in AS).

Explanation: The `include_flutter.groovy` script is currently not able to be called by Kotlin gradle files, because it is [intended to be invoked with the following lines](https://docs.flutter.dev/add-to-app/android/project-setup#depend-on-the-modules-source-code):
```
setBinding(new Binding([gradle: this]))                                // new
evaluate(new File(                                                     // new
    settingsDir.parentFile,                                            // new
    'flutter_module/.android/include_flutter.groovy'                   // new
))
```

`setBinding` isn't part of the Kotlin gradle DSL, and there isn't (that I can find) an easy Kotlin equivalent. If this binding isn't set, the reference to `gradle` in `include_flutter.groovy` is wrong, which breaks the script.

This PR modifies `include_flutter.groovy` to also support being invoked through the standard way of invoking a script via the Gradle Groovy/Kotlin DSLs, which is `apply from:` (or it's slightly different Kotlin syntax). The start of the script identifies which of the two approaches is being used by checking if the binding is set, and then initializes some variables differently depending on the case.

If we land this, I believe we should update the example Gradle files for both the `kts` and `groovy` cases to prefer the `apply from` syntax as I think this is the syntax most developers would be more familiar with already seeing in their Gradle files.
sfshaza2 pushed a commit to flutter/website that referenced this pull request Oct 31, 2024
_Description of what this PR is changing or adding, and why:_

Improves and adds documentation for launching Flutter from Kotlin and
Compose-flavored Android apps

_Issues fixed by this PR (if any):_

_PRs or commits this PR depends on (if any):_

Depends on flutter/flutter#156502. Must either use the `master` channel
or `stable` after that code reaches it.

Changes can be previewed on the staging built at these URLs:
*
[`/add-to-app/android/project-setup`](https://flutter-docs-prod--pr11345-launch-flutter-from-compose-wlgslbu8.web.app/add-to-app/android/project-setup)
*
[`/add-to-app/android/add-flutter-screen`](https://flutter-docs-prod--pr11345-launch-flutter-from-compose-wlgslbu8.web.app/add-to-app/android/add-flutter-screen)

## Presubmit checklist

- [x] This PR is marked as draft with an explanation if not meant to
land until a future stable release.
- [x] This PR doesn’t contain automatically generated corrections
(Grammarly or similar).
- [x] This PR follows the [Google Developer Documentation Style
Guidelines](https://developers.google.com/style) — for example, it
doesn’t use _i.e._ or _e.g._, and it avoids _I_ and _we_ (first person).
- [x] This PR uses [semantic line
breaks](https://github.com/dart-lang/site-shared/blob/main/doc/writing-for-dart-and-flutter-websites.md#semantic-line-breaks)
of 80 characters or fewer.

---------

Co-authored-by: Khanh Nguyen <[email protected]>
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 12, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 13, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 13, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 6, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 7, 2025
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 cp: beta cherry pick this pull request to beta release candidate branch tool Affects the "flutter" command-line tool. See also t: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants