Slide 1

Slide 1 text

neverstop Developing Plugins for Android Studio AHMED ALI ANDROID ENGINEER SEP 2024 All code examples are hosted here GITHUB

Slide 2

Slide 2 text

Ahmed Ali neverstop ANDROID ENGINEER BASED IN CAIRO @INFINUM

Slide 3

Slide 3 text

• Add a button to the context menu • Read the class code • Send to Gemini • Write docs What we are going to build!

Slide 4

Slide 4 text

Let's discuss what we need to build it!

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Plugin Types:

Slide 10

Slide 10 text

Plugin Types: Custom Framework Integration

Slide 11

Slide 11 text

Plugin Types: Custom Framework Integration Custom Language Support

Slide 12

Slide 12 text

Plugin Types: Custom Language Support Tool Integration Custom Framework Integration

Slide 13

Slide 13 text

Plugin Types: Tool Integration Theme Changes Custom Language Support Custom Framework Integration

Slide 14

Slide 14 text

Plugin Types: Theme Changes User interface add-ons Tool Integration Custom Language Support Custom Framework Integration

Slide 15

Slide 15 text

Creating a Plugin Gradle Project

Slide 16

Slide 16 text

• Install IntelliJ community • Install plugin DevKit • Use Latest versions Creating a Plugin Gradle Project

Slide 17

Slide 17 text

Creating a Plugin Gradle Project

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

Creating a Plugin Gradle Project • Familair Gradle structure • build.gradle.kts • gradle.properties • settings.gradle.kts • META-INF/plugin.xml?

Slide 20

Slide 20 text

META-INF/plugin.xml • Very much like AndroidManifest.xml • Declare several components • Share componentes with other plugins

Slide 21

Slide 21 text

Plugin config

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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.

Slide 24

Slide 24 text

How to create an action • Create a class that extends the AnAction class.

Slide 25

Slide 25 text

How to create an action • Create a class that extends the AnAction class. • Use the ‘Register Action’ option in IntelliJ.

Slide 26

Slide 26 text

How to create an action

Slide 27

Slide 27 text

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.

Slide 28

Slide 28 text

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.

Slide 29

Slide 29 text

How to create an action • getActionUpdateThread() Function (introduced in 2023): • Speci fi es the thread for the action to run on • EDT or BGT

Slide 30

Slide 30 text

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.

Slide 31

Slide 31 text

Example

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

PSI • PSI is a key layer within the IntelliJ platform.

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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.

Slide 36

Slide 36 text

PSI • PSI Files • PSI Elements

Slide 37

Slide 37 text

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.

Slide 38

Slide 38 text

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.

Slide 39

Slide 39 text

Example

Slide 40

Slide 40 text

Notice something?

Slide 41

Slide 41 text

Notice something? This is how you add support for custom language, by implementing your own suite of PsiElements and PsiFiles

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

Services • Plugins DI • A service is a class that can be loaded on demand • Each service has only one instance, making it a singleton

Slide 44

Slide 44 text

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.

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

Service Light services • Create a regular class and annotate it with @Service. • Include a Project parameter for project- level services, automatically injected by IntelliJ.

Slide 47

Slide 47 text

Service non-light services • No need to use @Service • Needs to be registerd in plugin.xml

Slide 48

Slide 48 text

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.

Slide 49

Slide 49 text

Example

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

Threading Model

Slide 52

Slide 52 text

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)

Slide 53

Slide 53 text

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.

Slide 54

Slide 54 text

Coroutines • Lightweight and easy-to-implement alternative to traditional threads. • Recommended by IntelliJ for plugin development starting in 2024. • Familiar to Android developers.

Slide 55

Slide 55 text

Coroutines Dispatchers Scopes Read and Write Locks

Slide 56

Slide 56 text

Dispatchers • Default, IO,Unconfined, EDT, and Main dispatchers. • Main and EDT are the same • Use EDT dispatcher for UI- related tasks, as recommended.

Slide 57

Slide 57 text

Scopes • Multiple scopes available, including application and project scopes - Deprecated . • Service scopes are preferred for their efficiency and lifecycle management.

Slide 58

Slide 58 text

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.

Slide 59

Slide 59 text

Example

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

Usecases

Slide 62

Slide 62 text

UI

Slide 63

Slide 63 text

UI • Swing • Kotlin UI dsl • Compose?

Slide 64

Slide 64 text

UI kotlin UI dsl • Declartive UI toolkit • Binding to a state • Used for panels and dialogs

Slide 65

Slide 65 text

UI Compose • Not officially supported • You have to use Jewel • Check this talk

Slide 66

Slide 66 text

How to learn more about plugin dev? Docs IntelliJ-community source code Slack community

Slide 67

Slide 67 text

in fi num.com neverstop Thank you!