If you’re like me, you don’t like doing the same thing over and over again. Once you’ve done it 3 times, you create a tool/script to automate the task. And sometimes, to take it to the next level, you also create a nice UI for visualisation purposes. If you ever wanted to create an Android Studio plugin and didn’t know how to get started, this article hopefully gives you the basic building blocks.
For this tutorial, it’s assumed that you’re comfortable with Android Studio and have all the necessary tools to run/debug an Android application.
Most articles about creating Android plugins suggest using IntelliJ IDEA Community Edition, but you can just use Android Studio directly. You just need a gradle.build file and an XML to get started.
Let’s break this into steps. I find it easier to follow.
Create the folder structure illustrated below, no files, just the folders:
Create build.gradle with the following code:
You can change this code to include your own plugin information (name, maven group and version).
Create plugin.xml with the following content:
Open Android Studio and select File -> Open. Navigate to the build.gradle file created in Step 2 and open it. Android Studio will recognise this and automatically do a lot of things, like installing a gradle wrapper, and creating the necessary project/module files.
NOTE: this may take a while since the project depends on the IntelliJ IDEA open source project and will download the source code dependencies.
The plugin.xml contains a “project component” that points to a class that doesn’t yet exist. Go ahead and create that java class. If the gradle import worked correctly you will have access to the IntelliJ platform classes. The class will look something like this:
You’ve just created the “Hello World” of Android Studio plugins. All you need to do now is “run” it. There’s a gradle task called
runIdea you can fire from Android studio, or the command line. This command will start an instance of IntelliJ Idea in a sandbox environment (don’t worry that it’s not Android Studio) so you can test out the plugin. Once a project is loaded in this instance of Idea you should see the command line output printed out from the
There is a way to instantiate a sandbox of a different IDE by tweaking the build.gradle file:
alternativeIdePath refers to the installation path of the IDE, you’ll have to adjust this based on your OS and IDE version.
Basic Plugin Architecture
I’ll mention a couple of things that stood out to me, as an Android developer reading these docs.
The code you write is effectively Java (or Kotlin). You can use standard Java Swing components for a custom user interface. There are some UI components specific to IntelliJ but they should be very familiar.
The plugin.xml you created in “Step 3” is the connection to the outside world (e.g. the Android Studio / IntelliJ platform). This is where you tell IntelliJ what components you have, what integrations, and it will know how to load/display them. You can also define how other plugins can integrate with yours via extensions. Think of this as your “Main”. It’s the entry point to your custom plugin, where you can define new languages, editors for special file types, specific actions that can be triggered, settings screens or tool windows (and many more).
All of these components and how to integrate them are documented in the links above.
How to target Android Studio
If you followed the setup described here, the plugin should work with Android Studio 3.0 (this version is one of the latest when writing this article).
Plugin compatibility is explained in more detail here. Android Studio is based of some version of IntelliJ Idea Open Source project. To find out which version that is you can check out this article, in which you’ll learn to download the source for Android Studio and start looking into it for information that is not easily available through other means. You don’t need to do this. You can try building with different versions and see if it works (and it will probably take less time), but I think it’s a great exercise and you can learn some internals, like Icon paths you can reuse, actions, how a specific tool window works.
Building and Publishing
Once you finished coding your super awesome custom plugin, you’ll want to use it in your daily routine. If you follow the tutorials you’ll know how to do that using the Plugin Development environment. The method we used to set up the project with Gradle will make it a bit different, but not harder.
To generate a build ready for deployment just call the
buildPlugin gradle task and it will generate a zip file with the plugin. You can find the file in
To publish the plugin for everyone follow this tutorial. There’s also a gradle task for this intuitively called
If you can’t or don’t want to publish it, just use it for yourself, or behind a company firewall, that’s also possible. You can install plugins from a zip file (the one you generated) through the plugin settings screen, or you can follow this tutorial to set up a plugin repository. It’s essentially just a file server with an XML containing the path to the latest plugin zip file.
Now you have the basic ingredients to write your own Android Studio plugin. Let me know in the comments what kind of tool you will create.