美文网首页
SystemServer 中的main函数是在安卓源码的什么地方

SystemServer 中的main函数是在安卓源码的什么地方

作者: 忧零520 | 来源:发表于2024-07-23 21:56 被阅读0次

在Android操作系统的源码中,SystemServer 类的 main 方法是 Android 应用程序启动的入口点。它在应用程序进程启动时被调用。具体来说,它是在 Zygote 进程中通过反射机制被调用的。

SystemServer.main 方法实际上是由 ZygoteInit 类启动应用程序进程时通过 RuntimeInit 类来调用的。以下是调用关系的简要说明:

ZygoteInit 启动新进程: Zygote 是一个守护进程,负责孵化新进程。当系统请求启动一个新的应用程序时,ZygoteInit 会创建一个新进程。

ZygoteInit 通过 fork 创建新进程: ZygoteInit 初始化应用程序进程,并在新进程中调用 ZygoteInit.zygoteInit 方法,该方法进一步调用 RuntimeInit 的 commonInit 和 applicationInit 方法。

RuntimeInit 初始化: 在 applicationInit 方法中,会通过反射调用 SystemServer.main 方法

以下是关键代码片段:

// ZygoteInit.java
public static void main(String[] argv) {
    // Initialize the system and start the Zygote process
    ZygoteServer zygoteServer = new ZygoteServer();
    ...
    if (startSystemServer) {
                Runnable r = forkSystemServer(abiList, zygoteSocketName, zygoteServer);

                // {@code r == null} in the parent (zygote) process, and {@code r != null} in the
                // child (system_server) process.
                if (r != null) {
                    r.run();
                    return;
                }
            }
    ...
}

private static boolean forkSystemServer(String abiList, String socketName,
            ZygoteServer zygoteServer) {
    // Fork and start the system server process.
    ...
    /* For child process */
        if (pid == 0) {
            if (hasSecondZygote(abiList)) {
                waitForSecondaryZygote(socketName);
            }

            zygoteServer.closeServerSocket();
            return handleSystemServerProcess(parsedArgs);
        }
    ...
}

private static Runnable handleSystemServerProcess(ZygoteArguments parsedArgs) {
  ...
        /*
         * Pass the remaining arguments to SystemServer.
         */
        return ZygoteInit.zygoteInit(parsedArgs.mTargetSdkVersion,
                    parsedArgs.mDisabledCompatChanges,
                    parsedArgs.mRemainingArgs, cl);

  ...

}

 public static final Runnable zygoteInit(int targetSdkVersion, long[] disabledCompatChanges,
            String[] argv, ClassLoader classLoader) {
        if (RuntimeInit.DEBUG) {
            Slog.d(RuntimeInit.TAG, "RuntimeInit: Starting application from zygote");
        }

        Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ZygoteInit");
        RuntimeInit.redirectLogStreams();

        RuntimeInit.commonInit();
        ZygoteInit.nativeZygoteInit();
        return RuntimeInit.applicationInit(targetSdkVersion, disabledCompatChanges, argv,
                classLoader);
    }

// RuntimeInit.java
protected static Runnable applicationInit(int targetSdkVersion, long[] disabledCompatChanges,
            String[] argv, ClassLoader classLoader) {
        ...
        final Arguments args = new Arguments(argv);
        // The end of of the RuntimeInit event (see #zygoteInit).
        Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
        // Remaining arguments are passed to the start class's static main
        return findStaticMain(args.startClass, args.startArgs, classLoader);
    }

protected static Runnable findStaticMain(String className, String[] argv,
            ClassLoader classLoader) {
        Class<?> cl;

        try {
            cl = Class.forName(className, true, classLoader);
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(
                    "Missing class when invoking static main " + className,
                    ex);
        }
        Method m;
        try {.   //这里就是调用ActivityThread的地方
            m = cl.getMethod("main", new Class[] { String[].class });
        } catch (NoSuchMethodException ex) {
            throw new RuntimeException(
                    "Missing static main on " + className, ex);
        } catch (SecurityException ex) {
            throw new RuntimeException(
                    "Problem getting static main on " + className, ex);
        }

        int modifiers = m.getModifiers();
        if (! (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))) {
            throw new RuntimeException(
                    "Main method is not public and static on " + className);
        }


        return new MethodAndArgsCaller(m, argv);
    }

通过以上调用链,SystemServer.main 方法在应用程序进程启动时得以执行。所以,最终是通过 Zygote 进程的初始化和反射机制来调用的 SystemServer.main 方法

相关文章

  • SystemServer进程--main函数的执行

    android源码学习目录 1.main函数 直接上代码 阅读代码,SystemServer main函数进行了各...

  • 分析SystemServer

    1.分析主函数main() 通过上述代码可知,SystemServer中的函数main()通过函数init1(),...

  • 开机第一次启动

    SystemServer进程是zygote孵化进程fork出的第一个进程。 SystemServer的main函数...

  • SMA

    1.首先执行SystemServer的Main函数 ActivityManagerService#LifeCycl...

  • Android 包管理服务

    PackageManagerService(PmS)包管理服务运行在SystemServer进程中,是一个安卓系统...

  • SystemServer启动

    本文基于安卓6.0源码,对systemserver进程的启动与管理的基本过程进行介绍。从Zygote进程初始化Zy...

  • redis-cli的实现原理

    首先从源码中找入口 redis源码:src/redis-cli.c中找到main函数,main函数中核心的处理就是...

  • 使用Systrace分析安卓性能

    一、概述 Systrace是安卓4.1中新增的数据采样和分析工具。谷歌通过在源码的关键函数中这是trace(也就是...

  • 【Android】WMS和AMS初探

    代码基于Android 9.0 1、wms的启动 SystemServer的main函数,启动了SystemSer...

  • RunLoop源码剖析---图解RunLoop

    RunLoop源码剖析---图解RunLoop 源码面前,了无秘密 前言 我们在iOS APP中的main函数如下...

网友评论

      本文标题:SystemServer 中的main函数是在安卓源码的什么地方

      本文链接:https://www.haomeiwen.com/subject/zgduhjtx.html