As an Android developer, I use Android Studio every day. While coding, I often wonder: Can I make Android Studio more powerful? For example, can you pull the source code of a library module with one click and bring it into a build? Is there support for more intelligent ai-based code hints? Can custom domain-specific languages be supported? In fact, the Android Studio plug-in makes all of these ideas a reality. This article describes in detail how to use Gradle to develop Android Studio plug-ins. If you’re an Android developer who wants to be more productive, read on!

Prepare the environment

The only tool you need to develop an Android Studio plug-in is Intellij IDEA (available in both paid and community versions). You can download the latest version of Intellij IDEA from the Jetbrains website. This article will be demonstrated using Intellij IDEA Community Edition 2021.2.

Create a project

Open Intellij IDEA and create the project by following these steps:

  1. Go to file-new-project…

  1. On the New Project page, select Gradle, select Java and Intellij Platform Plugin, and click Next

  1. Fill in the name of the project and click Finish

The configuration of engineering

After finishing the previous section, Intellij IDEA automatically created an Intellij Plugin project for us to build using Gradle. The main configuration information for the project is in the root directory of build.gradle:

As you can see, Jetbrains passes the Gradle pluginorg.jetbrains.intellijThe project configuration is complete. Gradle plugin provides rich configuration items to help developers achieve various personalized configuration. You can check it out in the official repository’s READMEComplete configuration items.

Since the purpose of this article is to help Android developers develop plug-ins for Android Studio, this section focuses on common configurations for Android Studio plug-ins.

Intellij configuration

Intellij version

In build.gradle, the default Intellij version is the same as the one used to create the project. The Intellij version of Android Studio is often different. We can view the Intellij version of Android Studio by clicking Android Studio – About Android Studio:

For example, Intellij information marked in the figure above is:

The Build # AI - 203.7717.56.2031.7583922, built on out 27, 2021Copy the code

The Intellij version is 203.7717.56. In addition, Android Studio is based on the community version of Intellij IDEA, so we need to add the type field to IC:

. Intellij {version = '203.7717.56' type = 'IC'}...Copy the code

Add android Plugin dependencies

Android Studio actually provides Android-specific features in the form of add-ons for Intellij IDEA. If we want to use some of the open android-related interfaces provided by Android Studio, we need to add a dependency on the plugin so that we can access these interfaces:

. Intellij {version = '203.7717.56' type = 'IC' plugins = ['android']}...Copy the code

RunIde configuration

Intellij’s Gradle plugin provides a task, runIde, to help developers debug plug-ins directly from the IDE. Android developers can configure the IDE path to the local Android Studio path to debug directly in the local Android Studio:

. runIde { ideDir = project.file('/Applications/Android Studio.app/Contents') } ...Copy the code

Note:runDirThe value of must beFileType (not mentioned in the JetBrains documentation).

The plugin. The XML configuration

By default, different plug-ins run in different Classloaders, so that different plug-ins can use different versions of the same library without affecting each other. However, when a developer wants to get some Android specific information (such as the version of the Android Gradle Plugin) by calling the Interface of the Android plugin, this default mechanism obviously fails to get the information. The developer can make the current plug-in run in the same Classloader as the plug-in declared in depends by declaring depends in plugin.xml. For Android plugins, we can declare in plugin.xml:

<idea-plugin>
    ...
    <depends>org.jetbrains.android</depends>
    ...
</idea-plugin>
Copy the code

This way, we can get the data provided by the plug-in at run time and invoke the methods provided by the plug-in correctly.

Build and run

Using the GUI

When we created the project, Intellij IDEA created the corresponding operation configuration for us automatically, as shown below:

You can Run the plug-in project by clicking the Run button, and Debug the plug-in project.

Clicking the button will launch an Intellij IDEA to run the plugin by default, and if you have configured the runIde task as described above, it will launch the native Android Studio plugin.

Using the command line

From the Tasks pane on the right, you can see that the Intellij group contains a series of Gradle Tasks related to IDE plug-ins.We can go through./gradlew buildPluginTo build the plug-in project, the default output path of the product is/build/distributions. We can go through./gradlew runPluginTo run the plug-in project.

More and more

At this point, we use Gradle to complete the plug-in project configuration, build and run. For more information on how to develop Android Studio plugins, you can refer to Jetbrains’ official documentation: Intellij Platform SDK. I’ll also be updating the fun part of this and how to release an Android Studio plugin later.

Follow me to learn more.