privatevoidhandleBindApplication(AppBindData data){// 将UI线程注册为运行时的虚拟机.
VMRuntime.registerSensitiveThread();if(data.trackAllocation){
DdmVmInternal.enableRecentAllocations(true);}// ...// 创建上下文对象final ContextImpl appContext = ContextImpl.createAppContext(this, data.info);updateLocaleListFromAppContext(appContext,
mResourcesManager.getConfiguration().getLocales());if(!Process.isIsolated()){finalint oldMask = StrictMode.allowThreadDiskWritesMask();try{setupGraphicsSupport(appContext);}finally{
StrictMode.setThreadPolicyMask(oldMask);}}else{
ThreadedRenderer.setIsolatedProcess(true);}// Continue loading instrumentation.if(ii != null){// ...}else{
mInstrumentation =newInstrumentation();// 1
mInstrumentation.basicInit(this);}
Application app;final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();final StrictMode.ThreadPolicy writesAllowedPolicy = StrictMode.getThreadPolicy();try{// If the app is being launched for full backup or restore, bring it up in// a restricted environment with the base application class.// 初始化Applcation类
app = data.info.makeApplication(data.restrictedBackupMode, null);// 2// Propagate autofill compat state
app.setAutofillCompatibilityEnabled(data.autofillCompatibilityEnabled);
mInitialApplication = app;// don't bring up providers in restricted mode; they may depend on the// app's custom Application classif(!data.restrictedBackupMode){if(!ArrayUtils.isEmpty(data.providers)){installContentProviders(app, data.providers);// For process that contains content providers, we want to// ensure that the JIT is enabled "at some point".
mH.sendEmptyMessageDelayed(H.ENABLE_JIT,10*1000);}}// Do this after providers, since instrumentation tests generally start their// test thread at this point, and we don't want that racing.try{
mInstrumentation.onCreate(data.instrumentationArgs);// 3}catch(Exception e){thrownewRuntimeException("Exception thrown in onCreate() of "+ data.instrumentationName +": "+ e.toString(), e);}try{//调用Applcation的OnCreate函数
mInstrumentation.callApplicationOnCreate(app);// 4}catch(Exception e){if(!mInstrumentation.onException(app, e)){thrownewRuntimeException("Unable to create application "+ app.getClass().getName()+": "+ e.toString(), e);}}}finally{// If the app targets < O-MR1, or doesn't change the thread policy// during startup, clobber the policy to maintain behavior of b/36951662if(data.appInfo.targetSdkVersion < Build.VERSION_CODES.O_MR1
|| StrictMode.getThreadPolicy().equals(writesAllowedPolicy)){
StrictMode.setThreadPolicy(savedPolicy);}}// Preload fonts resources
FontsContract.setApplicationContextForResources(appContext);if(!Process.isIsolated()){try{final ApplicationInfo info =getPackageManager().getApplicationInfo(
data.appInfo.packageName,
PackageManager.GET_META_DATA /*flags*/,
UserHandle.myUserId());if(info.metaData != null){finalint preloadedFontsResource = info.metaData.getInt(
ApplicationInfo.METADATA_PRELOADED_FONTS,0);if(preloadedFontsResource !=0){
data.info.getResources().preloadFonts(preloadedFontsResource);}}}catch(RemoteException e){throw e.rethrowFromSystemServer();}}}