开始进入Framework的源码分析,在分析源码之前,先来了解一下android系统启动的流程,如下图所示:
AMS介绍
AMS(Activity Manager Service)是android系统中非常重要的一个系统服务,他主要负责四大组件的启动,切换,调度以及应用进程的管理和调度工作。
也就说所有的app应用都会和AMS打交道。
AMS作为系统的引导服务,是在SystemServer进程中启动SystemServer时创建并启动的,关于SystemServer的启动流程另外分析。
AMS继承自IActivityManager.Stub ,作为系统服务的服务者提供系统服务。
AMS启动源码分析
先来看看下面这张思维导图,方便在看到源码分析时更清晰:
1、创建AMS对象,并启动AMS
1.1、获取ActivityTaskManagerService服务:
//SystemServer.java
private void startBootstrapServices(@NonNull TimingsTraceAndSlog t) {
ActivityTaskManagerService atm = mSystemServiceManager.startService(
ActivityTaskManagerService.Lifecycle.class).getService();
}
进入SystemServiceManager的startService()方法中,这里传递的参数是ActivityTaskManagerService.Lifecycle.class
注意:因为startService()执行的结果还需要调用getService()方法,所以startService()是带了返回值
//SystemServiceManager.java
public SystemService startService(String className) {
//通过类加载器加载service
final Class<SystemService> serviceClass = loadClassFromLoader(className,
this.getClass().getClassLoader());
return startService(serviceClass);//此处进入startService()方法
}
public <T extends SystemService> T startService(Class<T> serviceClass) {
try {
final String name = serviceClass.getName();
Slog.i(TAG, "Starting " + name);
Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartService " + name);
// Create the service.
if (!SystemService.class.isAssignableFrom(serviceClass)) {
throw new RuntimeException("Failed to create " + name
+ ": service must extend " + SystemService.class.getName());
}
final T service;
try {
//通过类的构造器来创建service实例
Constructor<T> constructor = serviceClass.getConstructor(Context.class);
service = constructor.newInstance(mContext);
} catch (InstantiationException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service could not be instantiated", ex);
} catch (IllegalAccessException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service must have a public constructor with a Context argument", ex);
} catch (NoSuchMethodException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service must have a public constructor with a Context argument", ex);
} catch (InvocationTargetException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service constructor threw an exception", ex);
}
startService(service);
return service;//这里返回的service是 ActivityTaskManagerService.Lifecycle对象
} finally {
Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
}
}
由上面的源码知道是通过反射来调用构造器创建的对象,而需要创建的对象是ActivityTaskManagerService.Lifecycle,那么接下来就进入ActivityTaskManagerService静态内部类Lifecycle的构造方法执行:
//ActivityTaskManagerService.java
public static final class Lifecycle extends SystemService {
private final ActivityTaskManagerService mService;
public Lifecycle(Context context) {
super(context);
//这里就创建好了ActivityTaskManagerService对象
mService = new ActivityTaskManagerService(context);
}
@Override
public void onStart() {
//这里就是上一步调回进来的地方
publishBinderService(Context.ACTIVITY_TASK_SERVICE, mService);
mService.start();
}
@Override
public void onUnlockUser(int userId) {
synchronized (mService.getGlobalLock()) {
mService.mStackSupervisor.onUserUnlocked(userId);
}
}
@Override
public void onCleanupUser(int userId) {
synchronized (mService.getGlobalLock()) {
mService.mStackSupervisor.mLaunchParamsPersister.onCleanupUser(userId);
}
}
public ActivityTaskManagerService getService() {
return mService;
}
}
ActivityTaskManagerService.Lifecycle对象创建时已经将ActivityTaskManagerService对象也创建好了,但是返回的是ActivityTaskManagerService.Lifecycle的对象实例,需要调用ActivityTaskManagerService.Lifecycle类的getServices()方法来得到ActivityTaskManagerService对象实例。
1.2、创建AMS对象
回到SystemServer的启动引导服务方法中:
//SystemServer.java
private void startBootstrapServices(@NonNull TimingsTraceAndSlog t) {
//这一步已经完成
ActivityTaskManagerService atm = mSystemServiceManager.startService(
ActivityTaskManagerService.Lifecycle.class).getService();
//从这里开始分析
mActivityManagerService = ActivityManagerService.Lifecycle.startService(
mSystemServiceManager, atm);
}
下面从ActivityManagerService类的内部静态Lifecycle的静态方法startService()开始分析:
这里和上一步的ActivityTaskManagerService的内部静态类Lifecycle要区分一下,
//ActivityManagerService.java
public static final class Lifecycle extends SystemService {
//这里就是SystemService中调用的startService(),该方法直接返回的是AMS对象
public static ActivityManagerService startService(
SystemServiceManager ssm, ActivityTaskManagerService atm) {
sAtm = atm;
//下面是调用了SystemServiceManager的带有返回值的startService(),
return ssm.startService(ActivityManagerService.Lifecycle.class).getService();
}
}
进入SystemServiceManager的startService()方法,传入的参数是:ActivityManagerService.Lifecycle.class
这里和上一步ActivityTaskManagerService.Lifecycle步骤一致:
//SystemServiceManager.java
public SystemService startService(String className) {
//通过类加载器加载service
final Class<SystemService> serviceClass = loadClassFromLoader(className,
this.getClass().getClassLoader());
return startService(serviceClass);//此处进入startService()方法
}
public <T extends SystemService> T startService(Class<T> serviceClass) {
try {
final String name = serviceClass.getName();
Slog.i(TAG, "Starting " + name);
Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartService " + name);
// Create the service.
if (!SystemService.class.isAssignableFrom(serviceClass)) {
throw new RuntimeException("Failed to create " + name
+ ": service must extend " + SystemService.class.getName());
}
final T service;
try {
//通过类的构造器来创建service实例
Constructor<T> constructor = serviceClass.getConstructor(Context.class);
service = constructor.newInstance(mContext);
} catch (InstantiationException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service could not be instantiated", ex);
} catch (IllegalAccessException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service must have a public constructor with a Context argument", ex);
} catch (NoSuchMethodException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service must have a public constructor with a Context argument", ex);
} catch (InvocationTargetException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service constructor threw an exception", ex);
}
startService(service);
return service;//这里返回的service是 ActivityTaskManagerService.Lifecycle对象
} finally {
Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
}
}
这里就直接进入到ActivityManagerService.Lifecycle类的构造方法,在构造方法中创建AMS对象,通过getService()来获取AMS对象,至此AMS对象就创建好了。
//ActivityManagerService.java
public static final class Lifecycle extends SystemService {
private final ActivityManagerService mService;
private static ActivityTaskManagerService sAtm;
public Lifecycle(Context context) {
super(context);
//在构造方法中创建了AMS对象
mService = new ActivityManagerService(context, sAtm);
}
//这里就是SystemService中调用的startService(),该方法直接返回的是AMS对象
public static ActivityManagerService startService(
SystemServiceManager ssm, ActivityTaskManagerService atm) {
sAtm = atm;
//下面是调用了SystemServiceManager的带有返回值的startService(),
return ssm.startService(ActivityManagerService.Lifecycle.class).getService();
}
@Override
public void onStart() {
mService.start();
}
@Override
public void onBootPhase(int phase) {
mService.mBootPhase = phase;
if (phase == PHASE_SYSTEM_SERVICES_READY) {
mService.mBatteryStatsService.systemServicesReady();
mService.mServices.systemServicesReady();
} else if (phase == PHASE_ACTIVITY_MANAGER_READY) {
mService.startBroadcastObservers();
} else if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
mService.mPackageWatchdog.onPackagesReady();
}
}
@Override
public void onCleanupUser(int userId) {
mService.mBatteryStatsService.onCleanupUser(userId);
}
public ActivityManagerService getService() {
return mService;
}
}
1.3、启动AMS
关于AMS的启动,其实细心的同学已经注意到了,就是在ActivityManagerService.Lifecycle对象创建成功(在SystemServiceManager中创建)且在返回对象之前会调用一次不带返回值的startService(),
//SystemServiceManager.java
public <T extends SystemService> T startService(Class<T> serviceClass) {
try {
final String name = serviceClass.getName();
Slog.i(TAG, "Starting " + name);
Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartService " + name);
// Create the service.
if (!SystemService.class.isAssignableFrom(serviceClass)) {
throw new RuntimeException("Failed to create " + name
+ ": service must extend " + SystemService.class.getName());
}
final T service;
try {
//通过类的构造器来创建service实例
Constructor<T> constructor = serviceClass.getConstructor(Context.class);
service = constructor.newInstance(mContext);
} catch (InstantiationException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service could not be instantiated", ex);
} catch (IllegalAccessException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service must have a public constructor with a Context argument", ex);
} catch (NoSuchMethodException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service must have a public constructor with a Context argument", ex);
} catch (InvocationTargetException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service constructor threw an exception", ex);
}
//下面启动ActivityServiceManager.Lifecycle服务
startService(service);
return service;
} finally {
Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
}
}
//SystemServiceManager.java
//注意这里传递进来的service是ActivityServiceManager.Lifecycle对象
public void startService(@NonNull final SystemService service) {
// Register it.
mServices.add(service);
// Start it.
long time = SystemClock.elapsedRealtime();
try {
//这里会调到ActivityServiceManager.Lifecycle对的onStart()方法
service.onStart();
} catch (RuntimeException ex) {
throw new RuntimeException("Failed to start service " + service.getClass().getName()
+ ": onStart threw an exception", ex);
}
warnIfTooLong(SystemClock.elapsedRealtime() - time, service, "onStart");
}
接下来又回到ActivityServiceManager.Lifecycle中:
//ActivityServiceManager.java
public static final class Lifecycle extends SystemService {
private final ActivityManagerService mService;
private static ActivityTaskManagerService sAtm;
public Lifecycle(Context context) {
super(context);
mService = new ActivityManagerService(context, sAtm);
}
public static ActivityManagerService startService(
SystemServiceManager ssm, ActivityTaskManagerService atm) {
sAtm = atm;
return ssm.startService(ActivityManagerService.Lifecycle.class).getService();
}
@Override
public void onStart() {
//这里会调用AMS的onstart方法
mService.start();
}
@Override
public void onBootPhase(int phase) {
mService.mBootPhase = phase;
if (phase == PHASE_SYSTEM_SERVICES_READY) {
mService.mBatteryStatsService.systemServicesReady();
mService.mServices.systemServicesReady();
} else if (phase == PHASE_ACTIVITY_MANAGER_READY) {
mService.startBroadcastObservers();
} else if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
mService.mPackageWatchdog.onPackagesReady();
}
}
@Override
public void onCleanupUser(int userId) {
mService.mBatteryStatsService.onCleanupUser(userId);
}
public ActivityManagerService getService() {
return mService;
}
}
进入AMS的start()方法
//ActivityManagerService.java
private void start() {
removeAllProcessGroups();
mProcessCpuThread.start();
mBatteryStatsService.publish();
mAppOpsService.publish();
Slog.d("AppOps", "AppOpsService published");
LocalServices.addService(ActivityManagerInternal.class, mInternal);
mActivityTaskManager.onActivityManagerInternalAdded();
mPendingIntentController.onActivityManagerInternalAdded();
// Wait for the synchronized block started in mProcessCpuThread,
// so that any other access to mProcessCpuTracker from main thread
// will be blocked during mProcessCpuTracker initialization.
try {
mProcessCpuInitLatch.await();
} catch (InterruptedException e) {
Slog.wtf(TAG, "Interrupted wait during start", e);
Thread.currentThread().interrupt();
throw new IllegalStateException("Interrupted wait during start");
}
}
至此就是全部AMS的启动流程结束。
2、加入SystemServiceManager、添加安装器installer
//SystemServer.java
private void startBootstrapServices(@NonNull TimingsTraceAndSlog t) {
//这一步已经完成
ActivityTaskManagerService atm = mSystemServiceManager.startService(
ActivityTaskManagerService.Lifecycle.class).getService();
//AMS对象已经创建成功并启动
mActivityManagerService = ActivityManagerService.Lifecycle.startService(
mSystemServiceManager, atm);
//来看看下面的
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
mActivityManagerService.setInstaller(installer);
}
2.1、SystemServiceManager负责管理system service的创建、启动等生命周期事件
说明:
- 使用ArrayList来存放系统Service的对象
- 使用ArrayMap存放每一个service类的路径和对应的类加载器
- 通过类加载器加载类,然后通过反射来调用类的构造函数创建对象
public class SystemServiceManager {
private static final String TAG = "SystemServiceManager";
//............
// Services that should receive lifecycle events.
private final ArrayList<SystemService> mServices = new ArrayList<SystemService>();
// Map of paths to PathClassLoader, so we don't load the same path multiple times.
private final ArrayMap<String, PathClassLoader> mLoadedPaths = new ArrayMap<>();
private int mCurrentPhase = -1;
private UserManagerInternal mUserManagerInternal;
SystemServiceManager(Context context) {
mContext = context;
}
/**
* Starts a service by class name.
*
* @return The service instance.
*/
public SystemService startService(String className) {
final Class<SystemService> serviceClass = loadClassFromLoader(className,
this.getClass().getClassLoader());
return startService(serviceClass);
}
/**
* Starts a service by class name and a path that specifies the jar where the service lives.
*
* @return The service instance.
*/
public SystemService startServiceFromJar(String className, String path) {
PathClassLoader pathClassLoader = mLoadedPaths.get(path);
if (pathClassLoader == null) {
// NB: the parent class loader should always be the system server class loader.
// Changing it has implications that require discussion with the mainline team.
pathClassLoader = new PathClassLoader(path, this.getClass().getClassLoader());
mLoadedPaths.put(path, pathClassLoader);
}
final Class<SystemService> serviceClass = loadClassFromLoader(className, pathClassLoader);
return startService(serviceClass);
}
/*
* Loads and initializes a class from the given classLoader. Returns the class.
*/
@SuppressWarnings("unchecked")
private static Class<SystemService> loadClassFromLoader(String className,
ClassLoader classLoader) {
try {
return (Class<SystemService>) Class.forName(className, true, classLoader);
} catch (ClassNotFoundException ex) {
throw new RuntimeException("Failed to create service " + className
+ " from class loader " + classLoader.toString() + ": service class not "
+ "found, usually indicates that the caller should "
+ "have called PackageManager.hasSystemFeature() to check whether the "
+ "feature is available on this device before trying to start the "
+ "services that implement it. Also ensure that the correct path for the "
+ "classloader is supplied, if applicable.", ex);
}
}
/**
* Creates and starts a system service. The class must be a subclass of
* {@link com.android.server.SystemService}.
*
* @param serviceClass A Java class that implements the SystemService interface.
* @return The service instance, never null.
* @throws RuntimeException if the service fails to start.
*/
public <T extends SystemService> T startService(Class<T> serviceClass) {
try {
final String name = serviceClass.getName();
Slog.i(TAG, "Starting " + name);
Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartService " + name);
// Create the service.
if (!SystemService.class.isAssignableFrom(serviceClass)) {
throw new RuntimeException("Failed to create " + name
+ ": service must extend " + SystemService.class.getName());
}
final T service;
try {
Constructor<T> constructor = serviceClass.getConstructor(Context.class);
service = constructor.newInstance(mContext);
} catch (InstantiationException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service could not be instantiated", ex);
} catch (IllegalAccessException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service must have a public constructor with a Context argument", ex);
} catch (NoSuchMethodException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service must have a public constructor with a Context argument", ex);
} catch (InvocationTargetException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service constructor threw an exception", ex);
}
startService(service);
return service;
} finally {
Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
}
}
public void startService(@NonNull final SystemService service) {
// Register it.
mServices.add(service);
// Start it.
long time = SystemClock.elapsedRealtime();
try {
service.onStart();
} catch (RuntimeException ex) {
throw new RuntimeException("Failed to start service " + service.getClass().getName()
+ ": onStart threw an exception", ex);
}
warnIfTooLong(SystemClock.elapsedRealtime() - time, service, "onStart");
}
//.....此处省略了更多代码
}
2.2、Installer一个安装应用的安装器
责任:负责应用数据的create、restore、migrate、clear、destory、fixup、move等管理
原理:获取installd的Binder服务端代理对象来完成相关的操作;
public class Installer extends SystemService {
public Installer(Context context) {
this(context, false);
}
/**
* @param isolated indicates if this object should <em>not</em> connect to
* the real {@code installd}. All remote calls will be ignored
* unless you extend this class and intercept them.
*/
public Installer(Context context, boolean isolated) {
super(context);
mIsolated = isolated;
}
/**
* Yell loudly if someone tries making future calls while holding a lock on
* the given object.
*/
public void setWarnIfHeld(Object warnIfHeld) {
mWarnIfHeld = warnIfHeld;
}
@Override
public void onStart() {
if (mIsolated) {
mInstalld = null;
} else {
connect();
}
}
private void connect() {
IBinder binder = ServiceManager.getService("installd");
if (binder != null) {
try {
binder.linkToDeath(new DeathRecipient() {
@Override
public void binderDied() {
Slog.w(TAG, "installd died; reconnecting");
connect();
}
}, 0);
} catch (RemoteException e) {
binder = null;
}
}
if (binder != null) {
mInstalld = IInstalld.Stub.asInterface(binder);
try {
invalidateMounts();
} catch (InstallerException ignored) {
}
} else {
Slog.w(TAG, "installd not found; trying again");
BackgroundThread.getHandler().postDelayed(() -> {
connect();
}, DateUtils.SECOND_IN_MILLIS);
}
}
/**
* Do several pre-flight checks before making a remote call.
*
* @return if the remote call should continue.
*/
private boolean checkBeforeRemote() {
if (mWarnIfHeld != null && Thread.holdsLock(mWarnIfHeld)) {
Slog.wtf(TAG, "Calling thread " + Thread.currentThread().getName() + " is holding 0x"
+ Integer.toHexString(System.identityHashCode(mWarnIfHeld)), new Throwable());
}
if (mIsolated) {
Slog.i(TAG, "Ignoring request because this installer is isolated");
return false;
} else {
return true;
}
}
public long createAppData(String uuid, String packageName, int userId, int flags, int appId,
String seInfo, int targetSdkVersion) throws InstallerException {
if (!checkBeforeRemote()) return -1;
try {
return mInstalld.createAppData(uuid, packageName, userId, flags, appId, seInfo,
targetSdkVersion);
} catch (Exception e) {
throw InstallerException.from(e);
}
}
/**
* Batched version of createAppData for use with multiple packages.
*/
public void createAppDataBatched(String[] uuids, String[] packageNames, int userId, int flags,
int[] appIds, String[] seInfos, int[] targetSdkVersions) throws InstallerException {
if (!checkBeforeRemote()) return;
final int batchSize = 256;
for (int i = 0; i < uuids.length; i += batchSize) {
int to = i + batchSize;
if (to > uuids.length) {
to = uuids.length;
}
try {
mInstalld.createAppDataBatched(Arrays.copyOfRange(uuids, i, to),
Arrays.copyOfRange(packageNames, i, to), userId, flags,
Arrays.copyOfRange(appIds, i, to), Arrays.copyOfRange(seInfos, i, to),
Arrays.copyOfRange(targetSdkVersions, i, to));
} catch (Exception e) {
throw InstallerException.from(e);
}
}
}
public void restoreconAppData(String uuid, String packageName, int userId, int flags, int appId,
String seInfo) throws InstallerException {
if (!checkBeforeRemote()) return;
try {
mInstalld.restoreconAppData(uuid, packageName, userId, flags, appId, seInfo);
} catch (Exception e) {
throw InstallerException.from(e);
}
}
public void migrateAppData(String uuid, String packageName, int userId, int flags)
throws InstallerException {
if (!checkBeforeRemote()) return;
try {
mInstalld.migrateAppData(uuid, packageName, userId, flags);
} catch (Exception e) {
throw InstallerException.from(e);
}
}
public void clearAppData(String uuid, String packageName, int userId, int flags,
long ceDataInode) throws InstallerException {
if (!checkBeforeRemote()) return;
try {
mInstalld.clearAppData(uuid, packageName, userId, flags, ceDataInode);
} catch (Exception e) {
throw InstallerException.from(e);
}
}
public void destroyAppData(String uuid, String packageName, int userId, int flags,
long ceDataInode) throws InstallerException {
if (!checkBeforeRemote()) return;
try {
mInstalld.destroyAppData(uuid, packageName, userId, flags, ceDataInode);
} catch (Exception e) {
throw InstallerException.from(e);
}
}
public void fixupAppData(String uuid, int flags) throws InstallerException {
if (!checkBeforeRemote()) return;
try {
mInstalld.fixupAppData(uuid, flags);
} catch (Exception e) {
throw InstallerException.from(e);
}
}
public void moveCompleteApp(String fromUuid, String toUuid, String packageName,
int appId, String seInfo, int targetSdkVersion,
String fromCodePath) throws InstallerException {
if (!checkBeforeRemote()) return;
try {
mInstalld.moveCompleteApp(fromUuid, toUuid, packageName, appId, seInfo,
targetSdkVersion, fromCodePath);
} catch (Exception e) {
throw InstallerException.from(e);
}
}
}
总结:
1、通过反射来创建service的静态内部类Lifecycle的对象,在其构造方法中创建ATMS ,AMS对象;
2、创建后ATMS ,AMS对象后,调用SystemServer的startService()方法,调用Lifecycle的onStart()方法,在其中执行对应的ATMS ,AMS对象的start()方法启动服务
3、通过Lifecycle对象的getService()来获取ATMS ,AMS对象,然后进行对象的配置:加入SystemServerManager中,添加Installer安装器等