···1+This patch introduces an intermediate Gradle build step to alter the behavior
2+of flutter_tools' Gradle project, specifically moving the creation of `build`
3+and `.gradle` directories from within the Nix Store to somewhere in `$HOME/.cache/flutter/nix-flutter-tools-gradle/$engineShortRev`.
4+5+Without this patch, flutter_tools' Gradle project tries to generate `build` and `.gradle`
6+directories within the Nix Store. Resulting in read-only errors when trying to build a
7+Flutter Android app at runtime.
8+9+This patch takes advantage of the fact settings.gradle takes priority over settings.gradle.kts to build the intermediate Gradle project
10+when a Flutter app runs `includeBuild("${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle")`
11+12+`rootProject.buildFileName = "/dev/null"` so that the intermediate project doesn't use `build.gradle.kts` that's in the same directory.
13+14+The intermediate project makes a `settings.gradle` file in `$HOME/.cache/flutter/nix-flutter-tools-gradle/<short engine rev>/` and `includeBuild`s it.
15+This Gradle project will build the actual `packages/flutter_tools/gradle` project by setting
16+`rootProject.projectDir = new File("$settingsDir")` and `apply from: new File("$settingsDir/settings.gradle.kts")`.
17+18+Now the `.gradle` will be built in `$HOME/.cache/flutter/nix-flutter-tools-gradle/<short engine rev>/`, but `build` doesn't.
19+To move `build` to `$HOME/.cache/flutter/nix-flutter-tools-gradle/<short engine rev>/` as well, we need to set `buildDirectory`.
20+diff --git a/packages/flutter_tools/gradle/settings.gradle b/packages/flutter_tools/gradle/settings.gradle
21+new file mode 100644
22+index 0000000000..b2485c94b4
23+--- /dev/null
24++++ b/packages/flutter_tools/gradle/settings.gradle
25+@@ -0,0 +1,19 @@
26++rootProject.buildFileName = "/dev/null"
27++
28++def engineShortRev = (new File("$settingsDir/../../../bin/internal/engine.version")).text.take(10)
29++def dir = new File("$System.env.HOME/.cache/flutter/nix-flutter-tools-gradle/$engineShortRev")
30++dir.mkdirs()
31++def file = new File(dir, "settings.gradle")
32++
33++file.text = """
34++rootProject.projectDir = new File("$settingsDir")
35++apply from: new File("$settingsDir/settings.gradle.kts")
36++
37++gradle.allprojects { project ->
38++ project.beforeEvaluate {
39++ project.layout.buildDirectory = new File("$dir/build")
40++ }
41++}
42++"""
43++
44++includeBuild(dir)
···1+This patch introduces an intermediate Gradle build step to alter the behavior
2+of flutter_tools' Gradle project, specifically moving the creation of `build`
3+and `.gradle` directories from within the Nix Store to somewhere in `$HOME/.cache/flutter/nix-flutter-tools-gradle/$engineShortRev`.
4+5+Without this patch, flutter_tools' Gradle project tries to generate `build` and `.gradle`
6+directories within the Nix Store. Resulting in read-only errors when trying to build a
7+Flutter Android app at runtime.
8+9+This patch takes advantage of the fact settings.gradle takes priority over settings.gradle.kts to build the intermediate Gradle project
10+when a Flutter app runs `includeBuild("${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle")`
11+12+`rootProject.buildFileName = "/dev/null"` so that the intermediate project doesn't use `build.gradle.kts` that's in the same directory.
13+14+The intermediate project makes a `settings.gradle` file in `$HOME/.cache/flutter/nix-flutter-tools-gradle/<short engine rev>/` and `includeBuild`s it.
15+This Gradle project will build the actual `packages/flutter_tools/gradle` project by setting
16+`rootProject.projectDir = new File("$settingsDir")` and `apply from: new File("$settingsDir/settings.gradle.kts")`.
17+18+Now the `.gradle` will be built in `$HOME/.cache/flutter/nix-flutter-tools-gradle/<short engine rev>/`, but `build` doesn't.
19+To move `build` to `$HOME/.cache/flutter/nix-flutter-tools-gradle/<short engine rev>/` as well, we need to set `buildDirectory`.
20+diff --git a/packages/flutter_tools/gradle/settings.gradle b/packages/flutter_tools/gradle/settings.gradle
21+new file mode 100644
22+index 0000000000..b2485c94b4
23+--- /dev/null
24++++ b/packages/flutter_tools/gradle/settings.gradle
25+@@ -0,0 +1,19 @@
26++rootProject.buildFileName = "/dev/null"
27++
28++def engineShortRev = (new File("$settingsDir/../../../bin/internal/engine.version")).text.take(10)
29++def dir = new File("$System.env.HOME/.cache/flutter/nix-flutter-tools-gradle/$engineShortRev")
30++dir.mkdirs()
31++def file = new File(dir, "settings.gradle")
32++
33++file.text = """
34++rootProject.projectDir = new File("$settingsDir")
35++apply from: new File("$settingsDir/settings.gradle.kts")
36++
37++gradle.allprojects { project ->
38++ project.beforeEvaluate {
39++ project.layout.buildDirectory = new File("$dir/build")
40++ }
41++}
42++"""
43++
44++includeBuild(dir)