1This patch introduces an intermediate Gradle build step to alter the behavior
2of flutter_tools' Gradle project, specifically moving the creation of `build`
3and `.gradle` directories from within the Nix Store to somewhere in `$HOME/.cache/flutter/nix-flutter-tools-gradle/$engineShortRev`.
4
5Without this patch, flutter_tools' Gradle project tries to generate `build` and `.gradle`
6directories within the Nix Store. Resulting in read-only errors when trying to build a
7Flutter Android app at runtime.
8
9This patch takes advantage of the fact settings.gradle takes priority over settings.gradle.kts to build the intermediate Gradle project
10when 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
14The intermediate project makes a `settings.gradle` file in `$HOME/.cache/flutter/nix-flutter-tools-gradle/<short engine rev>/` and `includeBuild`s it.
15This 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
18Now the `.gradle` will be built in `$HOME/.cache/flutter/nix-flutter-tools-gradle/<short engine rev>/`, but `build` doesn't.
19To move `build` to `$HOME/.cache/flutter/nix-flutter-tools-gradle/<short engine rev>/` as well, we need to set `buildDirectory`.
20diff --git a/packages/flutter_tools/gradle/settings.gradle b/packages/flutter_tools/gradle/settings.gradle
21new file mode 100644
22index 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)