···11+This patch introduces an intermediate Gradle build step to alter the behavior
22+of flutter_tools' Gradle project, specifically moving the creation of `build`
33+and `.gradle` directories from within the Nix Store to somewhere in `$HOME/.cache/flutter/nix-flutter-tools-gradle/$engineShortRev`.
44+55+Without this patch, flutter_tools' Gradle project tries to generate `build` and `.gradle`
66+directories within the Nix Store. Resulting in read-only errors when trying to build a
77+Flutter Android app at runtime.
88+99+This patch takes advantage of the fact settings.gradle takes priority over settings.gradle.kts to build the intermediate Gradle project
1010+when a Flutter app runs `includeBuild("${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle")`
1111+1212+`rootProject.buildFileName = "/dev/null"` so that the intermediate project doesn't use `build.gradle.kts` that's in the same directory.
1313+1414+The intermediate project makes a `settings.gradle` file in `$HOME/.cache/flutter/nix-flutter-tools-gradle/<short engine rev>/` and `includeBuild`s it.
1515+This Gradle project will build the actual `packages/flutter_tools/gradle` project by setting
1616+`rootProject.projectDir = new File("$settingsDir")` and `apply from: new File("$settingsDir/settings.gradle.kts")`.
1717+1818+Now the `.gradle` will be built in `$HOME/.cache/flutter/nix-flutter-tools-gradle/<short engine rev>/`, but `build` doesn't.
1919+To move `build` to `$HOME/.cache/flutter/nix-flutter-tools-gradle/<short engine rev>/` as well, we need to set `buildDirectory`.
2020+diff --git a/packages/flutter_tools/gradle/settings.gradle b/packages/flutter_tools/gradle/settings.gradle
2121+new file mode 100644
2222+index 0000000000..b2485c94b4
2323+--- /dev/null
2424++++ b/packages/flutter_tools/gradle/settings.gradle
2525+@@ -0,0 +1,19 @@
2626++rootProject.buildFileName = "/dev/null"
2727++
2828++def engineShortRev = (new File("$settingsDir/../../../bin/internal/engine.version")).text.take(10)
2929++def dir = new File("$System.env.HOME/.cache/flutter/nix-flutter-tools-gradle/$engineShortRev")
3030++dir.mkdirs()
3131++def file = new File(dir, "settings.gradle")
3232++
3333++file.text = """
3434++rootProject.projectDir = new File("$settingsDir")
3535++apply from: new File("$settingsDir/settings.gradle.kts")
3636++
3737++gradle.allprojects { project ->
3838++ project.beforeEvaluate {
3939++ project.layout.buildDirectory = new File("$dir/build")
4040++ }
4141++}
4242++"""
4343++
4444++includeBuild(dir)
···11+This patch introduces an intermediate Gradle build step to alter the behavior
22+of flutter_tools' Gradle project, specifically moving the creation of `build`
33+and `.gradle` directories from within the Nix Store to somewhere in `$HOME/.cache/flutter/nix-flutter-tools-gradle/$engineShortRev`.
44+55+Without this patch, flutter_tools' Gradle project tries to generate `build` and `.gradle`
66+directories within the Nix Store. Resulting in read-only errors when trying to build a
77+Flutter Android app at runtime.
88+99+This patch takes advantage of the fact settings.gradle takes priority over settings.gradle.kts to build the intermediate Gradle project
1010+when a Flutter app runs `includeBuild("${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle")`
1111+1212+`rootProject.buildFileName = "/dev/null"` so that the intermediate project doesn't use `build.gradle.kts` that's in the same directory.
1313+1414+The intermediate project makes a `settings.gradle` file in `$HOME/.cache/flutter/nix-flutter-tools-gradle/<short engine rev>/` and `includeBuild`s it.
1515+This Gradle project will build the actual `packages/flutter_tools/gradle` project by setting
1616+`rootProject.projectDir = new File("$settingsDir")` and `apply from: new File("$settingsDir/settings.gradle.kts")`.
1717+1818+Now the `.gradle` will be built in `$HOME/.cache/flutter/nix-flutter-tools-gradle/<short engine rev>/`, but `build` doesn't.
1919+To move `build` to `$HOME/.cache/flutter/nix-flutter-tools-gradle/<short engine rev>/` as well, we need to set `buildDirectory`.
2020+diff --git a/packages/flutter_tools/gradle/settings.gradle b/packages/flutter_tools/gradle/settings.gradle
2121+new file mode 100644
2222+index 0000000000..b2485c94b4
2323+--- /dev/null
2424++++ b/packages/flutter_tools/gradle/settings.gradle
2525+@@ -0,0 +1,19 @@
2626++rootProject.buildFileName = "/dev/null"
2727++
2828++def engineShortRev = (new File("$settingsDir/../../../bin/internal/engine.version")).text.take(10)
2929++def dir = new File("$System.env.HOME/.cache/flutter/nix-flutter-tools-gradle/$engineShortRev")
3030++dir.mkdirs()
3131++def file = new File(dir, "settings.gradle")
3232++
3333++file.text = """
3434++rootProject.projectDir = new File("$settingsDir")
3535++apply from: new File("$settingsDir/settings.gradle.kts")
3636++
3737++gradle.allprojects { project ->
3838++ project.beforeEvaluate {
3939++ project.layout.buildDirectory = new File("$dir/build")
4040++ }
4141++}
4242++"""
4343++
4444++includeBuild(dir)