一、APP启动
冷启动
系统不存在APP进程时,启动APP。如:首次通过桌面图标启动。
冷启动主要包含以下几步:
(1) 点击屏幕图标,launcher进程会通过binder 请求system_server进程,告诉AMS启动APP,AMS去PMS中查询APP的相关信息。
(2) 找到APP信息后,通过socket 的方式告诉zygote进程,启动APP进程。
(3) zygote进程fork出APP进程。
(4) APP进程会通过binder 调用AMS的attachApplication,AMS再通过binder 调用APP进程中Application的onCreate方法。
(5) 执行完毕Application的onCreate方法后,AMS继续通过一系列处理,最后通过binder 触发APP进程中Activity的生命周期:onCreate、onStart、onResume。
详细流程可以看AMS-Activity启动流程
热启动
APP成功展示,被切换到后台,再次启动APP。如:点击home键后,再展示APP。
温启动
APP退出但是进程仍然存在,重新启动APP。如:连续点击back键退出app,再马上启动app。
二、启动优化
可优化的节点
通过APP冷启动的流程,可以看到1、2、3步骤都是系统进行的操作,很难进行优化;而4、5两步,分别执行Application的onCreate和Activity的onCreate、onStart,应尽量避免耗时操作。
工具
1、通过adb命令
adb shell am start -W 包名/入口Activity(示例:adb shell am start -W com.niiiico.wgdemo/com.niiiico.wgdemo.MainActivity)
LaunchState: 启动方式
TotalTime: 新应用的启动耗时
WaitTime: 前一个应用的onPause + 新应用的启动耗时
2、通过profile
(1)点击app->Edit Configurations
(2)Edit Configurations设置
如图所示:
<1>选择app
<2>选择Profiling
<3>选中Start this recofing on startup
<4>勾选CPU activity
<5>选择Sample Java Methods
<6>点击OK
第<5>步中,各个选项的意义;
Sample Java Methods:对java方法采样跟踪
Trace Java Methods:对java方法全量跟踪
Sample C/C++ Functions:对 C/C++ 函数采样跟踪
Trace System Calls:对系统调用进行跟踪
(3)点击run->profile->app,启动app。
(4)待app完全展示后,点击stop停止跟踪。
(5)分析面板
(6)点击线程进行切换;如:点击main线程,在右侧就会展示出详细信息。
(7)详细分析
通过切换tab,切换展示方式,进行详细分析。可以看出,统计时长16s,main线程中执行耗时6s,其中在Application的onCreate中,由于调用了sleep()函数耗时5s。
优化思路
通过以上方法,可以得到APP的启动时间,也可以找到耗时代码。通常为了避免影响启动速度,可以有以下思路。
1、onCreate中少写耗时代码,如:懒加载等,将一些不必要代码延后加载。
2、优化xml布局,避免过度绘制,节省UI的绘制时间。
3、优化代码,减少执行时间。
4、通过产品层面进行修改,如:过渡页面、广告页等。
三、黑白屏
原因
通过上文可知,启动APP是有耗时的,会感觉到点击 APP图标时会有 “延迟” 现象。为了解决这一问题,Google 在 APP创建的过程中,先展示一个空白页面,用户体会到点击图标之后立马就有响应。
这个空白页面的颜色是根据我们在 AndroiMainfest 文件中配置的主题背景颜色来决定的,现在一般默认是白色。
解决思路
1、取消空白预览,把锅甩给系统,:( 不推荐
找到系统的APP的主题,添加如下代码:
<!--设置系统取消预览(空白窗口)-->
<item name="android:windowDisablePreview">true</item>
<!--设置背景透明-->
<item name="android:windowIsTranslucent">true</item>
当然这种行为是不推荐的,用户点击图标之后,后有长时间的等待,影响使用体验。
2、修改背景图,:)推荐
(1)新建自定义主题,设置window的背景
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.WGDemo" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
<!-- 新建主题,继承自原来的主题,设置window的背景 -->
<style name="Theme.My" parent="Theme.WGDemo">
<item name="android:windowBackground">@drawable/launcher_bg</item>
</style>
</resources>
(2)将启动页主题设置成自定义主题
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.niiiico.wgdemo">
<application
android:name=".SleepApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.WGDemo">
<activity
android:name=".LauncherActivity"
android:exported="true"
android:theme="@style/Theme.My">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
</application>
</manifest>
下面是展示效果,推荐使用这种方式进行优化。
四、黑白屏相关源码
由AMS-Activity启动流程可知,冷启动时会执行到ActivityStarter.startActivityUnchecked方法,然后调用到ActivityStack.startActivityLocked;由于SHOW_APP_STARTING_PREVIEW默认为true,调用到ActivityRecord.showStartingWindow。
class ActivityStarter {
private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord,
IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,
ActivityRecord[] outActivity) {
...
mTargetStack.startActivityLocked(mStartActivity, topFocused, newTask, mKeepCurTransition, mOptions);
...
return START_SUCCESS;
}
}
class ActivityStack<T extends StackWindowController> extends ConfigurationContainer implements StackWindowListener {
// Set to false to disable the preview that is shown while a new activity is being started.
private static final boolean SHOW_APP_STARTING_PREVIEW = true;
void startActivityLocked(ActivityRecord r, ActivityRecord focusedTopActivity,
boolean newTask, boolean keepCurTransition, ActivityOptions options) {
...
if (!isHomeOrRecentsStack() || numActivities() > 0) {
...
if (r.mLaunchTaskBehind) {
...
//
} else if (SHOW_APP_STARTING_PREVIEW && doShow) {
...
r.showStartingWindow(prev, newTask, isTaskSwitch(r, focusedTopActivity));
}
} else {
...
}
}
}
final class ActivityRecord extends ConfigurationContainer implements AppWindowContainerListener {
void showStartingWindow(ActivityRecord prev, boolean newTask, boolean taskSwitch) {
showStartingWindow(prev, newTask, taskSwitch, false /* fromRecents */);
}
void showStartingWindow(ActivityRecord prev, boolean newTask, boolean taskSwitch,
boolean fromRecents) {
...
final boolean shown = mWindowContainerController.addStartingWindow(packageName, theme,
compatInfo, nonLocalizedLabel, labelRes, icon, logo, windowFlags,
prev != null ? prev.appToken : null, newTask, taskSwitch, isProcessRunning(),
allowTaskSnapshot(),
mState.ordinal() >= RESUMED.ordinal() && mState.ordinal() <= STOPPED.ordinal(),
fromRecents);
...
}
}
ActivityRecord.showStartingWindow又会通过AppWindowContainerController.addStartingWindow继续执行,最终通过startingData.createStartingSurface(container)生成StartingSurface。
public class AppWindowContainerController extends WindowContainerController<AppWindowToken, AppWindowContainerListener> {
public boolean addStartingWindow(String pkg, int theme, CompatibilityInfo compatInfo,
CharSequence nonLocalizedLabel, int labelRes, int icon, int logo, int windowFlags,
IBinder transferFrom, boolean newTask, boolean taskSwitch, boolean processRunning,
boolean allowTaskSnapshot, boolean activityCreated, boolean fromRecents) {
synchronized (mWindowMap) {
...
scheduleAddStartingWindow();
}
return true;
}
void scheduleAddStartingWindow() {
if (!mService.mAnimationHandler.hasCallbacks(mAddStartingWindow)) {
if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Enqueueing ADD_STARTING");
mService.mAnimationHandler.postAtFrontOfQueue(mAddStartingWindow);
}
}
private final Runnable mAddStartingWindow = new Runnable() {
@Override
public void run() {
...
try {
surface = startingData.createStartingSurface(container);
} catch (Exception e) {
Slog.w(TAG_WM, "Exception when adding starting window", e);
}
...
}
};
}
startingData.createStartingSurface会调用PhoneWindowManager.addSplashScreen方法,在方法内部,会获取R.styleable.Window_windowBackground属性,替换context。
class SplashScreenStartingData extends StartingData {
...
@Override
StartingSurface createStartingSurface(AppWindowToken atoken) {
return mService.mPolicy.addSplashScreen(atoken.token, mPkg, mTheme, mCompatInfo,
mNonLocalizedLabel, mLabelRes, mIcon, mLogo, mWindowFlags,
mMergedOverrideConfiguration, atoken.getDisplayContent().getDisplayId());
}
}
public class PhoneWindowManager implements WindowManagerPolicy {
public WindowManagerPolicy.StartingSurface addSplashScreen(IBinder appToken, String packageName, int theme,
CompatibilityInfo compatInfo, CharSequence nonLocalizedLabel, int labelRes, int icon,
int logo, int windowFlags, Configuration overrideConfig, int displayId) {
...
WindowManager wm = null;
View view = null;
try {
Context context = mContext;
...
if (overrideConfig != null && !overrideConfig.equals(EMPTY)) {
final Context overrideContext = context.createConfigurationContext(overrideConfig);
overrideContext.setTheme(theme);
final TypedArray typedArray = overrideContext.obtainStyledAttributes(
com.android.internal.R.styleable.Window);
final int resId = typedArray.getResourceId(R.styleable.Window_windowBackground, 0);
if (resId != 0 && overrideContext.getDrawable(resId) != null) {
// We want to use the windowBackground for the override context if it is
// available, otherwise we use the default one to make sure a themed starting
// window is displayed for the app.
context = overrideContext;
}
typedArray.recycle();
}
...
final WindowManager.LayoutParams params = win.getAttributes();
params.token = appToken;
params.packageName = packageName;
params.windowAnimations = win.getWindowStyle().getResourceId(
com.android.internal.R.styleable.Window_windowAnimationStyle, 0);
params.privateFlags |=
WindowManager.LayoutParams.PRIVATE_FLAG_FAKE_HARDWARE_ACCELERATED;
params.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
if (!compatInfo.supportsScreen()) {
params.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_COMPATIBLE_WINDOW;
}
params.setTitle("Splash Screen " + packageName);
addSplashscreenContent(win, context);
wm = (WindowManager) context.getSystemService(WINDOW_SERVICE);
view = win.getDecorView();
wm.addView(view, params);
// Only return the view if it was successfully added to the
// window manager... which we can tell by it having a parent.
return view.getParent() != null ? new SplashScreenSurface(view, appToken) : null;
}
...
return null;
}
}