# luoyacheng/legado-e architecture

> Architecture diagram of the GitHub repository luoyacheng/legado-e, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/luoyacheng/legado-e
- Repository: https://github.com/luoyacheng/legado-e

## Overview

1. **Overall shape**
   - This is an Android reading app, built mainly in Kotlin with a large single `app` module plus three supporting modules: `modules/book` for EPUB/UMD parsing, `modules/rhino` for JavaScript rule execution, and `modules/web` for the companion browser-based editor/admin UI.
   - The app is not “just UI”: a lot of product logic lives in reusable engine layers under `help/`, `model/`, `lib/`, and `web/`.

2. **Primary user-facing surfaces**
   - The main Android shell is `MainActivity` with tabbed fragments for bookshelf, explore, RSS, and personal/config areas.
   - From there, the app branches into specialized experiences: book search, reading, manga reader, audio read-aloud, video player, source editing/debugging, file management, and settings.

3. **Core domain model**
   - The persistent domain is centered on books, chapters, book sources, RSS sources, replace rules, dictionaries, HTTP TTS configs, bookmarks, read records, cache, and groups.
   - Rule objects are first-class: book source rules, RSS rules, content rules, search rules, TOC rules, and replace rules are stored as entities and moved through the UI, engines, and editor tools.

4. **Local data layer**
   - `AppDatabase` is a Room database with many DAOs and a long migration history, so schema evolution is a significant architectural concern.
   - The database holds both content metadata and operational state such as bookmarks, history, caches, cookies, search keywords, server configs, and source definitions.
   - JSON schema snapshots under `app/schemas` indicate Room auto-migration/verification is part of the build discipline.

5. **Rule-driven content pipeline**
   - The app’s most important boundary is between “user-defined rule source” and “normalized app model.”
   - Fetching a book/article goes through rule analyzers (`model/analyzeRule`) that can interpret HTML, XPath, regex, JSoup, and JSONPath rules.
   - The output is converted into app-specific models like book lists, chapter lists, content pages, and RSS articles for display and storage.

6. **Script/runtime layer**
   - `modules/rhino` embeds a Rhino JavaScript engine so user rules and source scripts can run inside the app safely.
   - This layer is used by source/login/debug workflows and by various rule-extension helpers in `help/js*` and `model/SharedJsScope`.
   - That makes scripting an architecture boundary, not just a utility: the app executes user-authored logic to adapt to arbitrary websites.

7. **Networking and fetch stack**
   - The app uses an HTTP abstraction centered on OkHttp, with optional Cronet integration for alternative networking behavior.
   - Cookie management, SSL handling, decompression, interceptors, and response wrappers live in `help/http` and related helpers.
   - WebView-backed interactions are also used for login, source verification, and in-app browsing; this is important because some content flows require browser-like execution rather than plain HTTP.

8. **Reading engines**
   - Text reading is implemented as a dedicated rendering pipeline under `ui/book/read/page`, with page factories, layout providers, and multiple page delegates for scroll, slide, simulation, cover, and no-animation modes.
   - Manga has a separate rendering path under `ui/book/manga`, with its own recycler/layout logic and image transformations.
   - Audio read-aloud and video playback are separate subsystems, each with dedicated services, activities, and player helpers.

9. **Local file ingestion**
   - Local book support is a distinct pipeline: TXT, EPUB, MOBI, PDF, and UMD parsing are handled through `model/localBook` plus the `modules/book` library.
   - Import flows scan storage, normalize files into books/chapters, and then hand them off to the bookshelf and reader layers.
   - This is a classic ingest → parse → persist → read flow, separate from online source-based reading.

10. **Remote and sync-like capabilities**
   - The app supports remote books and WebDAV-based storage/access through `model/remote` and `lib/webdav`.
   - Backup/restore, old-data import, and direct-link upload are adjacent but separate storage workflows.
   - These features connect local state to external file/network services without collapsing into the core reading pipeline.

11. **Embedded web server and web editors**
   - `app/web` contains a local HTTP/WebSocket server plus socket endpoints for source debugging and book search.
   - `modules/web` is a separate Vue-based front-end used for bookshelf/source editing views, implying the Android app can serve or synchronize with a browser UI.
   - This creates a clear split: Android native UI for end-user reading, web UI for power-user editing/debugging.

