title: Android, Gradle. How to build application and run tests from test application
body: <p>I have android project and android test project inside it, located under folder tests. Structure of these project is eclipse like   </p>
  
  <p>+-src<br>
  | - res<br>
  | - libs<br>
  | - tests<br>
  |.....  </p>
  
  <p>I use gradle. All i want is to build app, run unit tests and get reports of them. But i don't understand how to do it right. I'm created build.gradle file in root folder</p>
  
  <pre><code>buildscript {
      repositories {
          mavenCentral()
      }
      dependencies {
          classpath 'com.android.tools.build:gradle:0.5.+'
      }
  }
  
  apply plugin: 'android'
  
  android {
      compileSdkVersion 18
      buildToolsVersion "18.1.1"
      defaultConfig {
          minSdkVersion 8
          targetSdkVersion 18
          testPackageName "ua.cooperok.stringcalc.tests"
          testInstrumentationRunner "android.test.InstrumentationTestRunner"
      }
      sourceSets {
          main {
              manifest.srcFile 'AndroidManifest.xml'
              java.srcDirs = ['src']
              resources.srcDirs = ['src']
              aidl.srcDirs = ['src']
              renderscript.srcDirs = ['src']
              res.srcDirs = ['res']
              assets.srcDirs = ['assets']
          }
          unitTest {
              java.srcDir file('tests/src')
              resources.srcDir file('tests/res')
          }
      }
  
      configurations {
          unitTestCompile.extendsFrom runtime
          unitTestRuntime.extendsFrom unitTestCompile
      }
  
      dependencies {
          unitTestCompile files("$project.buildDir/classes/release")
      }
  
      task unitTest(type:Test){
          description = "Run unit tests..."
          testClassesDir = android.sourceSets.unitTest.output.classesDir
          classpath = android.sourceSets.unitTest.runtimeClasspath
      }
  
      build.dependsOn unitTest
  
  }
  </code></pre>
  
  <p>But when i run <strong>gradle build</strong> i got error<br>
  <em>Could not find property 'output' on source set unit test.</em></p>
  
  <p>Also, as i understand, to get reports i need to apply plugin "android-reporting", but when i do this i got error<br>
  <em>Cannot add extension with name 'android', as there is an extension already registered with that name.</em>  </p>
  
  <p>Should i do it like this, or i must create new gradle.build file in my test application project?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I fixed error with output, and made gradle build successful, but I don't understand what's goind on. I purposely failed my test, but gradle build is still successful.<br>
  What I've done:<br>
   1. I moved task unitTest from android section to root, after that, my test project began to compile, but there is an error 'packackage android.test not found'<br>
   2. To fix that error I added to classpath path to android sources, so after that gradle build was successful, but realy I don't understand why it's always successful even when test fails  </p>
  
  <p>And still I don't understand how to get reports from test</p>
  
  <p>This is my new build.gradle file</p>
  
  <pre><code>buildscript {
      repositories {
          mavenCentral()
      }
      dependencies {
          classpath 'com.android.tools.build:gradle:0.6+'
      }
  }
  
  apply plugin: 'android'
  
  android {
      compileSdkVersion 18
      buildToolsVersion "18.1.1"
      defaultConfig {
          minSdkVersion 8
          targetSdkVersion 18
          testPackageName "ua.cooperok.stringcalc.tests"
          testInstrumentationRunner "android.test.InstrumentationTestRunner"
      }
      sourceSets {
          main {
              manifest.srcFile file('AndroidManifest.xml')
              java.srcDirs = ['src']
              resources.srcDirs = ['src']
              res.srcDirs = ['res']
              assets.srcDirs = ['assets']
          }
      }
      dependencies {
          compile fileTree(dir: 'libs', include: '*.jar')
      }
  }
  
  sourceSets {
      unitTest {
              java.srcDirs = ['tests/src']
              resources.srcDirs = ['tests/src']
      }
  }
  
  configurations {
      unitTestCompile.extendsFrom runtime
      unitTestRuntime.extendsFrom unitTestCompile
  }
  
  dependencies {
      unitTestCompile files("$project.buildDir/classes/release")
      unitTestCompile 'junit:junit:4.11'
      compile fileTree(dir: 'libs', include: '*.jar')
  }
  
  task unitTest(type:Test, dependsOn: assemble) {
      description = "run unit tests"
      testClassesDir = sourceSets.unitTest.output.classesDir
      classpath = files("$System.env.ANDROID_HOME/sources/android-18")
      //actualy I didn't  get any report
      getReports().getJunitXml().setOutputPerTestCase(true)
  }
  
  build.dependsOn unitTest
  </code></pre>