title: Robolectric, Gradle, Espresso With Flavors
body: <p>I was using this <a href="https://github.com/nenick/android-gradle-template" rel="nofollow">template</a> to incorporate robolectric tests but it's not working because I use flavors and I get this error:</p>
  
  <pre><code>Error:Could not find property 'testRelease' on project ':Component_Tests'.
  </code></pre>
  
  <p>How can I fix this? I'm using 2 flavors and 3 build types. Here's the gradle file:</p>
  
  <pre><code>buildscript {
      dependencies {
          classpath 'com.android.tools.build:gradle:0.12.1+'
          classpath "com.neenbedankt.gradle.plugins:android-apt:1.2"
      }
  }
  
  apply plugin: 'android'
  apply plugin: 'android-apt'
  apply from : "$rootDir/gradle/signing.gradle"
  apply from : "$rootDir/gradle/build_extras.gradle"
  apply from : "$rootDir/gradle/environments.gradle"
  
  loadConfiguration()
  
  apply from : "$rootDir/gradle/checkstyle.gradle"
  apply from : "$rootDir/gradle/pmd.gradle"
  apply from : "$rootDir/gradle/findbugs.gradle"
  apply from : "$rootDir/gradle/espresso.gradle"
  
  tasks.whenTaskAdded { task -&gt;
      if (task.name.startsWith('connectedAndroidTest')) {
          task.dependsOn grantAnimationPermission
      }
  }
  
  def loadPropertiesFile = { filePath -&gt;
      def propertiesFile = file(filePath)
      def Properties properties = new Properties()
  
      if (!propertiesFile.canRead()) {
          throw new GradleException("cannot read " + filePath)
      }
  
      properties.load(new FileInputStream(propertiesFile))
  
      return properties
  }
  
  def writePropertiesFile = { filePath, properties -&gt;
      def propertiesFile = file(filePath)
  
      if (!propertiesFile.canRead()) {
          throw new GradleException("cannot read " + filePath)
      }
  
      properties.store(propertiesFile.newWriter(), null)
  }
  
  def loadAndStorePropertyFile = { filePath, propertyName, value -&gt;
      def Properties properties = loadPropertiesFile(filePath)
      properties[propertyName] = value.toString()
      writePropertiesFile(filePath, properties)
      println "$propertyName is set to $value"
  }
  
  def getVersionCode = { -&gt;
      def Properties versionProperties = loadPropertiesFile('../version.properties')
      return versionProperties['VERSION_CODE'].toInteger()
  }
  
  def getAlphaHash = { -&gt;
      def Properties versionProperties = loadPropertiesFile('../version.properties')
      return versionProperties['LAST_ALPHA_HASH'].toString()
  }
  
  def getBetaHash = { -&gt;
      def Properties versionProperties = loadPropertiesFile('../version.properties')
      return versionProperties['LAST_BETA_HASH'].toString()
  }
  
  def getReleaseCandidateHash = { -&gt;
      def Properties versionProperties = loadPropertiesFile('../version.properties')
      return versionProperties['LAST_RC_HASH'].toString()
  }
  
  def getCurrentHash = { -&gt;
      def cmd = "git rev-parse --short HEAD"
      def gitProcess = cmd.execute()
      def gitHash = gitProcess.text.trim()
  
      return gitHash
  }
  
  def getReleaseNotes = { -&gt;
      def Properties versionProperties = loadPropertiesFile('../version.properties')
      return versionProperties['NOTES'].toString()
  }
  
  def getReleaseNotesSinceHash = { hash -&gt;
      def cmd = "git log --pretty=-%x20[%h]%x20%s%n " + hash + "..HEAD"
      def gitProcess = cmd.execute()
      def notes = gitProcess.text.trim()
  
      loadAndStorePropertyFile( '../version.properties', 'NOTES', notes)
  }
  
  def copyAndRenameZipAlignedPackage = { variant -&gt;
      def timeStamp = (int)(new Date().getTime() / 1000)
      def versionCode = getVersionCode()
      def gitHash = getCurrentHash()
      def environment = project.environment
      def file = variant.zipAlign.outputFile
      def copyTask = project.tasks.create("copy${variant.name}Apk", Copy)
  
      copyTask.from(file)
      copyTask.into(file.parent)
  
      if (variant.buildType.name == "release") {
          copyTask.rename(".apk", "-" + environment + "-" + versionCode + "-"
                  + gitHash + "-" + timeStamp + "-RELEASE.apk");
      } else {
          copyTask.rename(".apk", "-" + environment + "-" + versionCode + "-"
                  + gitHash + "-" + timeStamp + "-SNAPSHOT.apk");
      }
  
      variant.assemble.dependsOn copyTask
      copyTask.dependsOn variant.zipAlign
  }
  
  android {
      compileSdkVersion 19
      buildToolsVersion '19.1'
  
      useOldManifestMerger true
  
      defaultConfig {
          applicationId 'com.example.app’
          minSdkVersion 14
          targetSdkVersion 19
  
          versionCode getVersionCode()
          testInstrumentationRunner 'com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner'
      }
  
      productFlavors {
          dev {
              // For explicit testing with Espresso and other frameworks.
          }
          hockey {
              // For distro to stakeholders.
          }
  
      }
  
      packagingOptions {
          exclude 'LICENSE.txt'
          exclude 'META-INF/LICENSE.txt'
          exclude 'META-INF/LICENSE'
          exclude 'META-INF/DEPENDENCIES'
          exclude 'META-INF/NOTICE'
          exclude 'META-INF/NOTICE.txt'
      }
  
      lintOptions {
          abortOnError true
      }
  
      jacoco {
          version = '0.6.2.201302030002'
      }
  
      sourceSets {
          main {
              manifest.srcFile 'AndroidManifest.xml'
              java.srcDirs = ['src']
              resources.srcDirs = ['src']
              aidl.srcDirs = ['src']
              renderscript.srcDirs = ['src']
              res.srcDirs = ['res']
              assets.srcDirs = ['assets']
          }
  
          androidTest.setRoot('tests')
  
          debug.setRoot('build-types/debug')
          beta.setRoot('build-types/beta')
          release.setRoot('build-types/release')
          dev.setRoot('build-types/dev')
          hockey.setRoot('build-types/hockey')
      }
  
      buildTypes {
          debug {
              applicationIdSuffix '.debug'
              debuggable true
              // still broken with dagger (espresso's dependency) as of 0.11.1
              // https://code.google.com/p/android/issues/detail?id=69174
              testCoverageEnabled false
          }
          beta {
              applicationIdSuffix '.beta'
              debuggable true
              testCoverageEnabled false
              signingConfig signingConfigs.betaSigning
          }
          release {
              runProguard false
              proguardFile 'proguard-config.txt'
              signingConfig signingConfigs.releaseSigning
          }
  
      }
  
      applicationVariants.all { variant -&gt;
          removeSetAnimationScaleFromManifest(variant)
  
          task("generate${variant.name}Javadoc", type: Javadoc) {
              description "Generates Javadoc for $variant.name."
              group "Code Quality"
  
              def Properties localProperties = loadPropertiesFile('../local.properties')
  
              source = variant.javaCompile.source
              classpath = files(variant.javaCompile.classpath.files) +
                      files(localProperties['sdk.dir'] + "/platforms/android-4.4.2/android.jar")
          }
  
          copyAndRenameZipAlignedPackage(variant)
      }
  }
  
  apply plugin: 'monkey'
  
  monkey {
      teamCityLog = false
      eventCount = 5000
      seed = 42
      failOnFailure = false
      install = false
  }
  
  apply plugin: 'hockeyApp'
  
  hockeyapp {
      apiToken = “xxxxx”
      releaseType = 0
      notify = 0
      status = 2
      notesType = 1
      commitSha = getCurrentHash()
      notes = getReleaseNotes()
      variantToApplicationId = [
          hockeyDebug:   “xxxxx”,
          hockeyBeta: “xxxxx”,
          hockeyRelease: “xxxxx”
      ]
  }
  
  dependencies {
      compile fileTree(dir: 'libs', include: ['*.jar'])
  
      compile project(':android_ngn_stack:android-ngn-stack')
      compile project(':SlidingMenuLibrary')
  
      compile 'com.android.support:support-v4:19.1.+'
      compile 'com.google.android.gms:play-services:5.0.77'
      compile 'org.apache.james:apache-mime4j:0.6'
      compile 'org.apache.httpcomponents:httpmime:4.2.3'
      compile 'org.jsoup:jsoup:1.6.3'
      compile 'com.nineoldandroids:library:2.4.0'
      compile 'com.googlecode.ez-vcard:ez-vcard:0.9.3'
      compile 'com.googlecode.libphonenumber:libphonenumber:5.9'
      compile 'net.hockeyapp.android:HockeySDK:3.0.2'
      compile("org.springframework.android:spring-android-auth:1.0.1.RELEASE") { exclude module: '*' }
      compile("org.springframework.android:spring-android-core:1.0.1.RELEASE") { exclude module: '*' }
      compile("org.springframework.android:spring-android-rest-template:1.0.1.RELEASE") { exclude module: '*' }
  
      apt "org.androidannotations:androidannotations:" + androidAnnotationsVersion
  
      provided 'com.jakewharton.espresso:espresso:1.1-r3'
      androidTestCompile 'com.jakewharton.espresso:espresso:1.1-r3'
  }
  
  apt {
      arguments {
          androidManifestFile variant.processResources.manifestFile
          resourcePackageName 'com.example’
      }
  </code></pre>
  
  <p>Here's the test module gradle file:</p>
  
  <pre><code>buildscript {
      dependencies {
          classpath "com.android.tools.build:gradle:" + androidToolsBuildGradle
          classpath "com.novoda:gradle-android-test-plugin:0.9.9-SNAPSHOT"
      }
  }
  
  apply plugin: 'java'
  
  test.reports.html.enabled = false     // just clean up dashboard from not generated reports
  test.reports.junitXml.enabled = false // just clean up dashboard from not generated reports
  
  apply plugin: 'android-test'
  apply plugin: "jacoco"
  
  gradle.taskGraph.beforeTask { task -&gt;
      if (task.name == "compileTestJava") {
          task.deleteAllActions()
          println "Task $task.name is being rendered useless"
      }
  }
  
  //Note - Alphabetical order matters for projectUnderTest
  android {
      projectUnderTest ':Acision_RCS_Client'
  }
  
  dependencies {
      testCompile 'junit:junit:4.11'
      testCompile 'org.mockito:mockito-core:1.9.5'
      testCompile('com.squareup:fest-android:1.0.+') { exclude module: 'support-v4' }
      testCompile('org.robolectric:robolectric:2.3') {
          exclude module: 'classworlds'
          exclude module: 'maven-artifact'
          exclude module: 'maven-artifact-manager'
          exclude module: 'maven-error-diagnostics'
          exclude module: 'maven-model'
          exclude module: 'maven-plugin-registry'
          exclude module: 'maven-profile'
          exclude module: 'maven-project'
          exclude module: 'maven-settings'
          exclude module: 'nekohtml'
          exclude module: 'plexus-container-default'
          exclude module: 'plexus-interpolation'
          exclude module: 'plexus-utils'
          exclude module: 'support-v4' // crazy but my android studio don't like this dependency and to fix it remove .idea and re import project
          exclude module: 'wagon-file'
          exclude module: 'wagon-http-lightweight'
          exclude module: 'wagon-http-shared'
          exclude module: 'wagon-provider-api'
      }
  
  }
  
  apply from: "$rootDir/gradle/jacoco-support.gradle"
  
  apply from: "$rootDir/gradle/android-studio-robolectric-support.gradle"
  </code></pre>