12. **Background services and receivers**
   - Services handle long-running work such as downloads, caching, source checks, exporting, HTTP/TTS read-aloud, audio playback, video playback, and web serving.
   - Broadcast receivers react to media buttons, connectivity changes, time/battery events, and shared-intent style entry points.
   - The app therefore has both foreground interactive flows and background orchestration around content, media, and network state.

13. **Configuration and customization**
   - User customization is extensive: themes, reading styles, source configs, backup configs, cover rules, TTS configs, and welcome/other preferences are all first-class.
   - Many dialog and preference helper classes in `lib/prefs`, `lib/theme`, and `ui/*/config` show that settings are part of the product’s core behavior, not a minor side screen.

14. **Non-UI infrastructure**
   - The repo includes many reusable helpers for caching, coroutines, lifecycle, permissions, crypto, image loading, file utilities, markdown/web rendering, and custom widgets.
   - Several third-party libraries materially shape the architecture: Room, OkHttp, Glide, ExoPlayer/GSY video tooling, NanoHTTPD, WebView, and epublib.
   - CI/CD under `.github/workflows` and release scripts support app release, testing, and a web build/deploy path, but the runtime architecture remains primarily Android-centric with embedded scripting and local server capabilities.

Main entry points: [`app/src/main/java/io/legado/app/model/analyzeRule`](https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/model/analyzeRule) (Rule pipeline), [`app/src/main/java/io/legado/app/ui/main/MainActivity.kt`](https://github.com/luoyacheng/legado-e/blob/main/app/src/main/java/io/legado/app/ui/main/MainActivity.kt) (Main shell), [`app/src/main/java/io/legado/app/data/AppDatabase.kt`](https://github.com/luoyacheng/legado-e/blob/main/app/src/main/java/io/legado/app/data/AppDatabase.kt) (App database), [`app/src/main/java/io/legado/app/data/entities/rule`](https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/data/entities/rule) (Rule model)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_app["Android app"]
  node_node_main_shell["Main shell<br/>android entry<br/>[MainActivity.kt]"]
  node_node_bookshelf["Bookshelf<br/>native UI"]
  node_node_explore_rss["Explore and RSS<br/>native UI"]
  node_node_reader_stack["Reader stack<br/>reading engine"]
  node_node_manga["Manga reader<br/>media reader"]
  node_node_audio_video["Audio and video<br/>playback"]
  node_node_source_tools["Source tools<br/>rule editor"]
  node_node_rule_models["Rule model<br/>domain rules"]
  node_node_rule_pipeline["Rule pipeline<br/>content analyzer"]
  node_node_storage[("App database<br/>room database<br/>[AppDatabase.kt]")]
  node_node_http["HTTP stack<br/>networking"]
  node_node_webview["WebView layer<br/>browser bridge"]
  node_node_web_server["Embedded web<br/>local server"]
  node_node_storage_workflows["Import and sync<br/>storage workflows"]
  node_node_services["Background services<br/>android services"]
  node_node_config["Config system<br/>preferences"]
  node_node_shared_helpers["Shared helpers<br/>infrastructure"]
end

subgraph group_group_modules["Supporting modules"]
  node_node_book_module["Book module<br/>epub/umd library"]
  node_node_rhino_module["Rhino module<br/>js runtime"]
  node_node_web_module["Web module<br/>vue frontend"]
end

node_node_main_shell -->|"opens"| node_node_bookshelf
node_node_main_shell -->|"routes to"| node_node_explore_rss
node_node_main_shell -->|"launches"| node_node_reader_stack
node_node_main_shell -->|"launches"| node_node_manga
node_node_main_shell -->|"launches"| node_node_audio_video
node_node_bookshelf -->|"persists"| node_node_storage
node_node_explore_rss -->|"queries"| node_node_rule_pipeline
node_node_source_tools -->|"edits"| node_node_rule_models
node_node_source_tools -->|"debugs"| node_node_rule_pipeline
node_node_rule_models -->|"stored in"| node_node_storage
node_node_rule_pipeline -->|"fetches via"| node_node_http
node_node_rule_pipeline -->|"falls back to"| node_node_webview
node_node_rule_pipeline -->|"executes in"| node_node_rhino_module
node_node_rule_pipeline -->|"uses"| node_node_shared_helpers
node_node_storage_workflows -->|"parses with"| node_node_book_module
node_node_storage_workflows -->|"persists"| node_node_storage
node_node_reader_stack -->|"reads"| node_node_storage
node_node_audio_video -->|"runs through"| node_node_services
node_node_web_server -->|"serves"| node_node_web_module
node_node_web_server -->|"exposes"| node_node_rule_pipeline
node_node_services -->|"fetches via"| node_node_http
node_node_config -->|"stores"| node_node_storage
node_node_config -->|"uses"| node_node_shared_helpers
node_node_web_module -->|"edits"| node_node_rule_models

click node_node_main_shell "https://github.com/luoyacheng/legado-e/blob/main/app/src/main/java/io/legado/app/ui/main/MainActivity.kt"
click node_node_bookshelf "https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/ui/main/bookshelf"
click node_node_explore_rss "https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/ui/main/explore"
click node_node_reader_stack "https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/ui/book/read"
click node_node_manga "https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/ui/book/manga"
click node_node_audio_video "https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/ui/book/audio"
click node_node_source_tools "https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/ui/book/source"
click node_node_rule_models "https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/data/entities/rule"
click node_node_rule_pipeline "https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/model/analyzeRule"
click node_node_storage "https://github.com/luoyacheng/legado-e/blob/main/app/src/main/java/io/legado/app/data/AppDatabase.kt"
click node_node_http "https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/help/http"
click node_node_webview "https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/help/webView"
click node_node_web_server "https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/web"
click node_node_storage_workflows "https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/model/localBook"
click node_node_services "https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/service"
click node_node_config "https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/ui/config"
click node_node_shared_helpers "https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/help"
click node_node_book_module "https://github.com/luoyacheng/legado-e/tree/main/modules/book/src/main/java/me/ag2s/epublib"
click node_node_rhino_module "https://github.com/luoyacheng/legado-e/tree/main/modules/rhino/src/main/java/com/script"
click node_node_web_module "https://github.com/luoyacheng/legado-e/tree/main/modules/web/src"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_node_main_shell,node_node_bookshelf,node_node_explore_rss,node_node_reader_stack,node_node_manga,node_node_audio_video,node_node_source_tools,node_node_rule_models,node_node_rule_pipeline,node_node_storage,node_node_http,node_node_webview,node_node_web_server,node_node_storage_workflows,node_node_services,node_node_config,node_node_shared_helpers toneBlue
class node_node_book_module,node_node_rhino_module,node_node_web_module toneAmber
```

## Components

### Android app

Native Android application shell, UI, data, services, and embedded web/server layers.

- [Main shell](https://github.com/luoyacheng/legado-e/blob/main/app/src/main/java/io/legado/app/ui/main/MainActivity.kt): android entry · App launch, bottom tabs, and top-level navigation across bookshelf, explore, RSS, and personal areas. · `app/src/main/java/io/legado/app/ui/main/MainActivity.kt`
- [Bookshelf](https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/ui/main/bookshelf): native UI · Bookshelf home and book collection presentation, including alternate shelf styles. · `app/src/main/java/io/legado/app/ui/main/bookshelf`
- [Explore and RSS](https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/ui/main/explore): native UI · Discovery and feed surfaces for exploring sources and reading RSS content. · `app/src/main/java/io/legado/app/ui/main/explore`
- [Reader stack](https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/ui/book/read): reading engine · Text reading pipeline with page layout, chapter flow, and reading controls. · `app/src/main/java/io/legado/app/ui/book/read`
- [Manga reader](https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/ui/book/manga): media reader · Separate image-based reading path with custom recycler, transforms, and chapter handling. · `app/src/main/java/io/legado/app/ui/book/manga`
- [Audio and video](https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/ui/book/audio): playback · Read-aloud, audio playback, and video player subsystems with dedicated activities and services. · `app/src/main/java/io/legado/app/ui/book/audio`
- [Source tools](https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/ui/book/source): rule editor · Editing, debugging, login, and verification workflows for book and RSS rules. · `app/src/main/java/io/legado/app/ui/book/source`
- [Rule model](https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/data/entities/rule): domain rules · First-class rule entities for book sources, RSS sources, content, search, TOC, replace, dict, and TTS configs. · `app/src/main/java/io/legado/app/data/entities/rule`
- [Rule pipeline](https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/model/analyzeRule): content analyzer · Normalizes user-defined rules into app models via HTML, XPath, regex, JSoup, and JSONPath analyzers. · `app/src/main/java/io/legado/app/model/analyzeRule`
- [App database](https://github.com/luoyacheng/legado-e/blob/main/app/src/main/java/io/legado/app/data/AppDatabase.kt): room database · Room database, DAOs, entities, and migrations for books, sources, bookmarks, history, caches, and settings state. · `app/src/main/java/io/legado/app/data/AppDatabase.kt`
- [HTTP stack](https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/help/http): networking · OkHttp-centered fetch layer with cookie, SSL, decompression, interceptor, and Cronet support. · `app/src/main/java/io/legado/app/help/http`
- [WebView layer](https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/help/webView): browser bridge · WebView-backed login, browsing, and scripted browser-like interactions used where plain HTTP is insufficient. · `app/src/main/java/io/legado/app/help/webView`
- [Embedded web](https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/web): local server · Local HTTP/WebSocket server exposing search and source-debug endpoints and serving the companion UI. · `app/src/main/java/io/legado/app/web`
- [Import and sync](https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/model/localBook): storage workflows · Local book ingest, remote/WebDAV books, backup/restore, and data migration workflows. · `app/src/main/java/io/legado/app/model/localBook`
- [Background services](https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/service): android services · Long-running services for downloads, caching, source checks, export, read-aloud, playback, and web serving. · `app/src/main/java/io/legado/app/service`
- [Config system](https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/ui/config): preferences · Themes, reading style, source settings, backup, cover rules, TTS, and welcome/configuration screens. · `app/src/main/java/io/legado/app/ui/config`
- [Shared helpers](https://github.com/luoyacheng/legado-e/tree/main/app/src/main/java/io/legado/app/help): infrastructure · Cross-cutting helpers for caching, coroutines, crypto, permissions, theming, image loading, file utilities, and custom widgets. · `app/src/main/java/io/legado/app/help`

### Supporting modules

Reusable EPUB/UMD parsing, JavaScript runtime, and browser editor module.

- [Book module](https://github.com/luoyacheng/legado-e/tree/main/modules/book/src/main/java/me/ag2s/epublib): epub/umd library · Standalone EPUB and UMD parsing/writing library used for local book ingestion and document handling. · `modules/book/src/main/java/me/ag2s/epublib`
- [Rhino module](https://github.com/luoyacheng/legado-e/tree/main/modules/rhino/src/main/java/com/script): js runtime · Embedded Rhino JavaScript runtime used to execute user-authored source rules and helpers safely. · `modules/rhino/src/main/java/com/script`
- [Web module](https://github.com/luoyacheng/legado-e/tree/main/modules/web/src): vue frontend · Browser-based bookshelf and source editor/admin UI synchronized with the Android app. · `modules/web/src`

## Connections

- Main shell → Bookshelf: opens · Main navigation enters the bookshelf surface.
- Main shell → Explore and RSS: routes to · Main navigation switches to exploration and RSS areas.
- Main shell → Reader stack: launches · From collections or search results into the text reader.
- Main shell → Manga reader: launches · Opens manga reading when the content type requires it.
- Main shell → Audio and video: launches · Starts audio read-aloud or video playback experiences.
- Bookshelf → App database: persists · Bookshelf state and book metadata are read from Room.
- Explore and RSS → Rule pipeline: queries · Discovery and feed screens resolve source rules into content.
- Source tools → Rule model: edits · Source editors manipulate stored rule entities.
- Source tools → Rule pipeline: debugs · Debug and login flows execute rules against live targets.
- Rule model → App database: stored in · Rule entities and operational state live in the database.
- Rule pipeline → HTTP stack: fetches via · Analyzers rely on the network stack for page retrieval.
- Rule pipeline → WebView layer: falls back to · Browser-based execution supports login and verification flows.
- Rule pipeline → Rhino module: executes in · User-authored JavaScript rules run in the embedded Rhino runtime.
- Rule pipeline → Shared helpers: uses · Parsing and rule helpers are shared across the app.
- Import and sync → Book module: parses with · Local book import relies on EPUB/UMD library code.
- Import and sync → App database: persists · Imported books, remote book state, and backup data are stored locally.
- Reader stack → App database: reads · Reader state, bookmarks, history, and chapter data come from Room.
- Audio and video → Background services: runs through · Playback and read-aloud depend on long-running services.
- Embedded web → Web module: serves · The embedded server exposes the companion browser UI.
- Embedded web → Rule pipeline: exposes · WebSocket endpoints drive source and search debugging.
- Background services → HTTP stack: fetches via · Background work uses the same HTTP stack for downloads and checks.
- Config system → App database: stores · User preferences and app configuration are persisted locally.
- Config system → Shared helpers: uses · Theme, dialog, and preference infrastructure is shared.
- Web module → Rule model: edits · Browser UI supports source and bookshelf editing flows.
