12

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.

Is there any way in AS to attach the debugger to a java gradle task?

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)
  }
}
7
  • What exactly do you mean by "run this second library by itself?" Does it have a main method? What relation does this have to a Gradle task? Commented Aug 22, 2014 at 0:17
  • There is a core functionality library with tasks to build for android or desktop java. Android attaches the debugger automatically, java fails even when forcing OS level java console and debug. For reference it's a libgdx-based project. Commented Aug 22, 2014 at 13:11
  • You may only debug unit tests (Or any other executable -as in having a static void main method- that uses the library), you can't debug the library itself as a standalone project as Android Studio wouldn't know which routines to call Commented Aug 22, 2014 at 13:26
  • What if the library had its own entry point in a java-style application? I edited the OP to say it's more a module than a library. Picture it as 3 modules: one is common functionality which is platform-agnostic, which is a dependency of an Android launcher module and a Desktop launcher module. Unit test are platform-agnostic too but the launcher modules aren't. Commented Aug 22, 2014 at 13:29
  • In IntelliJ it's available on the click of a button, but AS is modified for android development so the option is either hidden or a bit more involved. Commented Aug 22, 2014 at 13:33

1 Answer 1

5

http://www.gradle.org/docs/current/userguide/application_plugin.html

Then, you can run the application by running gradle run. Gradle will take care of building the application classes, along with their runtime dependencies, and starting the application with the correct classpath. You can launch the application in debug mode with gradle run --debug-jvm (see JavaExec.setDebug()).

Add to your build.gradle

apply plugin:'application'

mainClassName = 'my.company.namespace.MainClass'

Create a new Run/Debug Gradle configuration

  1. Run
  2. Edit configurations
  3. "+" (Add)
  4. Gradle

or alternatively you can do a single [run] and duplicate the task.

  1. For the task use run --debug-jvm
  2. Rename it to [debug]

And from there attach the debugger following IntelliJ's indications:

  1. Run
  2. Edit configurations
  3. "+" (Add)
  4. Remote
  5. Rename to "Attach debugger"
  6. (Optional) On Before Launch, add Another Configuration and add [debug]

Now you can launch the [debug] followed by Attach Debugger, or if you followed Step 6 just press the Stop Debugger button once to get past the Listening for transport dt_socket at address: 5005 message that can be considered as entry point.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.