• APP启动流程二(源码30)之APP进程创建


    接上篇 APP启动流程一(源码30)之向Zygote发送创建APP进程的请求

    Zygote进程
    App进程

    ZygoteInit.main()

    //其他代码省略
    zygoteServer = new ZygoteServer(isPrimaryZygote);
    
    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;
        }
    }
    
    Log.i(TAG, "Accepting command socket connections");
    
    // The select loop returns early in the child process after a fork and
    // loops forever in the zygote.
    //fork之后就会返回,并且runSelectLoop()会在Zygote进程一直循环,有请求就处理,处理完了接着循环等待新的socket请求
    caller = zygoteServer.runSelectLoop(abiList);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    ZygoteServer.runSelectLoop()

    ZygoteConnection connection = peers.get(pollIndex);
    final Runnable command = connection.processOneCommand(this);
    
    • 1
    • 2

    ZygoteConnection.processOneCommand()

    if (pid == 0) {
        // in child
        zygoteServer.setForkChild();
    
        zygoteServer.closeServerSocket();
        IoUtils.closeQuietly(serverPipeFd);
        serverPipeFd = null;
    
        return handleChildProc(parsedArgs, childPipeFd, parsedArgs.mStartChildZygote);
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    ZygoteConnection.handleChildProc()

    if (!isZygote) {
        return ZygoteInit.zygoteInit(parsedArgs.mTargetSdkVersion,
                parsedArgs.mDisabledCompatChanges,
                parsedArgs.mRemainingArgs, null /* classLoader */);
    } else {
        return ZygoteInit.childZygoteInit(parsedArgs.mTargetSdkVersion,
                parsedArgs.mRemainingArgs, null /* classLoader */);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    ZygoteInit.childZygoteInit()

    static final Runnable childZygoteInit(int targetSdkVersion, String[] argv, ClassLoader classLoader) {
        RuntimeInit.Arguments args = new RuntimeInit.Arguments(argv);
        return RuntimeInit.findStaticMain(args.startClass, args.startArgs, classLoader);
    }
    
    • 1
    • 2
    • 3
    • 4

    RuntimeInit.findStaticMain()

    protected static Runnable findStaticMain(String className, String[] argv,
            ClassLoader classLoader) {
        Class<?> cl;
    
        try {
            //className:android.app.ActivityThread
            cl = Class.forName(className, true, classLoader);
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(
                    "Missing class when invoking static main " + className,
                    ex);
        }
    
        Method m;
        try {
            //main()
            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);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    至此调用到ActivityThread.main()

    接下篇 APP启动流程三(源码30)之APP进程启动及Activity启动

  • 相关阅读:
    Java 插入公式到PPT幻灯片
    JAVA毕业设计客服管理系统计算机源码+lw文档+系统+调试部署+数据库
    TypeScript中的declare关键字
    网络安全域内用户Hash获取方式
    Linux 内核 6.5 发布,首次支持 Wi-Fi 7 和 USB4
    33 C++ Web 编程
    懒人福利:6款Sketch插件合集,提升设计效率爆款推荐!
    汽车行驶中是怎么保障轴瓦安全的?
    电子统计台账:锁定某月台账数据,防止数据丢失
    gazebo中添加动态障碍物
  • 原文地址:https://blog.csdn.net/Super_666/article/details/126249021