title: Debugging a gradle-based Java project on Android Studio?
body: <p>My current project is composed of two modules, one android and another one that can be used as standalone desktop java. I'd like to run this second module by itself and be able to debug it without going through a device. I don't want to have a secondary IntelliJ installation to swap between one or the other.</p>
  
  <p>Is there any way in AS to attach the debugger to a java gradle task?</p>
  
  <pre><code>apply plugin: "java"
  
  sourceCompatibility = 1.6
  sourceSets.main.java.srcDirs = [ "src/" ]
  
  project.ext.mainClassName = "com.project.Desktop.Launcher"
  project.ext.assetsDir = new File("../android/assets");
  
  task run(dependsOn: classes, type: JavaExec) {
      main = project.mainClassName
      classpath = sourceSets.main.runtimeClasspath
      standardInput = System.in
      workingDir = project.assetsDir
      ignoreExitValue = true
  }
  
  task dist(type: Jar) {
      from files(sourceSets.main.output.classesDir)
      from files(sourceSets.main.output.resourcesDir)
      from {configurations.compile.collect {zipTree(it)}}
      from files(project.assetsDir);
  
      manifest {
          attributes 'Main-Class': project.mainClassName
      }
  }
  
  dist.dependsOn classes
  
  eclipse {
      project {
          name = appName + "-desktop"
          linkedResource name: 'assets', type: '2', location: 'PARENT-1-PROJECT_LOC/android/assets'
      }
  }
  
  task afterEclipseImport(description: "Post processing after project generation", group: "IDE") {
    doLast {
      def classpath = new XmlParser().parse(file(".classpath"))
      new Node(classpath, "classpathentry", [ kind: 'src', path: 'assets' ]);
      def writer = new FileWriter(file(".classpath"))
      def printer = new XmlNodePrinter(new PrintWriter(writer))
      printer.setPreserveWhitespace(true)
      printer.print(classpath)
    }
  }
  </code></pre>