title: Debug gradle-based unit tests with IntelliJ / AndroidStudio
body: <p>I am using the <a href="https://github.com/robolectric/robolectric-gradle-plugin">robolectric gradle plugin</a> to write my unit tests for Android. Everything works fine so far, apart from being able to properly debug my tests using Android Studio.</p>
  
  <p>I did some investigation (<a href="http://forums.gradle.org/gradle/topics/how_do_you_attach_a_debugger_to_gradle_so_that_i_can_debug_it_running_a_task">http://forums.gradle.org/gradle/topics/how_do_you_attach_a_debugger_to_gradle_so_that_i_can_debug_it_running_a_task</a>) and what I ended up is:</p>
  
  <ol>
  <li><p>Start debugable gradle config from console
  <code>gradlew -DtestDebug.debug=true app:clean app:testDebug</code>
  This will stop gradle build and wait for listener at 5005</p></li>
  <li><p>Create "Remote" launch configuration in Android Studio, which will attach on port 5005</p></li>
  <li><p>Launch that configuration from Android Studio in Debug-mode</p></li>
  </ol>
  
  <p>Step 1 + 3 have to be done each time, i want to debug my tests. That is pretty annoying. <strong>I would like to have a way, that I can directly launch a gradle build from intelliJ, which will automatically attach the debugger. How can I do that?</strong> </p>
  
  <p><strong>Info:</strong> (Of course, the ideal solution would be that I can launch unit tests directly via the JUnit-configuration of IntelliJ, since that would give me IDE support, neat display of failing and passing tests, etc. But I experienced problems with the JUnit builds not finding the Manifest file, etc, so I gave up on that for now.)</p>
  
  <p>**Gradle build file **</p>
  
  <pre><code>apply plugin: 'com.android.application'
  
  buildscript {
      repositories {
          mavenCentral()
      }
      dependencies {
          classpath 'org.robolectric:robolectric-gradle-plugin:0.12.+'
      }
  }
  
  apply plugin: 'robolectric'
  
  android {
      compileSdkVersion 20
      buildToolsVersion "20.0.0"
  
      packagingOptions {
          exclude 'LICENSE.txt'
          exclude 'META-INF/LICENSE'
          exclude 'META-INF/LICENSE.txt'
          exclude 'META-INF/NOTICE'
      }
  
      defaultConfig {
          applicationId "test.fs.test"
          minSdkVersion 14
          targetSdkVersion 20
          versionCode 1
          versionName "1.0"
      }
      buildTypes {
          release {
              runProguard false
              proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
          }
      }
  }
  
  dependencies {
      compile fileTree(dir: 'libs', include: ['*.jar'])
  
      androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'
      androidTestCompile 'org.hamcrest:hamcrest-core:1.3'
      androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
  
      androidTestCompile('junit:junit:4.11') {
          exclude module: 'hamcrest-core'
      }
      androidTestCompile('org.robolectric:robolectric:2.3') {
          exclude module: 'classworlds'
          exclude module: 'commons-logging'
          exclude module: 'httpclient'
          exclude module: 'maven-artifact'
          exclude module: 'maven-artifact-manager'
          exclude module: 'maven-error-diagnostics'
          exclude module: 'maven-model'
          exclude module: 'maven-project'
          exclude module: 'maven-settings'
          exclude module: 'plexus-container-default'
          exclude module: 'plexus-interpolation'
          exclude module: 'plexus-utils'
          exclude module: 'wagon-file'
          exclude module: 'wagon-http-lightweight'
          exclude module: 'wagon-provider-api'
      }
  }
  
  apply plugin: 'idea'
  
  idea {
      module {
          testOutputDir = file('build/test-classes/debug')
      }
  }
  </code></pre>