Plugins make developer's life easier by giving an opportunity to add specific features to the already existing programs and this way improving the development experience in general. Android Studio plugin is the best way to boost your productivity and power pack your IDE (Integrated development environment).
There are many of them already created and widely used by developers. Some of them allow one-click injections of code pieces, some help debugging the android app over WI-Fi, some show handy hints, others check the code and support markdown language or generate material design icons. What's common is that all of them speed up your day to day android development.
But the first reason for creating plugins is that developers don't like to do the same things over and over. Once some action is done more than 3 times, they are obliged to create a tool that automates the task. Sometimes attractive visualization is needed as nice UI makes those plugins even more pleasant to use.
Of course, there are many already existing plugins, but sometimes you need something specific that will solve your own repeating issue. At this time you will need some extra time to learn how to create your own Android Studio plugin. Have you done it already? No? Well, you are at the right place—here you will see a step by step guide on how to create a simple plugin for Android Studio.
There is a great platform that will make the process of plugin development much easier—IntelliJ Platform that is a platform for building IDEs. With IntelliJ IDEA it is possible to create plugins of any complexity—from those who solve applied tasks to the ones supporting custom programming languages. The only difficulty you might face is finding proper documentation. Although with IDEA’s developer, JetBrains and other resources that wouldn't be a problem.
Now let's break it into steps. I find it easier to follow.
Step 0. Before start
It is useful to prepare yourself beforehand in order to understand the context of the written below, so the "Getting Started" guide will help you to get familiar with IDEA plugin development.
Step 1. Find the Android Studio's source code
There is a lot of documentation on plugin creation about IDEA and Android Studio, but most tutorials don't give you the full information that is needed in such case. With Android Studio's source code, you will easily understand how IDEA functions. In general, it will save you lots of time and effort.
Android Studio's source code is important to get for several reasons. Most importantly, by checking that code you will see which IDEA git branch Android Studio is using. This will give you the version of IDEA you need and the exact branch that needs to be used. Remember, the version of IDEA's source code should completely match the one Android Studio uses. This way your plugin will work without issues.
Android Studio's source code consists of IDEA's source code and the Android plugin. So you can easily navigate through IDEA's code and find there any class or reference to it in Android Studio's source code. To the contrary, IDEA SDK doesn't have all the IDEA's plugins. So in case you open the missing in SDK class definition you will receive the decompiled version.
Step 2. Check the Android Studio's IDEA branch
You can find the branch in the adt-branding
module of the AndroidStudioApplicationInfo.xml
file. Here is the string you are interested in:
<build number="__BUILD_NUMBER__" date="__BUILD_DATE__" apiVersion="141.178"/>
So in this particular case, the branch is 141 (matches the source code for Android Studio 1.2). This information gives you the following:
- Understanding of what source code for IDEA to download from JetBrains' git repositories or Github
- The exact IDEA version to download
Step 3. Use IDEA
Android Studio needs help from IDEA to actually develop the plugin as has no inner tools for creating an "IntelliJ Platform Plugin" project. It is recommended to use Community Edition for our purpose, although you do have a choice. There is an IDEA plugin SDK that can be equally used for development.
Step 4. Get IDEA's source code
Following the JetBrains' guide, you can easily find the IDEA's source code with the exact same branch that Android Studio uses.
Step 5. Build an IntelliJ IDEA SDK
In order to build an IntelliJ IDEA SDK, it is required to follow the steps that Jetbrains officially offers. This way you will get the detailed information on what exactly to do while solving your task. Some dependencies might be absent in that SDK, so in case you need access to the code from the Android plugin, you want to add those dependencies to the SDK. You might want to experiment while doing it until the code runs well. Here they are:
${IDEA_HOME}/plugins/android/lib/android-common.jar
${IDEA_HOME}/plugins/android/lib/android-rt.jar
${IDEA_HOME}/plugins/android/lib/android.jar
${IDEA_HOME}/plugins/android/lib/androidAnnotations.jar
${IDEA_HOME}/plugins/android/lib/layoutlib-api.jar
${IDEA_HOME}/plugins/android/lib/sdk-tools.jar
${IDEA_HOME}/plugins/android/lib/sdklib.jar
${IDEA_HOME}/plugins/android/lib/common.jar
${IDEA_HOME}/plugins/android/lib/builder-model-1.1.0-rc1.jar
${IDEA_HOME}/plugins/junit/lib/idea-junit.jar
${IDEA_HOME}/plugins/junit/lib/junit-rt.jar
${IDEA_HOME}/plugins/properties/lib/properties.jar
${IDEA_HOME}/plugins/gradle/lib/gradle.jar
${IDEA_HOME}/plugins/Groovy/lib/Groovy.jar
${IDEA_HOME}/plugins/Groovy/lib/groovy_rt.jar
${IDEA_HOME}/lib/junit.jar
${IDEA_HOME}/plugins/android/lib/resources_en.jar
${IDEA_HOME}/plugins/android/lib/templates/activities
${IDEA_HOME}/plugins/android/lib/guava-jdk5-17.0.jar
${IDEA_HOME}
is the path where IntelliJ IDEA is installed.
Step 6. Create a plugin
The same as in the previous step it is better to use official documentation in order to create your plugin. With a minor reservation, in case you want to use Android plugin that IDEA SDK is missing, simply add the following lines to the plugin's plugin.xml file:
<depends>org.jetbrains.android</depends>
<depends>org.jetbrains.plugins.gradle</depends>
<depends>com.intellij.properties</depends>
Also, it is pretty useful to generate an Ant build for the project. Of course, in the case when you don't need to customize how the plugin is packaged, it is enough to compile and create a plugin via the menu Build > Prepare All Plugin Modules for Deployment
in IDEA.
In the opposite situation, you can generate Ant build files through the menu Build> Generate Ant Build
. This Ant build helps a lot in cases you use Mac OS X Yosemite. As it is not compatible with JDK 1.6 which you might want to use to create a plugin. You will need JDK 1.7 for that.
So without Ant build switching JDK versions for running and packaging the plugin will give you a hard time. But with Ant build, there is no switching needed.
Step 7. Launch and debugging
A simple combination of Shift + F10
or the "Run" button make wonders in launching your plugin. For debugging it is enough to use Shift + F9
combination. At this stage, you might find useful some solutions for possible issues you may need to overcome.
Step 8. Test your plugin
It is vitally important to test the plugin on both Android Studio and IDEA as the versions both of them use (Android Studio might not use the latest version of IDEA, and the opposite) can be different or outdated.
Step 9. Publishing
Here you are at the stage when all the possible issues have already been solved. There's only one thing remains is plugin publishing that will give you the opportunity to use it in your daily routine. It is easy to do using the Plugin Development environment. Of course, there are other methods. In particular, you want to use this guide for everyone to access and use it in their development process.
In case you just want to use it yourself without giving access to someone else, you can simply install the plugin from the generated plugin zip file or set up a plugin repository following the instructions.
Conclusion
Plugins help developers to build applications faster, manage project resources, and write better code with efficient structure.
Now that you know the fundamental ingredients of the way an Android Studio plugin can be developed, you can start thinking about what kind of tool you want to create yourself. From now on you have every chance to do it!