Create a Configure<Variant> task.
Xavier Ducrohet [Wed, 22 Aug 2012 01:05:22 +0000 (18:05 -0700)]
This task creates and configure the AndroidBuilder that will be
used in the other tasks.

Change-Id: Iedbfcd709287256394c736eacbd7a9597ce2fee0

gradle/src/main/groovy/com/android/build/gradle/AndroidPlugin.groovy
gradle/src/main/groovy/com/android/build/gradle/ConfigureVariant.groovy [new file with mode: 0644]

index cd0f01a..0aaa37d 100644 (file)
@@ -25,9 +25,7 @@ import com.android.build.gradle.internal.ProductionAppVariant
 import com.android.build.gradle.internal.TestAppVariant
 import com.android.builder.AndroidBuilder
 import com.android.builder.BuildType
-import com.android.builder.DefaultSdkParser
 import com.android.builder.ProductFlavor
-import com.android.utils.StdLogger
 import org.gradle.api.Plugin
 import org.gradle.api.Project
 import org.gradle.api.plugins.JavaBasePlugin
@@ -138,9 +136,6 @@ class AndroidPlugin implements Plugin<Project> {
         if (!sdkDir.directory) {
             throw new RuntimeException("The SDK directory '$sdkDir' specified in local.properties does not exist.")
         }
-
-        androidBuilder = new AndroidBuilder(new DefaultSdkParser(sdkDir),
-                new StdLogger(SdkLogger.Level.VERBOSE), true);
     }
 
     private void addBuildType(BuildType buildType) {
@@ -233,8 +228,16 @@ class AndroidPlugin implements Plugin<Project> {
             productFlavor.debugVariant = variant
         }
 
+        // add the base configure task
+        def configureTask = project.tasks.add("configure${variant.name}", ConfigureVariant)
+        configureTask.plugin = this
+        configureTask.mainProductFlavor = variant.productFlavor
+        configureTask.productFlavor = variant.productFlavor
+        configureTask.buildType = variant.buildType
+
         // Add a task to generate the manifest
         def generateManifestTask = project.tasks.add("generate${variant.name}Manifest", GenerateManifest)
+        generateManifestTask.dependsOn configureTask
         generateManifestTask.sourceFile = project.file('src/main/AndroidManifest.xml')
         generateManifestTask.conventionMapping.outputFile = { project.file("$project.buildDir/manifests/main/$variant.dirName/AndroidManifest.xml") }
         generateManifestTask.conventionMapping.packageName = { getMainManifest().packageName }
@@ -243,6 +246,7 @@ class AndroidPlugin implements Plugin<Project> {
 
         // Add a task to crunch resource files
         def crunchTask = project.tasks.add("crunch${variant.name}Resources", CrunchResources)
+        crunchTask.dependsOn configureTask
         crunchTask.conventionMapping.outputDir = { project.file("$project.buildDir/resources/main/$variant.dirName") }
         crunchTask.sdkDir = sdkDir
         crunchTask.conventionMapping.sourceDirectories =  {
diff --git a/gradle/src/main/groovy/com/android/build/gradle/ConfigureVariant.groovy b/gradle/src/main/groovy/com/android/build/gradle/ConfigureVariant.groovy
new file mode 100644 (file)
index 0000000..26a0259
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2012 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.
+ */
+package com.android.build.gradle
+
+import com.android.builder.AndroidBuilder
+import com.android.builder.BuildType
+import com.android.builder.DefaultSdkParser
+import com.android.builder.ProductFlavor
+import com.android.utils.StdLogger
+import org.gradle.api.DefaultTask
+import org.gradle.api.tasks.Input
+import org.gradle.api.tasks.TaskAction
+
+/**
+ */
+class ConfigureVariant extends DefaultTask {
+    AndroidPlugin plugin
+
+    @Input
+    BuildType buildType
+
+    @Input
+    ProductFlavor mainProductFlavor
+
+    @Input
+    ProductFlavor productFlavor
+
+    @TaskAction
+    void generate() {
+        plugin.androidBuilder = new AndroidBuilder(new DefaultSdkParser(sdkDir),
+                    new StdLogger(SdkLogger.Level.VERBOSE), true)
+
+        plugin.androidBuilder.setTarget(plugin.extension.target)
+        plugin.androidBuilder.setBuildVariant(mainProductFlavor, productFlavor, buildType)
+    }
+}