More refactoring to build library tests.
Xavier Ducrohet [Wed, 29 Aug 2012 02:32:07 +0000 (19:32 -0700)]
Change-Id: Ib9290f31ac9fc41bce54521ef3777079cfe6034b

gradle/src/main/groovy/com/android/build/gradle/AndroidBasePlugin.groovy
gradle/src/main/groovy/com/android/build/gradle/AndroidLibraryPlugin.groovy
gradle/src/main/groovy/com/android/build/gradle/AndroidPlugin.groovy

index 3f220cd..b425c74 100644 (file)
@@ -29,6 +29,10 @@ import org.gradle.api.logging.LogLevel
 import org.gradle.api.plugins.JavaBasePlugin
 import org.gradle.api.tasks.SourceSet
 import org.gradle.api.tasks.compile.Compile
+import org.gradle.api.Task
+import org.gradle.api.plugins.BasePlugin
+import com.android.build.gradle.internal.TestAppVariant
+import com.android.build.gradle.internal.ProductionAppVariant
 
 /**
  * Base class for all Android plugins
@@ -48,6 +52,11 @@ abstract class AndroidBasePlugin {
     protected SourceSet mainSourceSet
     protected SourceSet testSourceSet
 
+    protected Task installAllForTests
+    protected Task runAllTests
+    protected Task uninstallAll
+    protected Task assembleTest
+
     abstract String getTarget()
 
     protected void apply(Project project) {
@@ -61,6 +70,20 @@ abstract class AndroidBasePlugin {
             "Assembles all variants of all applications and secondary packages."
 
         findSdk(project)
+
+        installAllForTests = project.tasks.add("installAllForTests")
+        installAllForTests.group = "Verification"
+        installAllForTests.description = "Installs all applications needed to run tests."
+
+        runAllTests = project.tasks.add("runAllTests")
+        runAllTests.group = "Verification"
+        runAllTests.description = "Runs all tests."
+
+        uninstallAll = project.tasks.add("uninstallAll")
+        uninstallAll.description = "Uninstall all applications."
+        uninstallAll.group = "Install"
+
+        project.tasks.check.dependsOn installAllForTests, runAllTests
     }
 
     protected setDefaultConfig(ProductFlavor defaultConfig) {
@@ -250,4 +273,119 @@ abstract class AndroidBasePlugin {
         }
         variant.compileTask = compileTask
     }
+
+    protected void createTestTasks(TestAppVariant variant, ProductionAppVariant testedVariant) {
+        // Add a task to process the manifest
+        def processManifestTask = createProcessManifestTask(variant)
+
+        // Add a task to crunch resource files
+        def crunchTask = createCrunchResTask(variant)
+
+        // Add a task to create the BuildConfig class
+        def generateBuildConfigTask = createBuildConfigTask(variant, processManifestTask)
+
+        // Add a task to generate resource source files
+        def processResources = createProcessResTask(variant, processManifestTask, crunchTask)
+
+        // Add a task to compile the test application
+        createCompileTask(variant, testedVariant, processResources, generateBuildConfigTask)
+
+        Task assembleTask = addPackageTasks(variant, null, true /*isTestApk*/)
+
+        if (assembleTest != null) {
+            assembleTest.dependsOn assembleTask
+        }
+
+        def runTestsTask = project.tasks.add("Run${variant.name}Tests", RunTestsTask)
+        runTestsTask.sdkDir = sdkDir
+        runTestsTask.variant = variant
+
+        runAllTests.dependsOn runTestsTask
+    }
+
+    protected Task addPackageTasks(ApplicationVariant variant, Task assembleTask,
+                                   boolean isTestApk) {
+        // Add a dex task
+        def dexTaskName = "dex${variant.name}"
+        def dexTask = project.tasks.add(dexTaskName, Dex)
+        dexTask.dependsOn variant.compileTask
+        dexTask.plugin = this
+        dexTask.variant = variant
+        dexTask.conventionMapping.sourceFiles = { variant.runtimeClasspath }
+        dexTask.conventionMapping.outputFile = {
+            project.file(
+                    "${project.buildDir}/libs/${project.archivesBaseName}-${variant.baseName}.dex")
+        }
+        dexTask.dexOptions = extension.dexOptions
+
+        // Add a task to generate application package
+        def packageApp = project.tasks.add("package${variant.name}", PackageApplication)
+        packageApp.dependsOn variant.resourcePackage, dexTask
+        packageApp.plugin = this
+        packageApp.variant = variant
+
+        def signedApk = variant.isSigned()
+
+        def apkName = signedApk ?
+            "${project.archivesBaseName}-${variant.baseName}-unaligned.apk" :
+            "${project.archivesBaseName}-${variant.baseName}-unsigned.apk"
+
+        packageApp.conventionMapping.outputFile = {
+            project.file("$project.buildDir/apk/${apkName}")
+        }
+        packageApp.conventionMapping.resourceFile = { variant.resourcePackage.singleFile }
+        packageApp.conventionMapping.dexFile = { dexTask.outputFile }
+
+        def appTask = packageApp
+
+        if (signedApk) {
+            if (variant.zipAlign) {
+                // Add a task to zip align application package
+                def alignApp = project.tasks.add("zipalign${variant.name}", ZipAlign)
+                alignApp.dependsOn packageApp
+                alignApp.conventionMapping.inputFile = { packageApp.outputFile }
+                alignApp.conventionMapping.outputFile = {
+                    project.file(
+                            "$project.buildDir/apk/${project.archivesBaseName}-${variant.baseName}.apk")
+                }
+                alignApp.sdkDir = sdkDir
+
+                appTask = alignApp
+            }
+
+            // Add a task to install the application package
+            def installApp = project.tasks.add("install${variant.name}", InstallApplication)
+            installApp.description = "Installs the " + variant.description
+            installApp.group = "Install"
+            installApp.dependsOn appTask
+            installApp.conventionMapping.packageFile = { appTask.outputFile }
+            installApp.sdkDir = sdkDir
+
+            if (isTestApk) {
+                installAllForTests.dependsOn installApp
+            }
+        }
+
+        // Add an assemble task
+        Task returnTask = null
+        if (assembleTask == null) {
+            assembleTask = project.tasks.add("assemble${variant.name}")
+            assembleTask.description = "Assembles the " + variant.description
+            assembleTask.group = BasePlugin.BUILD_GROUP
+            returnTask = assembleTask
+        }
+        assembleTask.dependsOn appTask
+
+        // add an uninstall task
+        def uninstallApp = project.tasks.add("uninstall${variant.name}", UninstallApplication)
+        uninstallApp.description = "Uninstalls the " + variant.description
+        uninstallApp.group = AndroidBasePlugin.INSTALL_GROUP
+        uninstallApp.variant = variant
+        uninstallApp.sdkDir = sdkDir
+
+        uninstallAll.dependsOn uninstallApp
+
+        return returnTask
+    }
+
 }
