Zygisk C++ Module Development Guide
Master the official zygisk.hpp C++ API. Learn how to write native shared libraries that intercept Zygote process specialization, communicate with privileged root companion daemons over UNIX sockets, and compile with 16KB memory page alignment.
The Zygisk Module Class Contract
Every Zygisk module inherits from the pure virtual class zygisk::ModuleBase defined in zygisk.hpp. The module registers its entry point via REGISTER_ZYGISK_MODULE(MyModule) and optionally registers a root daemon companion via REGISTER_ZYGISK_COMPANION(companion_handler).
The 5 Core Lifecycle Methods in zygisk::ModuleBase
| Method Signature | Privilege Context | Purpose & Timing |
|---|---|---|
onLoad(Api *api, JNIEnv *env) |
Zygote Root | Called once when the module shared library is loaded into Zygote memory. |
preAppSpecialize(AppSpecializeArgs *args) |
Process Root (Pre-drop) | Called immediately after fork(). Code runs with ROOT privileges inside the app process. |
postAppSpecialize(const AppSpecializeArgs *args) |
App Sandbox (Post-drop) | Called after the kernel applies UID/GID and SELinux sandbox. Runs right before app main(). |
preServerSpecialize(ServerSpecializeArgs *args) |
System Server Root | Called before Android system_server specializes. |
postServerSpecialize(const ServerSpecializeArgs *args) |
System Server Context | Called after system_server drops privileges. |
Complete Production-Ready C++ Module Example
Below is a complete, working Zygisk C++ module (module.cpp) demonstrating package name inspection and root companion communication:
Module Packaging & Directory Structure
Zygisk shared libraries must be placed inside the zygisk/ subfolder of your Magisk module zip:
CMakeLists.txt & 16KB Memory Page Alignment
On Android 15 and 16, kernels require all 64-bit native binaries to be aligned to 16KB virtual memory page boundaries. Add this linker flag to your CMakeLists.txt:
Related Guides & Next Steps
Explore interconnected tutorials, module guides, and developer references related to this topic:
Android 15 16KB Page Sizes
AndroidConfigure CMake -Wl,-z,max-page-size=16384 for Android 15 & 16.
LSPosed Zygisk Framework
ModuleCase study on building high-performance Zygisk hooking modules.
MagiskSU CLI Reference
ToolCommunicate with root companion daemons and manage IPC.
Verified against official upstream zygisk.hpp API v4 on Magisk v30.7 (February 23, 2026).