After being forked by Zygote, execution in the system_server process begins at com.android.server.SystemServer.main(). This method is responsible for setting up the environment and orchestrating the instantiation of over 100 system services.
SystemServer.main() Entry Point
The main() method is surprisingly simple. It instantiates the SystemServer object and calls its run() method.
public static void main(String[] args) {
new SystemServer().run();
}
The run() method is where the heavy lifting occurs. It executes purely on the main thread of the system_server process.
System Properties at Startup
One of the first tasks in run() is to read and set system properties. These properties dictate how the system behaves.
For instance, it checks ro.factorytest to determine if the device is booting into factory test mode. It reads sys.boot_completed to ensure it is set to 0 (indicating boot is in progress). It also configures the VM policy using StrictMode to catch disk or network access on the main thread early in the boot process.
Another critical property is the device timestamp. SystemServer forces the system clock to a sane minimum value (typically the build date) if the RTC battery died and the clock reset to the Unix epoch, preventing SSL certificate validation failures during boot.
Looper and Main Thread Setup
System services rely heavily on Android's Handler and Looper messaging architecture. SystemServer must prepare the main thread to handle these messages.
Looper.prepareMainLooper();
// Initialize native services.
System.loadLibrary("android_servers");
It initializes the main looper, sets the main thread's priority to THREAD_PRIORITY_FOREGROUND, and loads the libandroid_servers.so JNI library. This library contains the native implementations for services that bridge Java and C++, such as InputManagerService.
SystemServiceManager Creation
To manage the complex lifecycle of dozens of services, SystemServer instantiates SystemServiceManager.
SystemServiceManager acts as the supervisor. It provides a standard startService() method that takes a class name, instantiates the service via reflection, registers it, and triggers its onStart() lifecycle callback.
By centralizing service creation through SystemServiceManager, Android ensures that boot phases (like "wait for safe mode", "wait for third party apps") are broadcast consistently to all running services via onBootPhase(int phase).