Boost your productivity by developing your own Android Studio plugin
Developing an Android app is most times fun, but sometimes it is just a repetitive work, where you do the same tasks over and over again. This is the not so much fun part of developing apps.
For the next 5-10 minutes I will guide you through the different use cases of my plugin and how each and every one of them can boost your productivity while you are coding.
Let’s start with a simple question. If you are using Google APIs, how often do you have encountered such a screen?
You need your certificate fingerprint every time you want to enable a new API. One solution would be to export the certificate fingerprint and store it somewhere on your hard drive. But if you actually need it, you have to search the file on your hard disk. Because of the struggle I had every time I needed my certificate fingerprint, I decided to write my own IntelliJ/Android Studio plugin, in order to retrieve the fingerprint directly in the IDE.
Shortly after my decision I realized that there are a lot of small reoccurring tasks, where I had to leave the IDE and I got distracted from my actual work. Here are some examples which really consumed my time: extract fingerprint information from certificates, connect to Android wear (a.k.a. Wear OS by Google) devices, add Gradle dependencies to a new project, capture the screen for a live demo or simply search for code fragments already written in other projects.
Plugin structure
Hint: I do not focus on the source code. There are only small pieces of code written in Kotlin. The UI is based on plain old Swing.
Because I was confronted with a heterogenous amount of tasks I tried to group them together. Therefore I came up with an the concept of Worker classes. Functionality is grouped into Worker classes. Each worker instance has its own UI and manages its own objects.
abstract class Worker {
protected var rootPanel: JPanel = JPanel()
val tabPanel: JPanel
get() {
initializePanel()
return rootPanel
}
abstract val title: String
abstract val icon: Icon
abstract fun initializePanel()
}
Worker instances can be easily managed by the plugin, and every worker has its own tab in the tab bar. Functionality is clearly separated between tabs.
Android utilites
The first part of functionality focuses on Androdi specific tasks.
The AndroidWorker class heavily utilizes the adb tool. Usually you need those two lines of code to connect to a Wear smart watch.
adb forward tcp:5353 localabstract:/adb-hub
adb connect 127.0.0.1:5353
I guess most people cannot recall those lines if they need it. So I decided to incorporate some of the adb tool functionality into my plugin.
Now I’m able to connect to my wear smartwatch by clicking on “Connect wear”. The plugin let’s me debug my (initially USB-connected) device via Wifi. It also allows me to extract the IP address of the connected device (without going to the settings screen on the device).
Next to this, the AndroidWorker allows me to extract the fingerprints of my debug certificate and my custom app certificate.
Those described actions are accessible with a single click without leaving Android Studio. By now you should have a pretty good impression how a custom plugin can boost your development performance.
Workspace Crawler
The Android utilities are quite handy, but there are some other goodies along the way.
I’m managing multiple projects, and sometimes it is not feasible to introduce a common library (be it because of project level dependencies, or of the to specific use case). My workspace contains over 2000 Java and Kotlin source files, but nevertheless I have a pretty good knowledge where I’ve implemented some code snippets. In my pre-plugin time I had to open the file explorer and navigate to the project and find it in the right package. Now I can focus on my code, while the WorkspaceCrawlerWorker finds it in my workspace.
The WorkspaceCrawler lets you search for a key word, open the found files, show them in the file explorer or directly copy it to your current project.
Gradle dependency management
The GradleWorker and the WorkspaceCrawlerWorker are come quite handy when you are creating a new project and want to copy code and dependencies from your previous work.
Back then in 2013, the Gradle build tool was one of the main reasons for me to migrate from Eclipse to Android Studio. But sometimes managing your dependencies and keep them up-to-date can be a real hassle.
Therefore I developed a nice user interface and I can add new dependencies by clicking on them. The statements are directly injected into the builld.gradle file. Adding all Dagger (incl. AppComponent/AppModule class stubs) or all Retrofit dependencies is now a task of clicking a button. The GradleWorker provides the ability to add external repositories to your build files as well.
Screen recording
The problem of live demos is the fact that they are live. And you have to somehow set them up for a presentation. I prefer videos, as they do not interrupt your presentation flow. Luckily adb comes with the possibility to record your screen.
The ScreenCaptureWorker lets you easily start and stop the screen recording and pulls the file directly onto your hard drive. As before, entering adb commands via cli is now abstracted— guess by what — by the click of a button.
It’s interesting, right?
If you are interested by now in developing your own plugin, you can take a look at my code on Github (if you need a reference to get started) or read through the official docs.