Develop Android Studio’s Plugin Using Gradle

As an Android developer, I use Android Studio every day. When coding, I often think: Is it possible for us to make Android Studio more powerful? For example, can we pull the source code of a library module with one click and import it to the build? Can we support smarter code completion based on AI? Can we support custom domain-specific languages? In fact, the Android Studio plugin can turn all of the above ideas into reality. This article will detail how to use Gradle to develop Android Studio plugins. If you are also an Android developer who wants to improve productivity, then continue reading!

Preparation

The only tool that needs to be prepared to develop Android Studio plugins is Intellij IDEA (both Ultimate Edition and Community Edition are OK). You can download the latest version of Intellij IDEA from the official website of Jetbrains. This article will use Intellij IDEA Community Edition 2021.2 as a demonstration.

Create Project

Open Intellij IDEA, follow the steps below:

  1. Select File – New – Project…

2. On the New Project page, select the Gradle tab and make sure Java and Intellij Platform Plugin are selected. Click Next.

3. Enter the project’s name. Click Finish.

Configure Project

Then Intellij IDEA will create an Intellij Plugin Project using Gradle to build. Most configurations are located in the build.gradle file under the root directory.

As you can see, here’s a Gradle plugin called org.jetbrains.intellij. It provides lots of configurations for developers to customize their plugins. You can see all of the configurations here.

intellij

Intellij’s version

In build.gradle generated by Intellij IDEA, Intellij’s version is the same as the Intellij IDEA which created this project. But Android Studio’s Intellij version is always different. We can see the Intellij version through Android Studio – About Android Studio:

The Intellij information of Android Studio shown upside is

Build #AI-203.7717.56.2031.7583922, built on July 27, 2021

So the corresponding Intellij version is 203.7717.56. As we all know that Android Studio is based on the Community Edition of Intellij IDEA, so we need to set the type field to IC:


intellij {
version = ‘203.7717.56’
type = ‘IC’
}

add android dependency

Actually, Android Studio adds android specific features through the android Intellij Plugin. If our plugin wants to use the APIs provided by Android Studio, we need to let our plugin depend on the android plugin:

 intellij { 
    version = ‘203.7717.56’ 
    type = ‘IC’ 
    plugins = [‘android’]
 } 

runIde

The Intellij Gradle plugin provides a task called runIde to help developers debug their plugins in the IDE. Android Developers can set the ideDir to the path of local installed Android Studio:

runIde { 
    ideDir = project.file(‘/Applications/Android Studio.app/Contents’) 
} 

Note: the value of ideDir needs to be File type which Jetbrains didn’t mention in their documentation.

plugin.xml

By default, different plugins have different ClassLoaders. So plugins can use different versions of the same libraries and don’t affect each other. But when we want to use APIs provided by the android plugin to get some Android-specific information(like the version of the android gradle plugin), we’ll fail because of this ClassLoader mechanism. Developers can use depends on API provided by plugin.xml to make their plugins use the same ClassLoader as the android plugin.

<idea-plugin> 
<depends>org.jetbrains.android</depends> 
</idea-plugin>

In this way, we can get the proper data at runtime.

Build and Run

Use GUI

Intellij IDEA will create Run Configuration called Plugin automatically:

Just click the run and debug button, you can run and debug your plugin perfectly.

After clicking the button, one Intellij IDEA client with your plugin installed will be opened. If you configured the runIde task as mentioned upside, the Android Studio you installed locally will be opened.

Use Cmd

Open the Gradle panel on the right side, you’ll see several tasks in the intellij group.

We can build the project through

./gradlew buildPlugin

We can find the artifact under directory /build/distributions.

We can run the project through

./gradlew runPlugin

More

Now, we can use Gradle to configure, build and run Android Studio plugin. About how to develop a plugin using Intellij Platform SDK, you can read Jetbrains’ documentation: Intellij Platform SDK. And I’ll write something about the interesting parts of the IDE and SDK.

Follow me to get updates!


Develop Android Studio’s Plugin Using Gradle was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Rex Tech

As an Android developer, I use Android Studio every day. When coding, I often think: Is it possible for us to make Android Studio more powerful? For example, can we pull the source code of a library module with one click and import it to the build? Can we support smarter code completion based on AI? Can we support custom domain-specific languages? In fact, the Android Studio plugin can turn all of the above ideas into reality. This article will detail how to use Gradle to develop Android Studio plugins. If you are also an Android developer who wants to improve productivity, then continue reading!

Preparation

The only tool that needs to be prepared to develop Android Studio plugins is Intellij IDEA (both Ultimate Edition and Community Edition are OK). You can download the latest version of Intellij IDEA from the official website of Jetbrains. This article will use Intellij IDEA Community Edition 2021.2 as a demonstration.

Create Project

