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);

ZygoteServer.runSelectLoop()

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

ZygoteConnection.processOneCommand()

if (pid == 0) {
    // in child
    zygoteServer.setForkChild();

    zygoteServer.closeServerSocket();
    IoUtils.closeQuietly(serverPipeFd);
    serverPipeFd = null;

    return handleChildProc(parsedArgs, childPipeFd, parsedArgs.mStartChildZygote);
} 

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 */);
}

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);
}

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);
}

至此调用到ActivityThread.main()

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值