index 56eeb1f..86aec9f 100644 (file)
@@ -25,6 +25,7 @@ import org.gradle.api.Project
 import org.gradle.api.tasks.Copy
 import org.gradle.api.tasks.bundling.Jar
 import org.gradle.api.tasks.bundling.Zip
+import com.android.build.gradle.internal.TestAppVariant
 
 class AndroidLibraryPlugin extends AndroidBasePlugin implements Plugin<Project> {
 
@@ -55,11 +56,12 @@ class AndroidLibraryPlugin extends AndroidBasePlugin implements Plugin<Project>
     }
 
     void createAndroidTasks() {
-        createLibraryTasks(debugBuildTypeData)
+        ProductionAppVariant testedVariant = createLibraryTasks(debugBuildTypeData)
         createLibraryTasks(releaseBuildTypeData)
+        createTestTasks(testedVariant)
     }
 
-    void createLibraryTasks(BuildTypeData buildTypeData) {
+    ProductionAppVariant createLibraryTasks(BuildTypeData buildTypeData) {
         ProductFlavorData defaultConfigData = getDefaultConfigData();
 
         def variantConfig = new VariantConfiguration(
@@ -108,6 +110,21 @@ class AndroidLibraryPlugin extends AndroidBasePlugin implements Plugin<Project>
         bundle.from(project.file("$project.buildDir/$DIR_BUNDLES/${variant.dirName}"))
 
         buildTypeData.assembleTask.dependsOn bundle
+
+        return variant
+    }
+
+    void createTestTasks(ProductionAppVariant testedVariant) {
+        ProductFlavorData defaultConfigData = getDefaultConfigData();
+
+        def testVariantConfig = new VariantConfiguration(
+                defaultConfigData.productFlavor, defaultConfigData.androidSourceSet,
+                debugBuildTypeData.buildType, null,
+                VariantConfiguration.Type.TEST)
+
+        def testVariant = new TestAppVariant(testVariantConfig, testedVariant.config)
+        createTestTasks(testVariant, testedVariant)
+
     }
 
     @Override
index a186141..586f54e 100644 (file)
@@ -15,7 +15,6 @@
  */
 package com.android.build.gradle
 
-import com.android.build.gradle.internal.ApplicationVariant
 import com.android.build.gradle.internal.BuildTypeData
 import com.android.build.gradle.internal.ProductFlavorData
 import com.android.build.gradle.internal.ProductionAppVariant
@@ -26,8 +25,8 @@ import com.android.builder.VariantConfiguration
 import org.gradle.api.Plugin
 import org.gradle.api.Project
 import org.gradle.api.Task
-import org.gradle.internal.reflect.Instantiator
 import org.gradle.api.plugins.BasePlugin
+import org.gradle.internal.reflect.Instantiator
 
 class AndroidPlugin extends AndroidBasePlugin implements Plugin<Project> {
     private final Map<String, BuildTypeData> buildTypes = [:]
@@ -35,11 +34,6 @@ class AndroidPlugin extends AndroidBasePlugin implements Plugin<Project> {
 
     private AndroidExtension extension
 
-    private Task installAllForTests
-    private Task runAllTests
-    private Task uninstallAll
-    private Task assembleTest
-
     @Override
     void apply(Project project) {
         super.apply(project)
@@ -73,20 +67,6 @@ class AndroidPlugin extends AndroidBasePlugin implements Plugin<Project> {
                     "Removing product flavors is not implemented yet.")
         }
 
-        installAllForTests = project.tasks.add("installAllForTests")
-        installAllForTests.group = "Verification"
-        installAllForTests.description = "Installs all applications needed to run tests."
-
-        runAllTests = project.tasks.add("runAllTests")
-        runAllTests.group = "Verification"
-        runAllTests.description = "Runs all tests."
-
-        uninstallAll = project.tasks.add("uninstallAll")
-        uninstallAll.description = "Uninstall all applications."
-        uninstallAll.group = "Install"
-
-        project.tasks.check.dependsOn installAllForTests, runAllTests
-
         project.afterEvaluate {
             createAndroidTasks()
         }
@@ -153,7 +133,6 @@ class AndroidPlugin extends AndroidBasePlugin implements Plugin<Project> {
             throw new RuntimeException("Test Build Type '$extension.testBuildType' does not exist.")
         }
 
-        VariantConfiguration testedVariantConfig = null
         ProductionAppVariant testedVariant = null
 
         ProductFlavorData defaultConfigData = getDefaultConfigData();
@@ -171,19 +150,19 @@ class AndroidPlugin extends AndroidBasePlugin implements Plugin<Project> {
                     buildTypeData.assembleTask, isTestedVariant)
 
             if (isTestedVariant) {
-                testedVariantConfig = variantConfig
                 testedVariant = productionAppVariant
             }
         }
 
-        assert testedVariantConfig != null && testedVariant != null
+        assert testedVariant != null
 
-        def variantTestConfig = new VariantConfiguration(
+        def testVariantConfig = new VariantConfiguration(
                 defaultConfigData.productFlavor, defaultConfigData.androidTestSourceSet,
                 testData.buildType, null,
                 VariantConfiguration.Type.TEST)
 
-        addTestVariant(variantTestConfig, testedVariantConfig, testedVariant)
+        def testVariant = new TestAppVariant(testVariantConfig, testedVariant.config)
+        createTestTasks(testVariant, testedVariant)
     }
 
     /**
@@ -198,7 +177,6 @@ class AndroidPlugin extends AndroidBasePlugin implements Plugin<Project> {
             throw new RuntimeException("Test Build Type '$extension.testBuildType' does not exist.")
         }
 
-        VariantConfiguration testedVariantConfig = null
         ProductionAppVariant testedVariant = null
 
         for (BuildTypeData buildTypeData : buildTypes.values()) {
@@ -220,21 +198,21 @@ class AndroidPlugin extends AndroidBasePlugin implements Plugin<Project> {
             productFlavorData.assembleTask.dependsOn productionAppVariant.assembleTask
 
             if (isTestedVariant) {
-                testedVariantConfig = variantConfig
                 testedVariant = productionAppVariant
             }
         }
 
-        assert testedVariantConfig != null && testedVariant != null
+        assert testedVariant != null
 
-        def variantTestConfig = new VariantConfiguration(
+        def testVariantConfig = new VariantConfiguration(
                 extension.defaultConfig, getDefaultConfigData().androidTestSourceSet,
                 testData.buildType, null,
                 VariantConfiguration.Type.TEST)
-        variantTestConfig.addProductFlavor(productFlavorData.productFlavor,
+        testVariantConfig.addProductFlavor(productFlavorData.productFlavor,
                 productFlavorData.androidTestSourceSet)
 
-        addTestVariant(variantTestConfig, testedVariantConfig, testedVariant)
+        def testVariant = new TestAppVariant(testVariantConfig, testedVariant.config)
+        createTestTasks(testVariant, testedVariant)
     }
 
     /**
@@ -273,116 +251,6 @@ class AndroidPlugin extends AndroidBasePlugin implements Plugin<Project> {
         return variant;
     }
 
-    private void addTestVariant(VariantConfiguration variantConfig,
-                                VariantConfiguration testedVariantConfig,
-                                ProductionAppVariant testedVariant) {
-
-        def variant = new TestAppVariant(variantConfig, testedVariantConfig)
-
-        // Add a task to process the manifest
-        def processManifestTask = createProcessManifestTask(variant)
-
-        // Add a task to crunch resource files
-        def crunchTask = createCrunchResTask(variant)
-
-        // Add a task to create the BuildConfig class
-        def generateBuildConfigTask = createBuildConfigTask(variant, processManifestTask)
-
-        // Add a task to generate resource source files
-        def processResources = createProcessResTask(variant, processManifestTask, crunchTask)
-
-        // Add a task to compile the test application
-        createCompileTask(variant, testedVariant, processResources, generateBuildConfigTask)
-
-        Task assembleTask = addPackageTasks(variant, null, true /*isTestApk*/)
-
-        if (assembleTest != null) {
-            assembleTest.dependsOn assembleTask
-        }
-
-        def runTestsTask = project.tasks.add("Run${variant.name}Tests", RunTestsTask)
-        runTestsTask.sdkDir = sdkDir
-        runTestsTask.variant = variant
-
-        runAllTests.dependsOn runTestsTask
-    }
-
-    private Task addPackageTasks(ApplicationVariant variant, Task assembleTask, boolean isTestApk) {
-        // Add a dex task
-        def dexTaskName = "dex${variant.name}"
-        def dexTask = project.tasks.add(dexTaskName, Dex)
-        dexTask.dependsOn variant.compileTask
-        dexTask.plugin = this
-        dexTask.variant = variant
-        dexTask.conventionMapping.sourceFiles = { variant.runtimeClasspath }
-        dexTask.conventionMapping.outputFile = { project.file("${project.buildDir}/libs/${project.archivesBaseName}-${variant.baseName}.dex") }
-        dexTask.dexOptions = extension.dexOptions
-
-        // Add a task to generate application package
-        def packageApp = project.tasks.add("package${variant.name}", PackageApplication)
-        packageApp.dependsOn variant.resourcePackage, dexTask
-        packageApp.plugin = this
-        packageApp.variant = variant
-
-        def signedApk = variant.isSigned()
-
-        def apkName = signedApk ?
-            "${project.archivesBaseName}-${variant.baseName}-unaligned.apk" :
-            "${project.archivesBaseName}-${variant.baseName}-unsigned.apk"
-
-        packageApp.conventionMapping.outputFile = { project.file("$project.buildDir/apk/${apkName}") }
-        packageApp.conventionMapping.resourceFile = { variant.resourcePackage.singleFile }
-        packageApp.conventionMapping.dexFile = { dexTask.outputFile }
-
-        def appTask = packageApp
-
-        if (signedApk) {
-            if (variant.zipAlign) {
-                // Add a task to zip align application package
-                def alignApp = project.tasks.add("zipalign${variant.name}", ZipAlign)
-                alignApp.dependsOn packageApp
-                alignApp.conventionMapping.inputFile = { packageApp.outputFile }
-                alignApp.conventionMapping.outputFile = { project.file("$project.buildDir/apk/${project.archivesBaseName}-${variant.baseName}.apk") }
-                alignApp.sdkDir = sdkDir
-
-                appTask = alignApp
-            }
-
-            // Add a task to install the application package
-            def installApp = project.tasks.add("install${variant.name}", InstallApplication)
-            installApp.description = "Installs the " + variant.description
-            installApp.group = "Install"
-            installApp.dependsOn appTask
-            installApp.conventionMapping.packageFile = { appTask.outputFile }
-            installApp.sdkDir = sdkDir
-
-            if (isTestApk) {
-                installAllForTests.dependsOn installApp
-            }
-        }
-
-        // Add an assemble task
-        Task returnTask = null
-        if (assembleTask == null) {
-            assembleTask = project.tasks.add("assemble${variant.name}")
-            assembleTask.description = "Assembles the " + variant.description
-            assembleTask.group = BasePlugin.BUILD_GROUP
-            returnTask = assembleTask
-        }
-        assembleTask.dependsOn appTask
-
-        // add an uninstall task
-        def uninstallApp = project.tasks.add("uninstall${variant.name}", UninstallApplication)
-        uninstallApp.description = "Uninstalls the " + variant.description
-        uninstallApp.group = AndroidBasePlugin.INSTALL_GROUP
-        uninstallApp.variant = variant
-        uninstallApp.sdkDir = sdkDir
-
-        uninstallAll.dependsOn uninstallApp
-
-        return returnTask
-    }
-
     @Override
     String getTarget() {
         return extension.target;