Write an Android Studio Plugin Part 1: Creating a Basic Plugin
IntelliJ IDEA is an official recommended learning plugin IDE development blog column, hoping to help readers who need it.
Back in October, I had a discussion at Droidcon UK 2018 about how to create your own plug-ins on Android Studio and how to automate everything. I didn’t have a lot of time to cover it in detail, so the series was born.
What are we going to do?
In this article, we will write a very basic plug-in. This time it may not be much, but the important thing is that we will learn about plug-ins and what it takes to create them. We will also create a new Action that displays a pop-up box with a message.
This is just the first part of a very long series of articles that I’ll delve into and extend more features for our plug-in. All the code you’ll see is in the GitHub repository below:
marcosholgado/plugin-medium
As I progress, I will keep a separate branch for each article, although the master will always keep the code up to date. To do so, you can always go back and look at the Part1 branch.
Step 1: Install IntelliJ IDEA CE
To create our plug-in, we will use The IntelliJ IDEA Community Edition. The main reason for this is that the community edition is free and very easy to use, and the Gradle-Intellij-plugin makes the process much easier.
I will use IntelliJ IDEA CE 2018.1.6 which is not the latest stable version of IntelliJ IDEA CE. The reason is because Android Studio 3.2.1 based on this IntelliJ release, I don’t want to use any new features that might not be compatible with Android Studio either.
You can download IntelliJ IDEA CE 2018.1.6 here:
www.jetbrains.com/idea/downlo…
Note: The above link jumps to the latest version of IDEA Preview, which you can download to your liking.
Step 2: Create a new plug-in project
As usual, create a new project.
Between Gradle and the IntelliJ Platform Plugin, the only thing we need to do is decide which language we will use. For this article I will choose Kotlin (Java).
After that, we need to define three properties.
- GroupId – The GroupId of the new project, which can be ignored if you plan to deploy the project locally;
- ArtifactId – The name of the new project;
- Version – Version of the new project. By default, this field is automatically specified.
Since I do not intend to publish this plug-in, I will use myPlugin only as GroupId and ArtifactId, which readers can customize as needed.
In the next popup, I’m going to leave nothing changed and just keep the default options. If you want to learn more about this step, please visit here.
The last step is to give our plug-in a name.
Congratulations to you! You just created a new plug-in that works not only with Android Studio, but with any other IDE based on IntelliJ — even though your plug-in doesn’t do anything else yet.
Step 3: Gradle and plugin.xml
Now that we have created the project, we will quickly browse through two files: plugin.xml and build.gradle.
Let’s start with plugin.xml. This file contains some plug-in related metadata and the locations where we must register different elements of the plug-in. We will delve deeper into some of this document when necessary.
The other file is build.gradle, which we are already familiar with, so I won’t explain what it does. Here is the default build.gradle file you will get (the content varies slightly from version to version) :
plugins {
id 'org.jetbrains.intellij' version '0.3.12'
id 'org.jetbrains.kotlin.jvm' version '1.3.10'
}
group 'myplugin'
version 1.0 the SNAPSHOT ' '
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
intellij {
version '2018.2.2'
}
patchPluginXml {
changeNotes """ Add change notes here.
most HTML tags may be used"""
}
Copy the code
It was deja vu because we were used to Android projects. As we start adding more dependencies, we’ll go back to that file, and so on, but for now I’m just focusing on two things.
First, you can see that our dependencies use compile instead of implementation/ API, mainly because gradle-Intellij-plugin does not yet support implementation/ API.
Note: The latest version of the IDEA plugin is supported.
However, if you need kapt for any type of annotation processing, you can use kapt (speaking of you, Dagger). As with any Android project, you can use any library you need, but be careful because this is not an Android project.
Second, we have a new section called Intellij. Here, we’ll add more properties, such as plug-in dependencies, as needed. Now, the only property we have is Version, which specifies the release version of IDEA that should be used as a dependency.
Before continuing, I’ll add another attribute to the Intellij section. The plugins we now have can only be debugged on IntelliJ IDEA CE. If we wanted to debug the behavior of the plug-in immediately, a new instance of IntelliJ would be launched and we would have to debug/test there. Obviously we want to test our plugin on Android Studio in order to tell Gradle-Intellij-Plugin that we want to use Android Studio and we must add a new property.
The final section states as follows:
intellij {
version '2018.1.6'
alternativeIdePath '/Applications/Android Studio.app'
}
Copy the code
By using AlternativeIdePath and pointing to the locally installed Android Studio, we tell gradle-intellij-plugin to use Android Studio whenever you run the plug-in or debug it, Instead of using the default IntelliJ IDE.
If you can’t wait for another article to look at the other properties available, visit here for more information.
Step 4: Write your first Action
There are different elements we can use in the plug-in, and we’ll look at all of them in a future article, but for now we’ll focus on the most commonly used: Actions.
When you click on a toolbar or menu item, it’s basically an action, as simple as that, and everything you see in the following images is displayed by Actions:
With Actions, we can apply our own projects to Android Studio menus and toolbars. These Actions are organized into groups, which can contain other groups, and so on.
To create a new Action, we must have this class inherit from AnAction and override the actionPerformed method.
class MyAction: AnAction(a){
override fun actionPerformed(e: AnActionEvent) {
val noti = NotificationGroup("myplugin", NotificationDisplayType.BALLOON, true)
noti.createNotification("My Title"."My Message",
NotificationType.INFORMATION,
null
).notify(e.project)
}
}
Copy the code
Here, we create a simple Action that displays a popover with a title and a message. The last thing we need to do is add this Action to our plugin.xml file.
<actions>
<group id="MyPlugin.TopMenu"
text="_MyPlugin"
description="MyPlugin Toolbar Menu">
<add-to-group group-id="MainMenu" anchor="last"/>
<action id="MyAction"
class="actions.MyAction"
text="_MyAction"
description="MyAction"/>
</group>
</actions>
Copy the code
Our Action must belong to a Group, so first, I create a new Group with ID myplugin.topmenu. I also added this Group to the MainMenu Group, which is the main toolbar you can see on any IntelliJ IDE. I anchor its position as the last one so that our Group will be in the last position of the Group. Finally, I added the Action to myplugin.topMenu Group so that we could access it from there.
If you want to know how I know MainMenu ID exists, just command + click MainMenu ID, which will take you to a file called platformActions.xml, This file contains most of the actions (similar to vcsactions.xml) and groups in the IDE.
We can perform many different operations on actions and groups, such as adding delimiters or reusing them. I’ll explore them in a future article, but for now you can check them out here.
Step 5: Run it!
So, we’ve just written a very simple plug-in. Now we can run it using the Run button and debug it. This will create a new Android Studio instance that will install our plug-in. Another option is to run the buildPlugin Gradle task, which generates a.jar file that you can install as a plug-in on Android Studio or any other IntelliJ IDE.
After installing the plug-in and running Android Studio, you can now see the new MyPlugin Group with MyAction on the main toolbar.
After you click MyAction, a new pop-up window displays with the title and message you defined. Remember to enable Show Balloons on the log log window; otherwise, you will not see pop-ups, but a log event.
That’s the first part. In Part 2, we’ll look at how to use components to store data and hold the state of plug-ins. In the meantime, if you have any questions, please visit Twitter or comment.
If you’d like to watch my talk at Droidcon UK, click here.
“AndroidStudio plugin” translation series
- Create a basic plugin for AndroidStudio
- Write a plugin for AndroidStudio (2): persist data
- Writing a plugin for AndroidStudio (iii): more configuration
- Write a plugin for AndroidStudio (4): integrate Jira
- Writing AndroidStudio plug-ins (5): localization and notifications
About the translator
If you think this article is valuable to you, welcome to ❤️, and also welcome to follow my blog or GitHub.
If you feel that the article is not enough, please also urge me to write a better article by paying attention to it — in case I make progress one day?
- My Android learning system
- About the article error correction
- About paying for knowledge
- About the Reflections series