深入探讨Android启动优化策略
在当今激烈竞争的移动应用市场,应用的启动速度直接影响着用户的第一印象和满意度。作为主流的移动操作系统之一,Android的启动优化是开发者必须关注的关键领域。本文将详细介绍一些强大有效的Android启动优化策略,帮助你优化应用的启动过程,为用户创造更出色的体验。
冷启动与热启动
在着手优化之前,让我们深入了解Android应用的启动过程。Android应用的启动可分为冷启动和热启动两种情况。冷启动是指应用从完全关闭状态启动,而热启动则是从后台状态重新启动应用。尽管热启动也重要,但优化冷启动对提升用户体验影响更为显著,因为它需要加载更多资源和组件。
布局优化
应用启动时,系统需要加载布局资源并构建视图层级。因此,布局优化是提高启动速度的关键所在。
使用ConstraintLayout进行灵活布局
ConstraintLayout
是一种强大且高效的布局方式,能够降低嵌套层级,从而提升布局性能。它通过定义约束关系来定位视图,减少了传统布局中频繁的测量和布局操作。
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="javascript" cid="n2444" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
</androidx.constraintlayout.widget.ConstraintLayout></pre>
使用ViewStub实现延迟加载
ViewStub
是Android提供的一个特殊视图,充当占位符,在需要显示其内容时才会实例化和加载。在布局中使用ViewStub
能够有效延迟加载视图,从而加速启动时间。
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="javascript" cid="n2448" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ViewStub
android:id="@+id/myViewStub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="@layout/my_delayed_layout" />
</RelativeLayout></pre>
其中@layout/my_delayed_layout
是要延迟加载的布局资源的引用。
在需要显示ViewStub
内容的位置,调用ViewStub.inflate()
方法加载实际的布局内容:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="javascript" cid="n2452" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">ViewStub myViewStub = findViewById(R.id.myViewStub);
View inflatedView = myViewStub.inflate();</pre>
通常情况下,你可以根据用户交互或其他条件来触发加载。总之,与将视图设置为android:visibility="gone"
相比,使用ViewStub
是更好的方式实现延迟加载,特别是在启动时需要提升性能的情况下。
启动时序优化
精细控制启动时序能够显著提升启动速度,以下是一些优化策略。
呈现引人注目的闪屏界面
引入闪屏界面(Splash Screen)能够在应用加载资源的同时显示品牌标志或加载动画,缓解启动过程中的等待感。
在 res/values/styles.xml
中定义样式:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="javascript" cid="n2460" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><style name="AppTheme.Splash" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/splash_background</item>
</style></pre>
在 res/drawable
中创建 splash_background.xml
:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="javascript" cid="n2463" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/splashBackgroundColor" />
<item>
<bitmap
android:src="@drawable/app_logo"
android:gravity="center" />
</item>
</layer-list></pre>
在 AndroidManifest.xml
中设置 Splash Screen 样式:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="javascript" cid="n2466" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><activity
android:name=".SplashActivity"
android:theme="@style/AppTheme.Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity></pre>
降低主线程负担
主线程负责处理应用的UI操作,因此在启动过程中降低主线程工作量至关重要。
充分利用异步任务
通过将耗时任务转移到后台线程,避免了阻塞主线程。你可以使用 AsyncTask
或 ViewModel
来管理数据和UI更新。
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="javascript" cid="n2472" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class MyAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
// 执行耗时任务
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
// 更新UI或执行其他操作
}
}</pre>
智能后台初始化
将启动所需的初始化工作一部分放到后台线程中处理,以更快地显示应用的核心界面。
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="javascript" cid="n2476" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class StartupTask extends Application {
@Override
public void onCreate() {
super.onCreate();
// 在后台线程中执行初始化工作
new Thread(() -> {
// 执行初始化工作
}).start();
}
}</pre>
优化应用资源加载
在应用启动过程中,资源的加载可能是影响启动速度的一个重要因素。优化资源加载可以显著减少启动时间。
使用矢量图形资源
使用矢量图形资源(SVG、Vector Drawable)代替位图资源,可以减小APK的大小,同时适应不同屏幕密度的设备。
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="javascript" cid="n2482" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_vector_image" /></pre>
压缩位图资源
使用工具如 TinyPNG 可以压缩PNG和JPEG图片,减小APK的大小。另外,确保提供各种密度的图片资源,以适应不同屏幕的设备。
使用应用冷启动优化库
Android提供了一些优秀的启动优化库,可以帮助你自动管理和减少启动时间。
使用Hilt进行依赖注入
Hilt是Android官方提供的依赖注入库。通过使用Hilt,你可以将启动时创建的依赖关系移到后台,减少主线程上的工作。
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="javascript" cid="n2490" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">// 定义依赖关系
@Module
@InstallIn(SingletonComponent.class)
public class MyModule {
@Provides
public MyDependency provideMyDependency() {
return new MyDependency();
}
}
// 在Application中初始化Hilt
@HiltAndroidApp
public class MyApp extends Application {
}</pre>
使用Jetpack Compose重构UI
Jetpack Compose是一款现代的UI工具包,可以帮助你以声明性的方式构建界面。由于其性能优势,使用Compose可以提升应用的启动速度。
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="javascript" cid="n2494" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@Composable
fun MyScreen() {
Column {
Text(text = "Hello, Jetpack Compose!")
Button(onClick = { /* Do something */ }) {
Text(text = "Click me")
}
}
}</pre>
适当使用多进程
将某些耗时的初始化工作放在单独的进程中进行,可以减少主进程的负担,从而提升应用的启动速度。
创建后台进程
在AndroidManifest.xml中定义一个后台进程:
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="javascript" cid="n2500" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><application
android:name=".MyApplication"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:process=":background">
</application></pre>
执行耗时任务
在后台进程中执行耗时任务,例如初始化某些模块或资源:
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="javascript" cid="n2504" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class BackgroundProcessService extends Service {
@Override
public void onCreate() {
super.onCreate();
// 在后台进程中执行耗时任务
// ...
stopSelf(); // 任务完成后停止服务
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}</pre>
减少启动Activity的冷启动
Android的启动过程中,冷启动Activity的时间占比较大。以下是一些减少冷启动Activity时间的方法。
使用SingleTask启动模式
将冷启动Activity设置为SingleTask启动模式,可以在同一任务栈中复用已有的Activity实例,从而减少Activity的重复创建。
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="javascript" cid="n2510" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><activity
android:name=".MainActivity"
android:launchMode="singleTask">
</activity></pre>
使用Splash Screen优化冷启动体验
在Splash Screen中执行一些初始化操作,如预加载数据,从而将部分冷启动时间移至Splash Screen阶段。
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="javascript" cid="n2514" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 执行初始化操作,如预加载数据
// ...
startActivity(new Intent(this, MainActivity.class));
finish();
}
}</pre>
借助第三方开源库
Android Startup
:https://github.com/idisfkj/android-startup
(可去底部点击阅读原文)
android-startup提供一种在应用启动时能够更加简单、高效的方式来初始化组件。开发人员可以使用android-startup来简化启动序列,并显式地设置初始化顺序与组件之间的依赖关系。与此同时android-startup支持同步与异步等待,并通过有向无环图拓扑排序的方式来保证内部依赖组件的初始化顺序。
添加依赖
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="javascript" cid="n2521" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">repositories {
mavenCentral()
}
dependencies {
implementation 'io.github.idisfkj:android-startup:1.1.0'
}</pre>
定义初始化的组件
每一个初始化的组件都需要实现AndroidStartup抽象类,它实现了Startup接口。例如,下面定义一个SampleSecondStartup类来实现AndroidStartup抽象类:
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="javascript" cid="n2525" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">class SampleSecondStartup : AndroidStartup<Boolean>() {
override fun callCreateOnMainThread(): Boolean = false
override fun waitOnMainThread(): Boolean = true
override fun create(context: Context): Boolean {
// 模仿执行耗时
Thread.sleep(5000)
return true
}
override fun dependenciesByName(): List<String> {
return listOf("com.rousetime.sample.startup.SampleFirstStartup")
}
}</pre>
在dependenciesByName()方法中返回了com.rousetime.sample.startup.SampleFirstStartup,所以它能保证SampleFirstStartup优先执行完毕。
启动配置
提供两种配置,Manifiest中自动配置与Application中手动配置 下面给出自动配置示例:
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="javascript" cid="n2530" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><provider
android:name="com.rousetime.android_startup.provider.StartupProvider"
android:authorities="${applicationId}.android_startup"
android:exported="false">
<meta-data
android:name="com.rousetime.sample.startup.SampleFourthStartup"
android:value="android.startup" />
</provider></pre>
在Android Startup中提供了StartupProvider类,它是一个特殊的content provider,提供自动识别在manifest中配置的初始化组件。为了让其能够自动识别,需要在StartupProvider中定义标签。其中的name为定义的组件类,value的值对应为android.startup。
合理的管理启动任务,将会极大的提高应用的启动时间,获得更佳的启动体验。
结论
通过优化应用资源加载、使用优秀的启动优化库、适当使用多进程以及减少冷启动Activity的时间,你可以进一步提升Android应用的启动速度,为用户创造更佳的启动体验。不同的优化策略可以相互协作,以达到更好的效果。