Have Gradle permit SL to be an included build

Bug: 72121179
Test: ./gradlew projects
Test: make another project whose settings.gradle applies
      include-builds.gradle , and build that project too

Change-Id: I860e85560e79e02120143c86a963f3769897dac8
diff --git a/README.md b/README.md
index d34ea7e..f6cdb65 100644
--- a/README.md
+++ b/README.md
@@ -29,12 +29,19 @@
 
 If you see any warnings (red underlines) run `Build > Clean Project`.
 
-## Optional - Full Build
+## Builds
+### Full Build (Optional)
 You can do most of your work from Android Studio, however you can also build the full support library from command line:
 
     cd path/to/checkout/frameworks/support/
     ./gradlew createArchive
 
+### Building Support Library as part of your App build
+If you intend to repeatedly make changes to Support Library and to wish to see
+the results in your app, and you don't want to have to repeatedly build them as
+separate Gradle projects, you can
+[configure your app build to build Support Library too](adding-support-library-as-included-build.md)
+
 ## Running Tests
 
 ### Single Test Class or Method
diff --git a/adding-support-library-as-included-build.md b/adding-support-library-as-included-build.md
new file mode 100644
index 0000000..136929f
--- /dev/null
+++ b/adding-support-library-as-included-build.md
@@ -0,0 +1,32 @@
+# Adding the Support Library Build Within Another Build
+
+Would you like to make a change in Support Library and have it be propagated to
+your downstream Gradle build (generally an app) without having to separately
+build Support Library and then build your application?
+
+## To build Support Library as part of your existing Gradle build
+*   To add the Support Library build
+    *   Add `apply(from: '<support-lib-repo-root>/frameworks/support/include-support-library.gradle')`
+        to your settings.gradle
+        *   See [include-support-library.gradle](include-support-library.gradle)
+            for more information
+*   If your project is an Android app, also update some dependencies:
+    *   Open your local.properties file and update the value of `sdk.dir` .
+        *   It should point to `<support-lib-repo-root>/prebuilts/fullsdk-<platform>` .
+        *   For example, `~/support-library/prebuilts/fullsdk-linux` .
+    *   In your build.gradle, update any versions that refer to previous versions of
+        Support Library.
+        *   To determine the correct version, find the SDK with the highest
+            number among SDKs in the Support Library repo.
+
+                echo <support-lib-repo-root>/prebuilts/fullsdk-linux/platforms/android* | xargs -n 1 echo | sed 's/.*android-//' | tail -n 1
+
+            This should output, for example, "28"
+
+        *   Update dependency versions
+            *   For example, you may want to replace
+                `com.android.support:app-compat-v7:26.0.2` with
+                `com.android.support:app-compat-v7:28.0.2`
+        *   Update configuration given to the Android Gradle plugin
+            *   Check `compileSdkVersion` and make sure its version is correct
+
diff --git a/app-toolkit/settings.gradle b/app-toolkit/settings.gradle
index 43237a4..18176de 100644
--- a/app-toolkit/settings.gradle
+++ b/app-toolkit/settings.gradle
@@ -113,10 +113,5 @@
 // External
 //
 /////////////////////////////
-if (inAppToolkitProject) {
-    File externalRoot = new File(supportRoot, '../../external')
 
-    includeBuild new File(externalRoot, 'doclava')
-
-    includeBuild new File(externalRoot, 'jdiff')
-}
+apply(from: new File(supportRoot, 'include-composite-deps.gradle'))
diff --git a/include-composite-deps.gradle b/include-composite-deps.gradle
new file mode 100644
index 0000000..c8a795f
--- /dev/null
+++ b/include-composite-deps.gradle
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2017 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.
+ */
+
+
+// This file is part of a a workaround for https://github.com/gradle/gradle/issues/1909 to enable
+// including this Support Library build as an included build. See include-support-library.gradle
+// for usage instructions.
+
+boolean currentBuildIsRootBuild = (gradle.parent == null)
+
+// Add included builds. This only works if this is currently the root build, so this script should
+// be applied to several builds and will only enable itself when part of the root build.
+if (currentBuildIsRootBuild) {
+  String buildScriptDir = buildscript.sourceFile.parent
+  File externalRoot = new File(buildScriptDir, '../../external')
+
+  includeBuild(new File(externalRoot, 'doclava'))
+  includeBuild(new File(externalRoot, 'jdiff'))
+}
+
+
diff --git a/include-support-library.gradle b/include-support-library.gradle
new file mode 100644
index 0000000..fec8041
--- /dev/null
+++ b/include-support-library.gradle
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2017 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.
+ */
+
+// This file enables using Support Library in a Composite Build. More information about Composite
+// Builds can be found at https://docs.gradle.org/current/userguide/composite_builds.html .
+// This file is a workaround for https://github.com/gradle/gradle/issues/1909 .
+
+// Projects that want to include the build of Support Library should apply this file to their
+// settings.gradle . For example, if Support Library is at the file path ~/support-library, then
+// to include Support Library in your build, add this line to the bottom of your settings.gradle:
+//
+//   apply(from:'~/support-library/frameworks/support/include-support-library.gradle')
+
+String buildScriptDir = buildscript.sourceFile.parent
+
+// include any builds required by Support Library
+apply(from:new File(buildScriptDir, 'include-composite-deps.gradle'))
+
+// include Support Library itself
+includeBuild(buildScriptDir)
+
diff --git a/settings.gradle b/settings.gradle
index d078414..71a09e1 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -225,12 +225,9 @@
 //
 /////////////////////////////
 
+apply(from: 'include-composite-deps.gradle')
 File externalRoot = new File(rootDir, '../../external')
 
-includeBuild new File(externalRoot, 'doclava')
-
-includeBuild new File(externalRoot, 'jdiff')
-
 include ':noto-emoji-compat'
 project(':noto-emoji-compat').projectDir = new File(externalRoot, 'noto-fonts/emoji-compat')