You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在Android Studio中创建UML图?(旧生成器已失效)

Hey there! I feel your pain—so many old UML generator plugins for Android Studio are abandoned and just don't work with the latest IDE builds anymore. Let's break down the most reliable, up-to-date methods to create UML diagrams right in your project:

1. Use Android Studio's Built-in Diagram Tool (Zero Plugins Required)

This is my go-to method because it's official, maintained by JetBrains, and works with every recent Android Studio version. Here's how to use it:

  • Open any class, interface, or enum in your project
  • Right-click inside the editor → hover over Diagrams → select Show Diagram
  • A new tab will open with a basic UML class diagram for your selected file
  • To add related classes (like superclasses, dependencies), right-click the diagram → Add Class → pick the files you want to include
  • You can rearrange elements, adjust the layout, and even export the diagram as a PNG or SVG via the toolbar buttons at the top of the diagram tab

Pro tip: Double-click any class in the diagram to jump straight to its code—super handy for navigating your project!

2. Install the PlantUML Integration Plugin (Active & Well-Maintained)

If you need more flexibility (like creating sequence diagrams, use case diagrams, or custom UML layouts), the PlantUML Integration plugin is still regularly updated and works great with modern Android Studio. Here's how to set it up:

  • Go to File → Settings → Plugins (on macOS, it's Android Studio → Settings → Plugins)
  • Search for "PlantUML Integration" in the Marketplace tab
  • Install the plugin and restart Android Studio
  • To create a new UML diagram: Right-click your project folder → New → PlantUML File
  • Name your file (e.g., UserClassDiagram.puml) and start writing PlantUML syntax. For example, a simple class diagram with relationships:
class User {
  -String username
  -String email
  +void login(String password)
  +void logout()
}

class Order {
  -int orderId
  -Date orderDate
  +void calculateTotal()
}

User "1" --> "*" Order : places
  • The plugin will show a live preview of your diagram next to the code, and you can export it to multiple formats (PNG, SVG, PDF) when you're done.
3. Generate UML Diagrams via Gradle (For Batch Processing)

If you need to generate UML diagrams for your entire project or multiple classes at once, using a Gradle plugin is a great option. The plantuml-gradle-plugin is still maintained and works with modern Gradle versions. Here's a quick setup:

  • Add the plugin to your project-level build.gradle file:
plugins {
  id 'net.sourceforge.plantuml' version '1.2.1'
}
  • Configure the plugin in your module-level build.gradle to specify which classes to include and where to output the diagrams:
plantuml {
  sourceSets = [sourceSets.main]
  outputDir = file("$buildDir/reports/uml")
  format = 'svg'
}
  • Run the Gradle task generatePlantUml from the terminal or Gradle tool window—this will generate UML diagrams for all your project's classes and save them to the specified output directory.

Quick Notes to Avoid Headaches

  • Avoid any plugins with names like "UML Generator" or "Class Diagram Generator" that haven't been updated in 2+ years—they'll likely crash or not show up in the latest Android Studio.
  • For the built-in diagram tool, if you don't see the Diagrams option, make sure you're right-clicking inside the code editor (not the project tree).

内容的提问来源于stack exchange,提问作者user12954213

火山引擎 最新活动