//路径:/frameworks/base/core/java/android/os/ZygoteProcess.java /** * Tries to open socket to Zygote process if not already open. If * already open, does nothing. May block and retry. Requires that mLock be held. */ @GuardedBy("mLock") private ZygoteState openZygoteSocketIfNeeded(String abi)throws ZygoteStartFailedEx { //检查当前线程是否持有mLock锁,没有则抛异常 Preconditions.checkState(Thread.holdsLock(mLock), "ZygoteProcess lock not held");
//路径:/frameworks/base/core/java/android/os/ZygoteProcess.java /** * Sends an argument list to the zygote process, which starts a new child * and returns the child's pid. Please note: the present implementation * replaces newlines in the argument list with spaces. * * @throws ZygoteStartFailedEx if process start failed for any reason */ @GuardedBy("mLock") privatestatic Process.ProcessStartResult zygoteSendArgsAndGetResult( ZygoteState zygoteState, ArrayList<String> args) throws ZygoteStartFailedEx { try { // 检查args数组中的参数 intsz= args.size(); for (inti=0; i < sz; i++) { if (args.get(i).indexOf('\n') >= 0) { thrownewZygoteStartFailedEx("embedded newlines not allowed"); } }
/** * See com.android.internal.os.SystemZygoteInit.readArgumentList() * Presently the wire format to the zygote process is: * a) a count of arguments (argc, in essence) * b) a number of newline-separated argument strings equal to count * * After the zygote process reads these it will write the pid of * the child or -1 on failure, followed by boolean to * indicate whether a wrapper process was used. */ finalBufferedWriterwriter= zygoteState.writer; finalDataInputStreaminputStream= zygoteState.inputStream; //将参数写入zygoteState中 writer.write(Integer.toString(args.size())); writer.newLine();
for (inti=0; i < sz; i++) { Stringarg= args.get(i); writer.write(arg); writer.newLine(); }
//路径:/frameworks/base/core/java/com/android/internal/os/ZygoteConnection.java /** * Reads one start command from the command socket. If successful, * a child is forked and a {@link Zygote.MethodAndArgsCaller} * exception is thrown in that child while in the parent process, * the method returns normally. On failure, the child is not * spawned and messages are printed to the log and stderr. Returns * a boolean status value indicating whether an end-of-file on the command * socket has been encountered. * * @return false if command socket should continue to be read from, or * true if an end-of-file has been encountered. * @throws Zygote.MethodAndArgsCaller trampoline to invoke main() * method in child process */ booleanrunOnce(ZygoteServer zygoteServer)throws Zygote.MethodAndArgsCaller {
//路径:/frameworks/base/core/java/com/android/internal/os/WrapperInit.java /** * Executes a runtime application with a wrapper command. * This method never returns. * * @param invokeWith The wrapper command. * @param niceName The nice name for the application, or null if none. * @param targetSdkVersion The target SDK version for the app. * @param pipeFd The pipe to which the application's pid should be written, or null if none. * @param args Arguments for {@link RuntimeInit#main}. */ publicstaticvoidexecApplication(String invokeWith, String niceName, int targetSdkVersion, String instructionSet, FileDescriptor pipeFd, String[] args) { //创建指令字符串并构造指令 StringBuildercommand=newStringBuilder(invokeWith);
try { //获取android.app.ActivityThread的类 cl = Class.forName(className, true, classLoader); } catch (ClassNotFoundException ex) { thrownewRuntimeException( "Missing class when invoking static main " + className, ex); }
Method m; try { //获得android.app.ActivityThread中的main方法 m = cl.getMethod("main", newClass[] { String[].class }); } catch (NoSuchMethodException ex) { thrownewRuntimeException( "Missing static main on " + className, ex); } catch (SecurityException ex) { thrownewRuntimeException( "Problem getting static main on " + className, ex); }
intmodifiers= m.getModifiers(); if (! (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))) { thrownewRuntimeException( "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. */ //通过抛出异常启动main方法 thrownewZygote.MethodAndArgsCaller(m, argv); }