To do this, you only need to…

  1. In Android Studio, select the Edit Configurations item edit_configurations
  2. In it, add a new Remote configuration. No need to change the settings, just click OK: debug_jvm
  3. Start the build from the command line:
     ./gradlew --no-daemon -Dorg.gradle.debug=true -Dkotlin.daemon.jvm.options="-Xdebug,-Xrunjdwp:transport=dt_socket,address=5005,server=y,suspend=y" assemble
    
  4. Immediately click on debug and don’t forget to set a breakpoint in the Gradle script.

debug_breakpoint

This will also help debug a custom Annotation Processor.