Open Intellij IDEA, follow the steps below:

  1. Select File - New - Project...

2. On the New Project page, select the Gradle tab and make sure Java and Intellij Platform Plugin are selected. Click Next.

3. Enter the project's name. Click Finish.

Configure Project

Then Intellij IDEA will create an Intellij Plugin Project using Gradle to build. Most configurations are located in the build.gradle file under the root directory.

As you can see, here's a Gradle plugin called org.jetbrains.intellij. It provides lots of configurations for developers to customize their plugins. You can see all of the configurations here.

intellij

Intellij's version

In build.gradle generated by Intellij IDEA, Intellij's version is the same as the Intellij IDEA which created this project. But Android Studio's Intellij version is always different. We can see the Intellij version through Android Studio - About Android Studio:

The Intellij information of Android Studio shown upside is

Build #AI-203.7717.56.2031.7583922, built on July 27, 2021

So the corresponding Intellij version is 203.7717.56. As we all know that Android Studio is based on the Community Edition of Intellij IDEA, so we need to set the type field to IC:


intellij {
version = ‘203.7717.56’
type = ‘IC’
}

add android dependency

Actually, Android Studio adds android specific features through the android Intellij Plugin. If our plugin wants to use the APIs provided by Android Studio, we need to let our plugin depend on the android plugin:

 intellij { 
    version = ‘203.7717.56’ 
    type = ‘IC’ 
    plugins = [‘android’]
 } 

runIde

The Intellij Gradle plugin provides a task called runIde to help developers debug their plugins in the IDE. Android Developers can set the ideDir to the path of local installed Android Studio:

runIde { 
    ideDir = project.file(‘/Applications/Android Studio.app/Contents’) 
} 

Note: the value of ideDir needs to be File type which Jetbrains didn't mention in their documentation.

plugin.xml

By default, different plugins have different ClassLoaders. So plugins can use different versions of the same libraries and don't affect each other. But when we want to use APIs provided by the android plugin to get some Android-specific information(like the version of the android gradle plugin), we'll fail because of this ClassLoader mechanism. Developers can use depends on API provided by plugin.xml to make their plugins use the same ClassLoader as the android plugin.

<idea-plugin> 
<depends>org.jetbrains.android</depends> 
</idea-plugin>

In this way, we can get the proper data at runtime.

Build and Run

Use GUI

Intellij IDEA will create Run Configuration called Plugin automatically:

Just click the run and debug button, you can run and debug your plugin perfectly.

After clicking the button, one Intellij IDEA client with your plugin installed will be opened. If you configured the runIde task as mentioned upside, the Android Studio you installed locally will be opened.

Use Cmd

Open the Gradle panel on the right side, you'll see several tasks in the intellij group.

We can build the project through

./gradlew buildPlugin

We can find the artifact under directory /build/distributions.

We can run the project through

./gradlew runPlugin

More

Now, we can use Gradle to configure, build and run Android Studio plugin. About how to develop a plugin using Intellij Platform SDK, you can read Jetbrains' documentation: Intellij Platform SDK. And I'll write something about the interesting parts of the IDE and SDK.

Follow me to get updates!


Develop Android Studio's Plugin Using Gradle was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Rex Tech


Print Share Comment Cite Upload Translate Updates
APA

Rex Tech | Sciencx (2021-08-17T18:36:01+00:00) Develop Android Studio’s Plugin Using Gradle. Retrieved from https://www.scien.cx/2021/08/17/develop-android-studios-plugin-using-gradle/

MLA
" » Develop Android Studio’s Plugin Using Gradle." Rex Tech | Sciencx - Tuesday August 17, 2021, https://www.scien.cx/2021/08/17/develop-android-studios-plugin-using-gradle/
HARVARD
Rex Tech | Sciencx Tuesday August 17, 2021 » Develop Android Studio’s Plugin Using Gradle., viewed ,<https://www.scien.cx/2021/08/17/develop-android-studios-plugin-using-gradle/>
VANCOUVER
Rex Tech | Sciencx - » Develop Android Studio’s Plugin Using Gradle. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/17/develop-android-studios-plugin-using-gradle/
CHICAGO
" » Develop Android Studio’s Plugin Using Gradle." Rex Tech | Sciencx - Accessed . https://www.scien.cx/2021/08/17/develop-android-studios-plugin-using-gradle/
IEEE
" » Develop Android Studio’s Plugin Using Gradle." Rex Tech | Sciencx [Online]. Available: https://www.scien.cx/2021/08/17/develop-android-studios-plugin-using-gradle/. [Accessed: ]
rf:citation
» Develop Android Studio’s Plugin Using Gradle | Rex Tech | Sciencx | https://www.scien.cx/2021/08/17/develop-android-studios-plugin-using-gradle/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.