Upgrade to Pro — share decks privately, control downloads, hide ads and more …

From Idea to IDE: Developing Plugins for Androi...

Ahmed Ali
September 12, 2024

From Idea to IDE: Developing Plugins for Android Studio

Android Studio allows developers to add custom features to make the IDE work better for their needs. In this talk, we'll look at how to use the Project Structure Interface (PSI) to create plugins that improve Android Studio and make development faster. We'll explain the basics of the Plugin SDK and show how Jetpack Compose can help build easy-to-use interfaces for these plugins. Attendees will learn about different kinds of plugins they can create and how these can help them work more efficiently in Android Studio. We'll also share examples of useful plugins to give you ideas and practical tips.

Ahmed Ali

September 12, 2024
Tweet

Other Decks in Programming

Transcript

  1. neverstop Developing Plugins for Android Studio AHMED ALI ANDROID ENGINEER

    SEP 2024 All code examples are hosted here GITHUB
  2. • Add a button to the context menu • Read

    the class code • Send to Gemini • Write docs What we are going to build!
  3. Actions To register a button in the context menu Let's

    discuss what we need to build it!
  4. Actions To register a button in the context menu Let's

    discuss what we need to build it! PSI To analyze di ff erent coding elements, and read the body
  5. Actions To register a button in the context menu The

    platform way of handling Dependency Injection Let's discuss what we need to build it! PSI To analyze di ff erent coding elements, and read the body Services
  6. Actions To register a button in the context menu The

    platform way of handling Dependency Injection Let's discuss what we need to build it! PSI To analyze di ff erent coding elements, and read the body Services Running the API calls in the background and doing updates in the UI Threading Model
  7. • Install IntelliJ community • Install plugin DevKit • Use

    Latest versions Creating a Plugin Gradle Project
  8. Creating a Plugin Gradle Project • Familair Gradle structure •

    build.gradle.kts • gradle.properties • settings.gradle.kts
  9. Creating a Plugin Gradle Project • Familair Gradle structure •

    build.gradle.kts • gradle.properties • settings.gradle.kts • META-INF/plugin.xml?
  10. Actions To register a button in the context menu The

    platform way of handling Dependency Injection PSI To analyze di ff erent coding elements, and read the body Services Running the API calls in the background and doing updates in the UI Threading Model
  11. Actions • Actions are key to adding interactive components in

    the IDE. • Add items to the platform menus and toolbars • Example: File | Open File... menu item and the Open... toolbar button.
  12. How to create an action • Create a class that

    extends the AnAction class. • Use the ‘Register Action’ option in IntelliJ.
  13. How to create an action • update() Function: • Manages

    visibility and enablement state. • De fi nes logic for when and where the action should be available. • Example: Only show the action when right- clicking on classes or functions.
  14. How to create an action • actionPerformed() Function: • Executes

    code when the action is triggered. • De fi nes what happens when the user clicks on the action.
  15. How to create an action • getActionUpdateThread() Function (introduced in

    2023): • Speci fi es the thread for the action to run on • EDT or BGT
  16. Choosing the Right Thread • Use the Main thread for

    most actions involving UI updates. • Use BGT for actions interacting with PSI elements or the file system. • If UI updates are needed after a BGT operation, switch back to the Main thread.
  17. Actions To register a button in the context menu The

    platform way of handling Dependency Injection PSI To analyze di ff erent coding elements, and read the body Services Running the API calls in the background and doing updates in the UI Threading Model
  18. PSI • PSI is a key layer within the IntelliJ

    platform. • Handles parsing files and creating syntactic and semantic code models(AST).
  19. PSI • PSI is a key layer within the IntelliJ

    platform. • Handles parsing files and creating syntactic and semantic code models. • Simplifies identifying code elements like functions, classes, etc.
  20. PSI Files • Root structure representing the content of a

    file. • Acts as the entry point for understanding the file’s structure. • PsiFile is the base class for all PSI files. • Example: PsiJavaFile for Java files, KtFile for Kotlin.
  21. PSI Elements • Building blocks representing parts of source code

    structure. • Includes everything from whitespace to classes and functions. • Provided by the IntelliJ platform for detailed code exploration.
  22. Notice something? This is how you add support for custom

    language, by implementing your own suite of PsiElements and PsiFiles
  23. Actions To register a button in the context menu The

    platform way of handling Dependency Injection PSI To analyze di ff erent coding elements, and read the body Services Running the API calls in the background and doing updates in the UI Threading Model
  24. Services • Plugins DI • A service is a class

    that can be loaded on demand • Each service has only one instance, making it a singleton
  25. Service Level 01 Application Application Level Global Singlton • Works

    across the entire IDE’s lifecycle. • Covers the whole scope of the application. 02 Project Project spec fi c • Speci fi c to individual projects. • Each project has its own instance of the service.
  26. Service Type 01 Light Private • Private to your plugin

    • Simple to setup 02 Non-Light Exposed • Exposed to other plugins • Needs to be added to plugin.xml fi le
  27. Service Light services • Create a regular class and annotate

    it with @Service. • Include a Project parameter for project- level services, automatically injected by IntelliJ.
  28. Service retrieving • Application-Level Service: Use getService() method. • Project-Level

    Service: Use getService() within a project context. • For dependent services, Don’t use constructor injection.
  29. Actions To register a button in the context menu The

    platform way of handling Dependency Injection PSI To analyze di ff erent coding elements, and read the body Services Running the API calls in the background and doing updates in the UI Threading Model
  30. Threading Model • Also known as the UI thread. •

    Handles UI events, such as button clicks and interface updates. • Used for writing data operations as well. Event Dispatch Thread (EDT)
  31. Threading Model • Also known as the UI thread. •

    Handles UI events, such as button clicks and interface updates. • Used for writing data operations as well. Event Dispatch Thread (EDT) Background Thread (BGT) • Handles long-running and costly operations. • Ideal for tasks like fi le processing, network calls, and intensive computations.
  32. Coroutines • Lightweight and easy-to-implement alternative to traditional threads. •

    Recommended by IntelliJ for plugin development starting in 2024. • Familiar to Android developers.
  33. Dispatchers • Default, IO,Unconfined, EDT, and Main dispatchers. • Main

    and EDT are the same • Use EDT dispatcher for UI- related tasks, as recommended.
  34. Scopes • Multiple scopes available, including application and project scopes

    - Deprecated . • Service scopes are preferred for their efficiency and lifecycle management.
  35. Read and write locks • Crucial for managing read/write operations

    in asynchronous environments. • Writing Allowing Read Action (WARA): Prioritizes writes; read actions are canceled and retried. • Writing Blocking Read Action (WBRA): Prioritizes reads; write actions are blocked until reads complete.
  36. Actions To register a button in the context menu The

    platform way of handling Dependency Injection PSI To analyze di ff erent coding elements, and read the body Services Running the API calls in the background and doing updates in the UI Threading Model
  37. UI

  38. UI kotlin UI dsl • Declartive UI toolkit • Binding

    to a state • Used for panels and dialogs