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)
}
}
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
- Run
- Edit configurations
- "+" (Add)
- Gradle
or alternatively you can do a single [run] and duplicate the task.
- For the task use
run --debug-jvm
- Rename it to [debug]
And from there attach the debugger following IntelliJ's indications:
- Run
- Edit configurations
- "+" (Add)
- Remote
- Rename to "Attach debugger"
- (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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With