# tencent/tinker architecture

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

- Interactive diagram: https://gitdiagram.com/tencent/tinker
- Repository: https://github.com/tencent/tinker

## Overview

##### Project Type and Purpose
- Tinker is an Android hot‑fix solution library (open‑source tool) that supports dex, native library (.so) and resource updates without reinstalling the APK.
- It consists of build‑time components (Gradle plugin, CLI, annotation processor, patch‑building libraries) and runtime components (loader modules, core Android libraries, patch applier).

##### File‑Structure Analysis
- Top‑level directories:
- gradle, build scripts, publishing logic
- third-party (forks of AOSP dexutils, bsdiff, ziputils)
- tinker-android (annotation support, annotation processor, no-op stubs, real runtime lib, runtime loader)
- tinker-build (CLI tool, Gradle plugin, patch‑lib core patch‑generation engine)
- tinker-commons (shared dex‑patch applier utilities)
- tinker-sample-android (sample Android app demonstrating integration)
- Patterns: separation of build‑time vs. runtime modules; annotation processing to generate Application wiring; plugin hooks into Android build pipeline; multi‑module Gradle project.

##### README Insights
- Shows how to “apply” the Gradle plugin, add dependencies, annotate ApplicationLike, and subclass TinkerApplication.
- Indicates core phases: patch building (CLI or Gradle), annotation (tinker-android-anno), runtime loading (tinker-android-loader, tinker-android-lib).

##### Main Components
a. Build‑Time Components
- Tinker‑Patch‑Gradle‑Plugin (Gradle plugin module)
- Tinker‑Patch‑CLI (standalone Java CLI)
- Annotation Processor (tinker-android-anno)
- Patch‑Lib Engine (tinker-build/tinker-patch‑lib + tinker-commons)
- Third‑Party Utilities (dexutils, bsdiff, ziputils)
b. Runtime Components
- Tinker‑Android‑Loader (classloader hooks, hotplug interceptors)
- Tinker‑Android‑Lib (patch applier: DexDiffPatchInternal, ResDiffPatchInternal, SoDiffPatchInternal, ArkHotDiffPatchInternal)
- ApplicationLike / TinkerApplication integration
- Android OS (classloader, resource manager)
c. Sample App
- SampleApplicationLike, SampleApplication
- Sample patch listeners & reporters
d. External Dependencies
- Android Gradle Plugin, AAPT, Java/Kotlin toolchain

##### Relationships & Data Flows
- Build‑time:
- Developer invokes Gradle build → Gradle plugin invokes patch-lib + third‑party tools to diff old vs. new APK → outputs patch files (classes.dex, patch.apk)
- CLI alternative uses patch‑lib engine directly
- Annotation processor runs during compile to generate Application wiring classes
- Runtime:
- On Application start, TinkerApplication initializes TinkerInstaller → loader module injects new Dex, So, resources into classloader & resource system → patch applier (core lib + commons) applies incremental diffs
- PatchListener and LoadReporter receive callbacks
- Sample app exercises the above flow.

##### Architectural Patterns & Principles
- Modular separation: build‑time vs. runtime vs. sample vs. third‑party
- Plugin pattern: Gradle plugin hooks into Android build lifecycle
- Annotation processing: code‑generation to decouple compile‑time wiring
- Strategy pattern: Diff/Patch algorithms (Dex, resource, library)
- Chain of responsibility: loader hotplug interceptors

##### Essential Elements Checklist
- [ ] Separate build‑time vs. runtime vs. sample vs. external
- [ ] Show patch file creation and injection flows
- [ ] Indicate use of third‑party utils (dexutils, bsdiff, ziputils)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Developer & Build Invocation
    subgraph "Developer & Build Invocation"
        Developer["Developer"]:::external
        CLI["Tinker-Patch-CLI"]:::build
        Gradle["Tinker-Patch-Gradle-Plugin"]:::build
        Developer -->|invokes CLI| CLI
        Developer -->|invokes Gradle| Gradle
        click CLI "https://github.com/tencent/tinker/tree/dev/tinker-build/tinker-patch-cli"
        click Gradle "https://github.com/tencent/tinker/tree/dev/tinker-build/tinker-patch-gradle-plugin"
    end

    %% Build-Time Modules
    subgraph "Build-Time Modules"
        anno["Annotation Processor"]:::build
        support["Annotation Support Stubs"]:::build
        patchlib["Patch-Lib Engine"]:::build
        commons["Patch-Applier Commons"]:::third
        dexutils["DexUtils"]:::third
        bsdiff["BSDiff Utilities"]:::third
        ziputils["ZipUtils"]:::third

        Gradle --> anno
        Gradle --> patchlib
        anno --> support
        patchlib --> commons
        patchlib --> dexutils
        patchlib --> bsdiff
        patchlib --> ziputils

        click anno "https://github.com/tencent/tinker/tree/dev/tinker-android/tinker-android-anno"
        click support "https://github.com/tencent/tinker/tree/dev/tinker-android/tinker-android-anno-support"
        click patchlib "https://github.com/tencent/tinker/tree/dev/tinker-build/tinker-patch-lib"
        click commons "https://github.com/tencent/tinker/tree/dev/tinker-commons"
        click dexutils "https://github.com/tencent/tinker/tree/dev/third-party/aosp-dexutils"
        click bsdiff "https://github.com/tencent/tinker/tree/dev/third-party/bsdiff-util"
        click ziputils "https://github.com/tencent/tinker/tree/dev/third-party/tinker-ziputils"
    end

    %% Generated Artifacts
    subgraph "Generated Artifacts"
        patches["Patch Files"]:::artifact
        apkDiff["Diff APK"]:::artifact
        patchlib -->|creates| patches
        patchlib -->|creates| apkDiff
    end

    %% Runtime Components
    subgraph "Runtime Components"
        loader["Runtime Loader Module"]:::runtime
        noploader["No-Op Loader Stubs"]:::runtime
        lib["Runtime Lib Module"]:::runtime
        nopolib["No-Op Lib Stubs"]:::runtime

        loader --> lib
        loader --> nopolib

        click loader "https://github.com/tencent/tinker/tree/dev/tinker-android/tinker-android-loader"
        click noploader "https://github.com/tencent/tinker/tree/dev/tinker-android/tinker-android-loader-no-op"
        click lib "https://github.com/tencent/tinker/tree/dev/tinker-android/tinker-android-lib"
        click nopolib "https://github.com/tencent/tinker/tree/dev/tinker-android/tinker-android-lib-no-op"
    end

    %% Sample App
    subgraph "Sample App"
        sample["Sample Android App"]:::sample
        Developer --> sample
        sample -->|uses| loader
        sample -->|uses| lib
        click sample "https://github.com/tencent/tinker/tree/dev/tinker-sample-android/app"
    end

    %% External Systems
    OS["Android OS"]:::external
    OS --> loader
    OS --> lib

    %% Styles
    classDef build fill:#cce5ff,stroke:#004085,color:#004085
    classDef runtime fill:#d4edda,stroke:#155724,color:#155724
    classDef third fill:#e2e3e5,stroke:#6c757d,color:#6c757d
    classDef sample fill:#fff3cd,stroke:#856404,color:#856404
    classDef external fill:#f0f0f0,stroke:#b3b3b3,color:#333333
    classDef artifact fill:#d1ecf1,stroke:#0c5460,color:#0c5460
```
