Building Custom Android Studio Plugins to Automate Code Generation
By walking through the creation of a custom Android Studio (IntelliJ) plugin that reads protobuf files and auto‑generates Service and Presenter classes, the author shows how VFS, PSI, and project‑model APIs can replace tedious manual coding, delivering roughly 5.5% time savings per requirement.
This article explains how to develop custom IntelliJ/Android Studio plugins to automate repetitive coding tasks, specifically demonstrating how to create a plugin that generates network request code from protobuf (PB) files.
The author, a senior mobile client development engineer at Tencent, shares their experience building a plugin that automatically generates Service and Presenter files from PB files, eliminating tedious manual coding work.
What are Android Studio Plugins?
Android Studio plugins are actually IntelliJ plugins. Android Studio is essentially IntelliJ with the Android plugin pre-installed. These plugins can significantly improve development efficiency - examples include translation plugins, code search, refactoring tools, and XML-to-adapter code generators.
The Problem and Solution
The author encountered a pain point: for each new business requirement, the backend provides PB files containing API definitions, and the frontend needs to manually write Service classes and request methods - a repetitive and tedious process. After considering alternatives (asking backend to provide JAR packages, using code generation libraries like JavaPoet), the author decided to build a custom Android Studio plugin.
Benefits Achieved
After implementing the plugin, the author achieved approximately 5.5% time savings per requirement:
Development efficiency significantly improved - manual coding replaced with one-click generation
Unified naming conventions for Service and Presenter files
Avoided duplicate work across different developers
Reduced parameter type errors that caused debugging time
Key Technical Concepts
The article covers essential IntelliJ plugin development concepts:
VFS (Virtual File System) : Provides a unified API for file operations regardless of storage location. Important: file write operations must be performed in asynchronous threads to avoid blocking the main thread.
PSI (Program Structure Interface) : A core concept in IntelliJ Platform where directories, packages, source files, and resources are abstracted as PSI objects. This enables convenient APIs for adding methods and fields to existing classes without low-level file operations.
Project Model : Provides APIs for handling project structure.
Development Steps
Create a plugin project following JetBrains' official guide
Create an Action (plugin entry point)
Write the actual code implementation
Perform requirement and feasibility analysis
Conduct technical research
Build and package the plugin
The article provides links to official JetBrains documentation and additional learning resources including the TranslationPlugin source code on GitHub.
Tencent Cloud Developer
Official Tencent Cloud community account that brings together developers, shares practical tech insights, and fosters an influential tech exchange community.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.