# 2dust/v2rayng architecture

> Architecture diagram of the GitHub repository 2dust/v2rayng, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-04). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/2dust/v2rayng
- Repository: https://github.com/2dust/v2rayng

## Overview

##### Main Components
- UI Layer – Represent the Android Activities, Fragments, and adapters under the “ui” package. This layer handles user interactions (e.g., MainActivity, SettingsActivity, LogcatActivity, etc.) and connects with viewmodels (e.g., MainViewModel, SettingsViewModel).
- Service Layer – Include Android services such as V2RayVpnService, ProcessService, NotificationService, and other background services that manage VPN connections, network tests, and subscription updates.
- Native Library Integration – Create a component for native cores where the v2ray core (and the Xray alternative) are integrated. Note that the aar is built from separate Go projects (AndroidLibXrayLite, AndroidLibV2rayLite) and that additional native tools like tun2socks, hysteria, and others are interfaced.
- Data Resources & Assets – Consider assets (configuration files, json files, open_source_licenses, geoip/geosite data) as a separate repository component that supplies data to the app.

##### Relationships and Interactions
- Plugin & Extension: Illustrate the plugin interface connecting external plugins (NativePlugin, PluginList) with the core management logic, emphasizing extensibility.

##### Architectural Patterns and Design Principles
- Layered/MVC (or MVVM) – Indicate that the app structure follows a separation between view (XML layouts, Activities), business logic (ViewModels, Managers) and data access (configuration loading/persistence).
- Modularity – Reflect that there is a clear separation between the UI, services, and the native integration (as separate libraries and modules).
- Dependency Inversion – Note that the plugins and configuration handlers abstract underlying platform or native functionality.

##### Relevant Technologies, Frameworks, and Libraries
- Kotlin as the programming language and Android Studio with Gradle as the build system.
- Integration with native libraries compiled from Go (AndroidLibXrayLite and potentially AndroidLibV2rayLite).
- Fastlane for deployment and GitHub Actions for Continuous Integration.
- Android’s architectural components (Activities/Fragment, ViewModels, Services) which are evident from the directory structure.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Android App Layers
    subgraph "Android App Layers"
        UI["UI Layer<br/>(Activities, Fragments, Adapters)"]:::ui
        VM["ViewModels<br/>(MainViewModel, SettingsViewModel)"]:::ui
        SERVICE["Service Layer<br/>(V2RayVpnService, ProcessService, NotificationService)"]:::service
        CONFIG["Configuration & Management<br/>(AngConfigManager, SettingsManager, V2rayConfigManager)"]:::config
        PLUGIN["Plugin Management<br/>(Plugin, PluginManager)"]:::plugin
    end

    %% Native and Data Layers
    subgraph "Native & Data"
        NATIVE["Native Library Integration<br/>(AndroidLibXrayLite, tun2socks, etc.)"]:::native
        DATA["Data Resources & Assets<br/>(Configurations, JSON, geoip/geosite, resources)"]:::data
    end

    %% Build and Deployment
    subgraph "Build & CI"
        BUILD["Build/CI & Deployment<br/>(Gradle, Fastlane, GitHub Workflows)"]:::build
    end

    %% Interactions among components
    UI -->|"userAction"| VM
    VM -->|"fetchOrUpdateConfig"| CONFIG
    CONFIG -->|"loadPlugins"| PLUGIN
    CONFIG -->|"supplyConfig"| SERVICE
    UI -->|"initService"| SERVICE
    SERVICE -->|"invokeNative"| NATIVE
    CONFIG -->|"readAssets"| DATA
    NATIVE -->|"accessData"| DATA
    PLUGIN -->|"extendFunctionality"| NATIVE
    SERVICE -->|"notifyPlugin"| PLUGIN

    %% Build system influences components
    BUILD ---|"buildsAndDeploys"| UI
    BUILD ---|"buildsAndDeploys"| SERVICE
    BUILD ---|"buildsAndDeploys"| NATIVE

    %% Click Events
    click UI "https://github.com/2dust/v2rayng/tree/master/V2rayNG/app/src/main/java/com/v2ray/ang/ui"
    click SERVICE "https://github.com/2dust/v2rayng/tree/master/V2rayNG/app/src/main/java/com/v2ray/ang/service"
    click CONFIG "https://github.com/2dust/v2rayng/tree/master/V2rayNG/app/src/main/java/com/v2ray/ang/handler"
    click PLUGIN "https://github.com/2dust/v2rayng/tree/master/V2rayNG/app/src/main/java/com/v2ray/ang/plugin"
    click NATIVE "https://github.com/2dust/v2rayng/tree/master/AndroidLibXrayLite"
    click DATA "https://github.com/2dust/v2rayng/tree/master/V2rayNG/app/src/main/assets"
    click BUILD "https://github.com/2dust/v2rayng/tree/master/.github/workflows"

    %% Styles
    classDef ui fill:#a1cfff,stroke:#000,stroke-width:1px;
    classDef service fill:#b6ffb6,stroke:#000,stroke-width:1px;
    classDef config fill:#ffffb3,stroke:#000,stroke-width:1px;
    classDef plugin fill:#ffccff,stroke:#000,stroke-width:1px;
    classDef native fill:#d3d3d3,stroke:#000,stroke-width:1px;
    classDef data fill:#cce5ff,stroke:#000,stroke-width:1px;
    classDef build fill:#ffb3b3,stroke:#000,stroke-width:1px;
```
