Class diagrams in android projects

Denis Rebrov
4 min readSep 27, 2021

UML class diagrams are a great tool for visualizing the internal structure of a software system. Such a diagram shows classes, their methods and attributes, as well as the relationships between them — dependencies, inheritance and aggregation.

Diagrams can in many cases increase the speed and efficiency of both the creation of new features and the analysis and refactoring of existing ones, and simplifies modeling.

Let’s try to apply this tool to an Android project!

You can run Android Studio, open Action Search, write “Class Diagram”, and then … “No Actions found” 🙃

Unfortunately, Android Studio does not support the generation of class diagrams out of the box, which forces us to choose from the following options:

  • Use plugins or third party programs such as CodeIris or PlantUML.
  • Use class diagrams from Intellij Idea Ultimate.

The second solution has several advantages:

  • Class diagrams can be opened in the IDE window — this makes refactoring and navigation easier.
  • More convenient and functional than most other solutions (in my personal opinion)

Setup

Install Intellij Idea Ultimate, open an android project and wait for import and gradle sync to finish.

If you just run the diagram generation via RMB on the module directory in the Project → Diagrams → Show Diagram → Java Classes window, you will get something like this:

There are no dependencies here, and there may be many blocks, such as DI modules, that you probably want to exclude from the diagram.

To fix that, you should create a custom file scope in the project (Intellij / AndroidStudio Scope, not to be confused with Dagger’s scopes), which will include the necessary files with classes.

How to add custom scope

  1. File → Settings → Appearance & Behavior → Scopes

2. Create a new scope, and add a pattern string according to which files will be included / excluded from the scope.

Pattern example: (com.foo..*||com.bar..*)&&!test:*..*&&!file:*Module**

This string restricts scope to include only source files from packages “com.foo” & “com.bar”, which are not tests and do not have the word “Module” in their name.

Read more about scope patterns here

3. When creating a scope, it is convenient to check its correctness by the number of files included in it and the colors of files and folders in the hierarchy.

4. Do not forget to click “Apply” — otherwise the scope won’t be created!

After creating the scope, we can generate a diagram with the classes we need.

  1. RMB on the directory with the resources (not on the module — otherwise the scope for some unknown reason will not be applied correctly) → Diagrams → Show Diagram → Java Classes
  2. Click on the filter icon at the top, and select created scope.

3. Select the link icon (before plus) to display dependencies in the diagram.

Ready!

The result should be something like this:

Class Diagram Usage Tips

  • Use it to familiarize yourself with unfamiliar project modules in which you need to make changes, so you can find the classes and their dependencies you need faster.
  • The diagram is generated by default with dependencies directed from bottom to top. Based on this, you can quickly identify components that violate the Dependency Rule.
  • When using it in the refactoring of medium and large modules, do not start working from the general diagram — refactor the individual submodules / layers / folders first.

P.S.

This is my first story on medium and also the first story in English :)
I would be glad if in the comments you point out my mistakes both in the language and in the content.

--

--