Merge "Check for null mGapWorker in onDetachedFromWindow"
diff --git a/.gitignore b/.gitignore
index 162af55..d697780 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
+/unbundled-build
 .classpath
 .gradle
 **/.idea/**
@@ -16,4 +17,4 @@
 **/out
 buildSrc/build
 lifecycle/common/build
-jacoco.exec
\ No newline at end of file
+jacoco.exec
diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle
index 9c0d8af..f8217a9 100644
--- a/buildSrc/build.gradle
+++ b/buildSrc/build.gradle
@@ -1,9 +1,13 @@
 buildscript {
     def supportRootFolder = project.projectDir.getParentFile()
+    apply from: "unbundled_check.gradle"
     repositories {
         maven {
             url "${supportRootFolder}/../../prebuilts/tools/common/m2/repository"
         }
+        if (isUnbundledBuild(supportRootFolder)) {
+            jcenter()
+        }
     }
 
     apply from: "build_dependencies.gradle"
diff --git a/buildSrc/init.gradle b/buildSrc/init.gradle
index 6b613d0..5221a73 100644
--- a/buildSrc/init.gradle
+++ b/buildSrc/init.gradle
@@ -33,6 +33,8 @@
 ext.runningInBuildServer = System.env.DIST_DIR != null && System.env.OUT_DIR != null
 
 apply from: "${supportRoot}/buildSrc/dependencies.gradle"
+apply from: "${supportRoot}/buildSrc/unbundled_check.gradle"
+
 ext.docs = [:]
 ext.docs.offline = rootProject.getProperties().containsKey("offlineDocs")
 ext.docs.dac = [
@@ -56,11 +58,53 @@
 }
 
 def getFullSdkPath() {
-    final String osName = System.getProperty("os.name").toLowerCase();
-    final boolean isMacOsX =
-            osName.contains("mac os x") || osName.contains("darwin") || osName.contains("osx");
-    final String platform = isMacOsX ? 'darwin' : 'linux'
-    return "${repos.prebuiltsRoot}/fullsdk-${platform}"
+    if (isUnbundledBuild(ext.supportRootFolder)) {
+        Properties properties = new Properties()
+        File propertiesFile = new File('local.properties')
+        if (propertiesFile.exists()) {
+            propertiesFile.withInputStream {
+                properties.load(it)
+            }
+        }
+        File location = findSdkLocation(properties, supportRootFolder)
+        return location.getAbsolutePath()
+    } else {
+        final String osName = System.getProperty("os.name").toLowerCase();
+        final boolean isMacOsX =
+                osName.contains("mac os x") || osName.contains("darwin") || osName.contains("osx");
+        final String platform = isMacOsX ? 'darwin' : 'linux'
+        return "${repos.prebuiltsRoot}/fullsdk-${platform}"
+    }
+}
+
+/**
+ * Adapted from com.android.build.gradle.internal.SdkHandler
+ */
+public static File findSdkLocation(Properties properties, File rootDir) {
+    String sdkDirProp = properties.getProperty("sdk.dir");
+    if (sdkDirProp != null) {
+        File sdk = new File(sdkDirProp);
+        if (!sdk.isAbsolute()) {
+            sdk = new File(rootDir, sdkDirProp);
+        }
+        return sdk
+    }
+
+    sdkDirProp = properties.getProperty("android.dir");
+    if (sdkDirProp != null) {
+        return new File(rootDir, sdkDirProp);
+    }
+
+    String envVar = System.getenv("ANDROID_HOME");
+    if (envVar != null) {
+        return new File(envVar);
+    }
+
+    String property = System.getProperty("android.home");
+    if (property != null) {
+        return new File(property);
+    }
+    return null;
 }
 
 def setSdkInLocalPropertiesFile() {
diff --git a/buildSrc/repos.gradle b/buildSrc/repos.gradle
index 156ec66..4a1ff20 100644
--- a/buildSrc/repos.gradle
+++ b/buildSrc/repos.gradle
@@ -14,8 +14,6 @@
  * limitations under the License.
  */
 
-import org.gradle.api.artifacts.dsl.RepositoryHandler;
-
 String getFullSdkPath(String prebuiltsRoot) {
     final String osName = System.getProperty("os.name").toLowerCase()
     final boolean isMacOsX =
@@ -30,7 +28,9 @@
             " including this script")
 }
 
-def checkoutRoot = "${supportRoot}/../.."
+apply from: "${supportRoot}/buildSrc/unbundled_check.gradle"
+
+def checkoutRoot = "${ext.supportRootFolder}/../.."
 ext.repos = new Properties()
 ext.repos.prebuiltsRoot = "${checkoutRoot}/prebuilts"
 ext.repos.prebuiltsRootUri = "file://${repos.prebuiltsRoot}"
@@ -50,10 +50,13 @@
             url repo
         }
     }
-    if (System.getenv("ALLOW_PUBLIC_REPOS") != null) {
+    if (System.getenv("ALLOW_PUBLIC_REPOS") != null || (isUnbundledBuild(ext.supportRootFolder))) {
         handler.mavenCentral()
         handler.jcenter()
         handler.google()
+        handler.maven {
+            url "https://plugins.gradle.org/m2/"
+        }
     }
     def androidPluginRepoOverride = System.getenv("GRADLE_PLUGIN_REPO")
     if (androidPluginRepoOverride != null) {
diff --git a/buildSrc/unbundled_check.gradle b/buildSrc/unbundled_check.gradle
new file mode 100644
index 0000000..b80daf7
--- /dev/null
+++ b/buildSrc/unbundled_check.gradle
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2018 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.
+ */
+
+public boolean isUnbundledBuild(File rootDirectory) {
+    return (new File(rootDirectory, "unbundled-build")).exists();
+}
+
+rootProject.ext['isUnbundledBuild'] = this.&isUnbundledBuild
\ No newline at end of file
diff --git a/development/unbundled-build b/development/unbundled-build
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/development/unbundled-build
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
index 96e6dab..e355705 100644
--- a/gradle/wrapper/gradle-wrapper.properties
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -3,4 +3,4 @@
 distributionPath=wrapper/dists
 zipStoreBase=GRADLE_USER_HOME
 zipStorePath=wrapper/dists
-distributionUrl=../../../../tools/external/gradle/gradle-4.3-bin.zip
+distributionUrl=../../../../tools/external/gradle/gradle-4.4-bin.zip