android 启动 ZygoteInit.java分析

来源:互联网 发布:网络安全法 二十七 编辑:程序博客网 时间:2024/05/16 10:18

关于android启动分析的文章 是拜读柯元旦老师的android内核剖析 与 邓凡平老师的深入理解android 自己读源码按图索骥的成果。

在这里感谢 两位老师的不辞辛劳把知识奉献出来与大家分享!


ZygoteInit.java 受精卵初始化

主要做的工作有
1.主要功能为 注册一个 服务端接口 端口号为666 在init.rc中定义的
这个socketserver 接收来自客户端分裂子他进程的请求
2.加载class文件
3.加载系统资源文件 包括preloaded_drawables 与preloaded_color_state_lists
4.启动:SystemServer进程 并且入口地址为com.android.server.SystemServer.main

5.如果启动成功了 关闭子进程 即第4部中的socketserer监听

6.监听socket请求



System_server进程 是android frameWork 各模块的存在进程

System_server路径为 /frameworks/base/services/java/com/android/server/SystemServer.java

 /**
     * This method is called from Zygote to initialize the system. This will cause the native
     * services (SurfaceFlinger, AudioFlinger, etc..) to be started. After that it will call back
     * up into init2() to start the Android services.
     */
    native public static void init1(String[] args);

    public static void main(String[] args) {
        if (SamplingProfilerIntegration.isEnabled()) {
            SamplingProfilerIntegration.start();
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    SamplingProfilerIntegration.writeSnapshot("system_server");
                }
            }, SNAPSHOT_INTERVAL, SNAPSHOT_INTERVAL);
        }

        // The system server has to run all of the time, so it needs to be
        // as efficient as possible with its memory usage.
        VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);

        System.loadLibrary("android_servers");
        init1(args);
    }

这里注释写的也很清楚 这个方法是由Zygote 即 ZygoteInit.java来调用初始化系统的,这里会通过native方法启动服务(显示,多媒体 等服务)然后调用init2()来开启

android服务

native方法是在 /frameworks/base/services/jni/com_android_server_SystemServer.cpp实现的。

static void android_server_SystemServer_init1(JNIEnv* env, jobject clazz)
{
    system_init();
}

又调用了system_init();方法

通过搜索 发现这个方法在frameworks\base\cmds\system_server\library\system_init.cpp文件中

这里启动的服务并不多 包括 SurfaceFlinger AudioFlinger MediaPlayerService CameraService AudioPolicyService

然后调用了init2();

来看下init2()吧

init2 启动了一个线程来加载各种服务

加载的服务有:

new EntropyService()

new PowerManagerService()

new TelephonyRegistry(context)

TelephonyRegistry

PackageManagerService

ActivityManagerService

AccountManagerService

ContentService

BatteryService

HardwareService

AlarmManagerService

SensorService

WindowManagerService

InputMethodManagerService

WallpaperManagerService




原创粉丝点击