# xuningjack/servicekeep architecture

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

- Interactive diagram: https://gitdiagram.com/xuningjack/servicekeep
- Repository: https://github.com/xuningjack/servicekeep

## Overview

1. **What this repo is**
   - This is an Android “service keep-alive” module, not a general app.
   - Its core goal is to keep a target `Service` process alive across Android versions by using two different strategies:
     - **Android 5.0 and below:** native process forking via JNI/C
     - **Android 5.0 and above:** `JobScheduler`-based wakeups

2. **Main runtime boundary**
   - The architecture is split between:
     - **Java/Kotlin-side Android components** in `app/src/jack/com/servicekeep`
     - **Native C code** in `app/jni`
   - The Java side owns service orchestration and Android lifecycle integration.
   - The native side exists only to support the pre-Lollipop process-fork keep-alive mechanism.

3. **Primary entry points**
   - `MainActivity` is the sample/demo UI entry point.
   - The real integration point for consumers is `KeepAliveManager.INSTANCE.startKeepAliveService(context)` and `stopKeepAliveSerice(context)`.
   - The README makes it clear the library is meant to be embedded, with the host app putting its actual business logic into `WorkService.onStartCommand()`.

4. **Core orchestration layer**
   - `KeepAliveManager` is the central facade.
   - It likely decides which keep-alive strategy to use based on OS version and coordinates starting/stopping the relevant components.
   - Supporting managers:
     - `ServiceManager` for service lifecycle operations
     - `PreferencesManager` for persisted flags/configuration
   - This is the main control plane of the module.

5. **Business service boundary**
   - `WorkService` is the service that the host application is expected to keep alive.
   - The README explicitly says this is where the actual business behavior should be executed, specifically in `onStartCommand`.
   - From an architecture perspective, this is the payload service; the rest of the repo exists to make sure it gets restarted or reawakened.

6. **Scheduler-based keep-alive path for Android 5.0+**
   - `KeepAliveJobSchedulerService` is the platform job component used on newer Android versions.
   - It runs in its own process, as indicated by the manifest example.
   - Its role is to schedule periodic or deferred work that wakes or restarts `WorkService`.
   - This is the main recovery path on modern Android, replacing the older native forking approach.

7. **Native keep-alive path for Android 5.0 and below**
   - `NativeRuntime` bridges Java to native code.
   - `app/jni/helper.c` contains the C-side logic, which the README describes as using `fork` to create a process that helps keep the service alive.
   - `Android.mk` and `Application.mk` show this is built with the NDK and packaged into the app.
   - The native path is a version-specific compatibility subsystem, not part of the main business flow.

8. **Persistence and state**
   - `PreferencesManager` likely stores enable/disable state or runtime settings needed to remember whether keep-alive should be active.
   - This is the only obvious persistence boundary in the repo besides Android’s own service/job state.
   - It supports the orchestration layer rather than holding business data.

9. **Utilities and support code**
   - `FileUtils` and `LogUtils` are supporting utilities.
   - They are not core architectural boundaries, but they indicate the module includes file/system interactions and centralized logging.
   - `Constant.java` likely holds process names, service names, or scheduling constants that glue the components together.

10. **Process model**
   - The README and manifest examples show multiple isolated Android processes:
     - one for `WorkService`
     - one for the job scheduler service
     - one or more native/helper processes in the older strategy
   - This multi-process design is the key architectural feature of the module.
   - The keep-alive strategy depends on process separation so one component can revive another.

11. **Android platform dependencies**
   - The repo depends on Android system services and permissions, especially:
     - `RECEIVE_BOOT_COMPLETED` for boot-time reactivation
     - `BIND_JOB_SERVICE` for the scheduler service
   - The manifest also marks services as `persistent` and uses explicit processes, which is essential to the keep-alive design.

