Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging a gradle-based Java project on Android Studio?

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)
  }
}
like image 816
MLProgrammer-CiM Avatar asked Aug 22 '14 00:08

MLProgrammer-CiM


Video Answer


1 Answers

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.

like image 144
MLProgrammer-CiM Avatar answered Oct 19 '22 14:10

MLProgrammer-CiM