Android应用程序进程启动过程

注:

1、问:安卓中SO 64位还是32位的检查是在什么地方?

答:AMS在向Zygote进程发送创建应用程序进程请求的时候,ZygoteProcess.java 中,通过Socket连接Zygote,根据返回的状态与应用程序进程所需要的ABI进行匹配。

Zygote接受请求并创建应用程序进程的时序图:

image

调用ActivityThread之前源码:

image

注释1处通过反射获得了android.app.ActivityThread类,注释2处获得了ActivityThread的main方法,并将main方法传入注释3处的Zygote中的MethodAndArgsCaller r类的构造方法中。在注释3处跑出的MethodAndArgsCaller异常会被ZygoteInit的main方法捕获,至于这里为何采用抛出异常而不是直接调用ActivityThread的main方法,这种抛出异常的处理会清楚所有的设置过程需要的堆栈帧,并让ActivityThread的main方法看起来像是程序进程的入口方法。

注:代码截图是参考《Android进阶解密》一书

我看9.0的源码


    /**
     * Invokes a static "main(argv[]) method on class "className".
     * Converts various failing exceptions into RuntimeExceptions, with
     * the assumption that they will then cause the VM instance to exit.
     *
     * @param className Fully-qualified class name
     * @param argv Argument vector for main()
     * @param classLoader the classLoader to load {@className} with
     */
    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 {
            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);
        }

        /*
         * This throw gets caught in ZygoteInit.main(), which responds
         * by invoking the exception's run() method. This arrangement
         * clears up all the stack frames that were required in setting
         * up the process.
         */
        return new MethodAndArgsCaller(m, argv);
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容