Mostly Revert "Revert "Moving the buildDir for the buildSrc project to be in the out/ dir""

This mostly reverts commit 5bf892ecb54bfa053994e7318bdfbd5210330629 but also fixes the creation of a dir named "null" by Studio

Test: rm ../../out null -rf && ./studiow && sleep 30 && ls ../../out && ls null # and make sure 'null' does not exist
Test: OUT_DIR=/tmp/out DIST_DIR=/tmp/out/dist ./gradlew buildOnServer && ls /tmp/out/dist/top-of-tree-m2repository-partially-dejetified-dist.zip
Test: ./cleanBuild.sh assembleDebug && ls ../../out/dist
Test: git clean -fdx && ./gradlew projects && ((stat buildSrc/build && echo failed: buildSrc/build must not exist) || true)
Test: OUT_DIR=/tmp/out DIST_DIR=/tmp/out/dist ./gradlew misspelledTaskName # and see that there's a stacktrace
Test: ./gradlew misspelledTaskName # and see that there's no stacktrace

Change-Id: I0577fa2d4d961d5357badc17d936102918f9dc76
diff --git a/buildSrc/out.gradle b/buildSrc/out.gradle
new file mode 100644
index 0000000..e469144
--- /dev/null
+++ b/buildSrc/out.gradle
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+def init = new Properties()
+ext.init = init
+
+def chooseOutDir() {
+    /*
+     * The OUT_DIR is a temporary directory you can use to put things during the build.
+     */
+    def outDir = System.env.OUT_DIR
+    if (outDir == null) {
+        outDir = new File("${buildscript.getSourceFile().parent}/../../../out")
+    }
+    buildDir = new File(outDir, "$project.name/build")
+                .getCanonicalFile()
+    subprojects {
+        // Change buildDir first so that all plugins pick up the new value.
+        project.buildDir = new File("$project.parent.buildDir/../$project.name/build")
+    }
+    ext.supportRepoOut = new File(buildDir, 'support_repo')
+    ext.supportRepoOut.mkdirs()
+    ext.docsDir = new File(buildDir, 'javadoc')
+}
+
+ext.init.chooseOutDir = this.&chooseOutDir