12. **Native binary packaging**
   - `app/libs/` contains prebuilt native libraries for multiple ABIs:
     - `armeabi`, `armeabi-v7a`, `arm64-v8a`, `x86`, `x86_64`, `mips`, `mips64`
   - This matters because the keep-alive mechanism must run across device architectures, and the module is intended to be broadly compatible.

13. **How data/control flows**
   - Host app calls `KeepAliveManager.startKeepAliveService(context)`.
   - Manager starts/configures the appropriate keep-alive mechanism.
   - On modern Android, `KeepAliveJobSchedulerService` periodically re-triggers or monitors `WorkService`.
   - On older Android, the native forked process helps preserve or restart the service.
   - `WorkService.onStartCommand()` is where the host app’s persistent logic actually executes.

14. **How to draw the diagram**
   - Center the diagram on `KeepAliveManager`.
   - Draw two branches:
     - **Newer Android:** `KeepAliveManager` → `KeepAliveJobSchedulerService` → `WorkService`
     - **Older Android:** `KeepAliveManager` → `NativeRuntime` → `helper.c` forked process → `WorkService`
   - Add `PreferencesManager` and `ServiceManager` as supporting components around the manager.
   - Place `MainActivity` as a demo/consumer entry point, not part of the core keep-alive mechanism.

