Use the internal plugins and the cloned artifacts.

Change-Id: Id5e15ea3631225132dd4df3a74cba01b39172a29
diff --git a/.gitignore b/.gitignore
index 06efd9b..505f40b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,7 +5,6 @@
 *.iws
 .gradle
 /build
-gradle/build
 tests/*/build
 tests/api/*/build
 tests/applibtest/*/build
diff --git a/build.gradle b/build.gradle
index 6819af7..5197b98 100644
--- a/build.gradle
+++ b/build.gradle
@@ -1,4 +1,20 @@
+buildscript {
+    repositories {
+        maven { url "$rootDir/../../prebuilts/tools/common/gradle-plugins/repository" }
+    }
+    dependencies {
+        classpath 'com.android.tools.internal:internal-plugins:1.0'
+    }
+}
+
 apply plugin: 'idea'
+apply plugin: 'clone-artifacts'
+
+// artifact cloning destinations
+cloneArtifacts {
+    mainRepo = "$rootDir/../../prebuilts/tools/common/m2/repository"
+    secondaryRepo = "$rootDir/../../prebuilts/tools/common/m2/internal"
+}
 
 // ext.androidHostOut is shared by all tools/{base,build,swt} gradle projects/
 ext.androidHostOut = file("$rootDir/../../out/host/gradle")
@@ -14,10 +30,24 @@
     apply plugin: 'findbugs'
 
     repositories {
-        mavenCentral()
+        // repository containing the cloned 3rd party artifacts.
+        maven { url = uri(rootProject.cloneArtifacts.mainRepo) }
+        maven { url = uri(rootProject.cloneArtifacts.secondaryRepo) }
+
+        // repository with the artifacts from tools/base
         maven { url = uri("$rootProject.ext.androidHostOut/repo") }
     }
 
+    // find bug dependencies is added dynamically so it's hard for the
+    // clone artifact plugin to find it. This custom config lets us manually
+    // add such dependencies.
+    configurations {
+        hidden
+    }
+    dependencies {
+        hidden "com.google.code.findbugs:findbugs:2.0.1"
+    }
+
     project.ext {
         baseVersion = '0.4'
     }
diff --git a/builder-model/build.gradle b/builder-model/build.gradle
index c36c645..6a13c79 100644
--- a/builder-model/build.gradle
+++ b/builder-model/build.gradle
@@ -1,6 +1,7 @@
 apply plugin: 'java'
 apply plugin: 'maven'
 apply plugin: 'signing'
+apply plugin: 'clone-artifacts'
 
 dependencies {
     compile "com.android.tools:common:$project.ext.baseAndroidVersion"
diff --git a/builder/build.gradle b/builder/build.gradle
index c01b204..7de0b1b 100644
--- a/builder/build.gradle
+++ b/builder/build.gradle
@@ -1,6 +1,7 @@
 apply plugin: 'java'
 apply plugin: 'maven'
 apply plugin: 'signing'
+apply plugin: 'clone-artifacts'
 
 evaluationDependsOn(':builder-model') 
 
diff --git a/gradle-model/build.gradle b/gradle-model/build.gradle
index 36bc575..47affba 100644
--- a/gradle-model/build.gradle
+++ b/gradle-model/build.gradle
@@ -1,16 +1,16 @@
 apply plugin: 'java'
 apply plugin: 'maven'
 apply plugin: 'signing'
-
-repositories {
-    maven {
-        url 'http://repo.gradle.org/gradle/libs-snapshots-local'
-        //url 'http://repo.gradle.org/gradle/libs-releases-local'
-    }
-}
+apply plugin: 'clone-artifacts'
 
 def toolingApiVersion = gradle.gradleVersion
 
+// Custom config that cloneArtifact will not look into, since this
+// artifact is not in mavenCentral, but in the gradle repo instead.
+configurations{
+  gradleRepo
+}
+
 dependencies {
     // should be build-model but until builder is in a repo w/o it's local dependencies, we have to
     // depend on builder itself to not have a dependency on 'common'
@@ -18,11 +18,20 @@
     compile project(':builder')
 
     testCompile 'junit:junit:3.8.1'
-    testCompile "org.gradle:gradle-tooling-api:${toolingApiVersion}"
     // Need an SLF4J implementation at runtime
     testRuntime 'org.slf4j:slf4j-simple:1.7.2'
+
+    // this is technically testCompile, but we don't want
+    // it in there to avoid breaking cloneArtifact.
+    // we'll add it to the test compile classpath manually below
+    gradleRepo "org.gradle:gradle-tooling-api:${toolingApiVersion}"
+
 }
 
+//Include custom for compilation
+sourceSets.test.compileClasspath += configurations.gradleRepo
+sourceSets.test.runtimeClasspath += configurations.gradleRepo
+
 def getVersion() {
     if (project.has("release")) {
         return project.ext.baseVersion
diff --git a/gradle/build.gradle b/gradle/build.gradle
index c112c54..3b5734b 100644
--- a/gradle/build.gradle
+++ b/gradle/build.gradle
@@ -1,6 +1,7 @@
 apply plugin: 'groovy'
 apply plugin: 'maven'
 apply plugin: 'signing'
+apply plugin: 'clone-artifacts'
 
 configurations {
     gradleApi
@@ -43,6 +44,20 @@
     deviceTestCompile configurations.testRuntime
 }
 
+// configuration for dependencies provided by the runtime,
+// in this case proguard.
+configurations{
+    provided
+}
+
+dependencies{
+    provided 'net.sf.proguard:proguard-gradle:4.9'
+}
+
+//Include provided for compilation
+sourceSets.main.compileClasspath += configurations.provided
+
+
 idea {
     module {
         testSourceDirs += files('src/build-test/groovy', 'src/device-test/groovy').files