Emacs as an IDE: Android, Java, and Kotlin
In the two previous posts, I covered basic Emacs setup and Flutter support. I'll now be tackling the other area that consumes most of my work development time, Java/Kotlin and Android.
Installing Java, Kotlin, and Gradle with SDKMAN
My preferred way to install Java and Kotlin is sdkman.io which allows you to install several Java and Kotlin versions along side each other and easily switch between them. To run Android projects, you only specifically need to install Java which all versions of Gradle requires to run. Many Android projects will include a gradlew
script that will download Gradle at build time but I like to install a specific version of Gradle because the wrapper will redownload executables and dependencies.
sdk i java 11.0.13.fx
sdk i kotlin
sdk i gradle
Latest and greatest is generally fine for Kotlin and Gradle. For Java, I like to use JDK11.
Adding Java, Kotlin and Android support to Emacs
Again, we venture to Doom's init.el
file (SPC f P
), we need to add enable some options.
In the :lang
section, we need to modify the java and kotlin lines as follows:
(java +lsp)
(kotlin +lsp)
Enabling the Java module enables Java, Groovy, and Android support. The Android features allow you to create, control, and debug Android projects from Emacs with some keybindings for common operations. You can even control emulators. The Kotlin module provides a Kotlin REPL and its own Gradle support.
Setting up Java and Kotlin LSPs
Java's LSP server is a part of the default doom-emacs build so all you need to do to install is executing M-x lsp-install-server
and select jdtls
.
If you change Java versions, you need to uninstall and reinstall the server.
M-x +lsp/uninstall-server
M-x lsp-install-server
Kotlin, on the other hand, requires a little bit more work. LSP support is provided via a community developed LSP server at https://github.com/fwcd/kotlin-language-server.
Clone the repo, run gradlew :server:distZip
to get an archive of the language server and its dependencies. The compiled zip will be in the /server/build/distributions
folder. Extract that somewhere and add the path to your config.el
doom file. Add the Java path for good measure
(add-to-list 'execpath "<path to kotlin-language-server>")
(setenv "JAVA_HOME" "<java home path>")
Finally, reload(SPC h r r
) your configuration and you are good to go. Here's a quick video showing how moving around a project would work.