Get started with Android Studio Plugin Development

Abhishek Ippakayal
Simform Engineering
7 min readDec 29, 2022

Android Studio, a widely used IDE for Android App development, helps us create apps faster and more productively. But, to modify or add new UI elements or features that we think will make the IDE better, plugins are the go-to option.

We may have seen several plugins or already using them. Some of them are Rainbow Bracket, Android Drawable Preview, Json to Kotlin class, etc. These plugins make our development easy and fast in one way or another.

Android Studio Plugins are additional features and customizations that are added to make development easy and fast.

In this blog, we will learn the basics of creating an Android Studio Plugin. We are going to create a basic plugin that shows a menu in the toolbar, and on click of that, will show a pop-up. So let’s get started.

Tip: When creating a new Android Studio Plugin, it is recommended to check whether any other alternative is available or not. Also, read the types of plugins to identify the plugin type based on its requirements.

Here are the steps that we are going to follow in this blog to complete our first Plugin:

1. Setup
2. Creating Plugin Project
3. Understanding key files
4. Creating our first action
5. Run our Plugin

1. Setup

We can use IntelliJ IDEA Community Edition or IntelliJ IDEA Ultimate to create a plugin. For this process, we will be using IntelliJ IDEA Community Edition 2022.1.2 version. The latest version can be found here.

Tip: Always use the latest version.

2. Create Plugin Project

After downloading the IntelliJ IDEA Community Edition, let’s create Plugin Project:

Open IntelliJ IDEA, select Projects from the left-drawer menu, and click New Project.

It will then redirect you to the project configuration window.

In the project configuration window, select the IDE Plugin option from the left drawer menu and fill in the basic details, as shown in the below image.

Here’s what each field represents:

  • Name: Project name for the plugin. We will use Demoplugin as the Project name.
  • Location: The location where the project files will be stored. It shows the Default IDE path, we can change it as per the project requirements. For now, let’s leave it as it is.
  • Type: It will define the type. We need to select Plugin as we are going to build a plugin. The second Theme type is for creating an IDE theme by overriding the existing theme.
  • Language: Select the programming language. We will use Kotlin.
  • Group: The group ID identifies this project across other plugin projects. It should be unique for every plugin. Normally we use inverted company domains such as ‘com.example’.
  • Artifact: This is the project name.
  • JDK: This is used to compile and run the project.

3. Understanding key files

This step is to get familiar with the project configuration files and structure, as going forward, we will modify those files.

After creating the Plugin project, we can see the below structure of our project.

So in this structure, 2 files are key files for configuring our plugin:
- plugin.xml
- build.gradle.kts
Let’s take a look at these files and their content:

plugin.xml

This configuration file contains information about the plugin, like components and actions.

Below are the contents of our newly created default DemoPlugin project’s plugin.xml.

<idea-plugin>: It is the root tag of this file; all the configurations related to the plugin will be declared under this tag.

<id>: It is the unique identifier of the plugin, just like the package name of an Android project. It should not collide with the ID of other existing plugins as it is used to identify the plugin in IDE and JetBrains Marketplace.

<name>: This tag contains the plugin’s name displayed in the Plugin list.

<vendor>: Display the vendor name or organization ID for users to know which organization has created this plugin. This can be used to show the company name. There are two values for this tag, and both are optional.

  • <email>: email of the Vendor.
  • <url>: Organization’s website URL. We can use this to redirect users to our company website.

<description>: This tag contains a detailed description of the plugin, which will be displayed on the JetBrains Marketplace Plugin page and in the Plugins settings dialog.

<depends>: This specifies the dependency of the plugin on another plugin or IntelliJ Idea Platform module. A single <idea-plugin> element can contain multiple <depends> elements.

<extensions>: This specifies the plugin extensions.

For more information about all the available tags, check this guide.

build.gradle.kts

  • This file contains all the build-related configurations. Anyone familiar with Android development may know about the build.gradle.kts file.
  • This file is similar to the Android project’s build.gradle.kts file. Gradle is the build tool that uses this file to build the project. We can specify the steps and configurations for building our project in this file.

4. Create the first action

Action is an element that is added to the menu or toolbar. It creates an item in the menu or toolbar, which, when clicked, performs some task that we have defined. Some examples are file, open, etc.

To create an action, we need to inherit our class with AnAction class and implement its method. Below is the code for MyDemoActionclass:

class MyDemoAction: AnAction() {

override fun update(e: AnActionEvent) {
super.update(e)
}

override fun actionPerformed(e: AnActionEvent) {
Messages.showMessageDialog(
e.project,
"Great! You just created your first action!",
"My First Action",
Messages.getInformationIcon());

}

// Override getActionUpdateThread() when you target 2022.3 or later!
}

Let’s understand its methods and what it does. Here I have inherited MyDemoAction class with AnAction and overridden two methods:

  • AnAction.update(): This method is used to enable/disable the menu. This method should be overridden if the action needs to be enabled on specific conditions and disabled in some cases.
  • AnAction.actionPerformed(): This method is called when the menu or toolbar is clicked. In this method, we need to write code for our action implementation. We must override this method otherwise, the IDE will give an error.
  • AnAction.getActionUpdateThread(): This method must be implemented when targeting IntelliJ Platform 2022.3 or later.

In actionPerformed() method, we have written code to show a message with a Title and Body. Messages.showMessageDialog is a library method to show a popup message.

We have created our action, but we need to register it in plugin.xml.

This can be done in two ways:
- With UI wizard
- Writing manually in plugin.xml

We are going to write it manually:

<actions>
<group
id="MyDemoActionGroup"
description="Demo action group">
<action
id="com.example.demoplugin.MyDemoAction"
class="com.example.demoplugin.MyDemoAction"
text="Demo Action"
description="Demo action example"/>
<add-to-group group-id="ToolsMenu" anchor="first"/>
</group>
</actions>

In plugin.xml this block is added under <idea-plugin> tag. Let’s look at the tags and their properties:

  • <actions>: This tag is used to define action.
  • <group>: This tag defines the group to which this action will be added.
    - id is used to give a unique identifier to this group.
    - description is used to describe the group.
  • <action>: This defines the action and its properties.
    - id is used to give a unique identifier to every action. This must be unique.
    - class is the FQN of the class in which this action is implemented.
    - text is the name of the action in the menu or toolbar. This text is visible to the user in the menu or toolbar.
    - description is hint text displayed to the user for the action.
  • <add-to-group>: The action group — menu or toolbar — to which the action is added. Here we added it to the Tools menu.
    - anchor is to define where we want our action to be placed relative to other actions in that group. Example, first or last.

With that, we are done with the code. We have created one action and registered it. You can learn more about actions here.

Finally, it is time to run the plugin!

5. Run the Plugin

To run the plugin, press the run button at the top right corner of the IDE. Select the run plugin from the drop-down, as shown in the below image. (Note: we can also run the plugin from the run menu.)

It will launch another instance of the IDE to run our plugin. After it is launched, in a new instance of the IDE, we can see our Demo Action menu in the Tools menu.

On clicking that, we will see our Action dialog box.

This is it! We have successfully created our first plugin.

Wrap up

This is a basic plugin where we added a menu in the Tools menu using the action element and a dialog box that shows up on clicking.

We can do a lot more things with Plugin SDK.

Go, explore, and implement your plugin ideas that make IDE better for developers.

Happy coding! 😃

--

--