Main entry points: [`app/src/jack/com/servicekeep/manager/KeepAliveManager.java`](https://github.com/xuningjack/servicekeep/blob/master/app/src/jack/com/servicekeep/manager/KeepAliveManager.java) (KeepAliveManager), [`app/src/jack/com/servicekeep/service/WorkService.java`](https://github.com/xuningjack/servicekeep/blob/master/app/src/jack/com/servicekeep/service/WorkService.java) (WorkService), [`app/src/jack/com/servicekeep/service/KeepAliveJobSchedulerService.java`](https://github.com/xuningjack/servicekeep/blob/master/app/src/jack/com/servicekeep/service/KeepAliveJobSchedulerService.java) (KeepAliveJobSchedulerService), [`app/src/jack/com/servicekeep/fork/NativeRuntime.java`](https://github.com/xuningjack/servicekeep/blob/master/app/src/jack/com/servicekeep/fork/NativeRuntime.java) (NativeRuntime)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_java["Java side"]
  node_main_activity["MainActivity<br/>demo entry<br/>[MainActivity.java]"]
  node_keep_alive_manager["KeepAliveManager<br/>facade"]
  node_service_manager["ServiceManager<br/>service ops"]
  node_preferences_manager[("PreferencesManager<br/>state store")]
  node_constant["Constant<br/>[Constant.java]"]
  node_job_service["KeepAliveJobSchedulerService<br/>job service"]
  node_work_service["WorkService<br/>payload service<br/>[WorkService.java]"]
  node_file_utils["FileUtils<br/>file util<br/>[FileUtils.java]"]
  node_log_utils["LogUtils<br/>logging<br/>[LogUtils.java]"]
end

subgraph group_native["Native side"]
  node_native_runtime["NativeRuntime<br/>jni bridge<br/>[NativeRuntime.java]"]
  node_helper_c["helper.c<br/>fork logic<br/>[helper.c]"]
  node_ndk_build["NDK build<br/>native build<br/>[Android.mk]"]
  node_native_libs[("Native libs<br/>abi binaries")]
end

subgraph group_platform["Android platform"]
  node_android_job_scheduler(("JobScheduler<br/>android api"))
  node_android_processes(("App processes<br/>android runtime"))
end

node_main_activity -->|"starts"| node_keep_alive_manager
node_keep_alive_manager -->|"reads/writes"| node_preferences_manager
node_keep_alive_manager -->|"coordinates"| node_service_manager
node_keep_alive_manager -->|"uses"| node_constant
node_keep_alive_manager -->|"newer Android"| node_job_service
node_keep_alive_manager -->|"older Android"| node_native_runtime
node_job_service -->|"scheduled by"| node_android_job_scheduler
node_job_service -->|"revives"| node_work_service
node_native_runtime -->|"bridges to"| node_helper_c
node_helper_c -->|"forks"| node_android_processes
node_android_processes -.->|"isolates"| node_work_service
node_ndk_build -->|"builds"| node_helper_c
node_native_libs -->|"packages"| node_native_runtime
node_work_service -.->|"uses"| node_file_utils
node_work_service -.->|"logs with"| node_log_utils
node_keep_alive_manager -.->|"depends on"| node_android_processes

click node_main_activity "https://github.com/xuningjack/servicekeep/blob/master/app/src/jack/com/servicekeep/MainActivity.java"
click node_keep_alive_manager "https://github.com/xuningjack/servicekeep/blob/master/app/src/jack/com/servicekeep/manager/KeepAliveManager.java"
click node_service_manager "https://github.com/xuningjack/servicekeep/blob/master/app/src/jack/com/servicekeep/manager/ServiceManager.java"
click node_preferences_manager "https://github.com/xuningjack/servicekeep/blob/master/app/src/jack/com/servicekeep/manager/PreferencesManager.java"
click node_constant "https://github.com/xuningjack/servicekeep/blob/master/app/src/jack/com/servicekeep/Constant.java"
click node_job_service "https://github.com/xuningjack/servicekeep/blob/master/app/src/jack/com/servicekeep/service/KeepAliveJobSchedulerService.java"
click node_work_service "https://github.com/xuningjack/servicekeep/blob/master/app/src/jack/com/servicekeep/service/WorkService.java"
click node_file_utils "https://github.com/xuningjack/servicekeep/blob/master/app/src/jack/com/servicekeep/utils/FileUtils.java"
click node_log_utils "https://github.com/xuningjack/servicekeep/blob/master/app/src/jack/com/servicekeep/utils/LogUtils.java"
click node_native_runtime "https://github.com/xuningjack/servicekeep/blob/master/app/src/jack/com/servicekeep/fork/NativeRuntime.java"
click node_helper_c "https://github.com/xuningjack/servicekeep/blob/master/app/jni/helper.c"
click node_ndk_build "https://github.com/xuningjack/servicekeep/blob/master/app/jni/Android.mk"
click node_native_libs "https://github.com/xuningjack/servicekeep/tree/master/app/libs"

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_main_activity,node_keep_alive_manager,node_service_manager,node_preferences_manager,node_constant,node_job_service,node_work_service,node_file_utils,node_log_utils toneBlue
class node_native_runtime,node_helper_c,node_ndk_build,node_native_libs toneAmber
class node_android_job_scheduler,node_android_processes toneMint
```

## Components

### Java side

Android-facing orchestration, services, and sample entry point.

- [MainActivity](https://github.com/xuningjack/servicekeep/blob/master/app/src/jack/com/servicekeep/MainActivity.java): demo entry · Sample UI used to trigger keep-alive behavior. · `app/src/jack/com/servicekeep/MainActivity.java`
- [KeepAliveManager](https://github.com/xuningjack/servicekeep/blob/master/app/src/jack/com/servicekeep/manager/KeepAliveManager.java): facade · Central control plane that starts and stops the keep-alive subsystem. · `app/src/jack/com/servicekeep/manager/KeepAliveManager.java`
- [ServiceManager](https://github.com/xuningjack/servicekeep/blob/master/app/src/jack/com/servicekeep/manager/ServiceManager.java): service ops · Coordinates Android service lifecycle operations. · `app/src/jack/com/servicekeep/manager/ServiceManager.java`
- [PreferencesManager](https://github.com/xuningjack/servicekeep/blob/master/app/src/jack/com/servicekeep/manager/PreferencesManager.java): state store · Persists enablement and runtime flags for keep-alive. · `app/src/jack/com/servicekeep/manager/PreferencesManager.java`
- [Constant](https://github.com/xuningjack/servicekeep/blob/master/app/src/jack/com/servicekeep/Constant.java): config constants · Shared names, flags, and scheduling constants. · `app/src/jack/com/servicekeep/Constant.java`
- [KeepAliveJobSchedulerService](https://github.com/xuningjack/servicekeep/blob/master/app/src/jack/com/servicekeep/service/KeepAliveJobSchedulerService.java): job service · Modern keep-alive service used on Android 5.0+. · `app/src/jack/com/servicekeep/service/KeepAliveJobSchedulerService.java`
- [WorkService](https://github.com/xuningjack/servicekeep/blob/master/app/src/jack/com/servicekeep/service/WorkService.java): payload service · Host application's persistent service; business logic runs here. · `app/src/jack/com/servicekeep/service/WorkService.java`
- [FileUtils](https://github.com/xuningjack/servicekeep/blob/master/app/src/jack/com/servicekeep/utils/FileUtils.java): file util · Support code for filesystem and path handling. · `app/src/jack/com/servicekeep/utils/FileUtils.java`
- [LogUtils](https://github.com/xuningjack/servicekeep/blob/master/app/src/jack/com/servicekeep/utils/LogUtils.java): logging · Centralized logging helper used across the module. · `app/src/jack/com/servicekeep/utils/LogUtils.java`

### Native side

Pre-Lollipop compatibility layer and NDK-backed fork logic.

- [NativeRuntime](https://github.com/xuningjack/servicekeep/blob/master/app/src/jack/com/servicekeep/fork/NativeRuntime.java): jni bridge · Java-to-native bridge for the pre-Lollipop strategy. · `app/src/jack/com/servicekeep/fork/NativeRuntime.java`
- [helper.c](https://github.com/xuningjack/servicekeep/blob/master/app/jni/helper.c): fork logic · C-side fork-based process helper used to keep the service alive. · `app/jni/helper.c`
- [NDK build](https://github.com/xuningjack/servicekeep/blob/master/app/jni/Android.mk): native build · Build scripts that compile and package the native runtime. · `app/jni/Android.mk`
- [Native libs](https://github.com/xuningjack/servicekeep/tree/master/app/libs): abi binaries · Prebuilt native binaries for multiple CPU architectures. · `app/libs`

### Android platform

OS services and processes the module depends on.

- **JobScheduler**: android api · Platform scheduling API used to wake or relaunch work on newer Android versions.
- **App processes**: android runtime · Separate Android processes used to isolate the worker, scheduler, and helper paths.

## Connections

- MainActivity → KeepAliveManager: starts · Demo UI invokes the keep-alive facade.
- KeepAliveManager → PreferencesManager: reads/writes · Facade consults persisted state before starting or stopping the subsystem.
- KeepAliveManager → ServiceManager: coordinates · Facade delegates service lifecycle operations.
- KeepAliveManager → Constant: uses · Shared constants guide component names and behavior.
- KeepAliveManager → KeepAliveJobSchedulerService: newer Android · Selects the JobScheduler-based keep-alive path on Android 5.0+.
- KeepAliveManager → NativeRuntime: older Android · Selects the JNI/native path on Android 5.0 and below.
- KeepAliveJobSchedulerService → JobScheduler: scheduled by · Runs through the platform job scheduling API.
- KeepAliveJobSchedulerService → WorkService: revives · Wakeups or deferred work target the persistent service.
- NativeRuntime → helper.c: bridges to · Java layer enters native fork logic through JNI.
- helper.c → App processes: forks · Native helper creates a process used to sustain keep-alive behavior.
- App processes → WorkService: isolates · Separate process boundaries let one component restart another.
- NDK build → helper.c: builds · NDK scripts compile the native helper into packaged binaries.
- Native libs → NativeRuntime: packages · ABI-specific binaries support the JNI/native path across architectures.
- WorkService → FileUtils: uses · Payload service can rely on filesystem helpers.
- WorkService → LogUtils: logs with · Service-side execution is centralized through logging helpers.
- KeepAliveManager → App processes: depends on · Overall design relies on process separation for recovery.
