当前位置:   article > 正文

App优化之提升你的App启动速度之实例挑战_提高app启动速度的方法

提高app启动速度的方法

1, 代码分析

因为这个App集成了Bugly, Push, Feedback等服务, 所以Application的onCreate有很多第三方平台的初始化工作...

  1. public class GithubApplication extends MultiDexApplication {
  2. @Override
  3. public void onCreate() {
  4. super.onCreate();
  5. // init logger.
  6. AppLog.init();
  7. // init crash helper
  8. CrashHelper.init(this);
  9. // init Push
  10. PushPlatform.init(this);
  11. // init Feedback
  12. FeedbackPlatform.init(this);
  13. // init Share
  14. SharePlatform.init(this);
  15. // init Drawer image loader
  16. DrawerImageLoader.init(new AbstractDrawerImageLoader() {
  17. @Override
  18. public void set(ImageView imageView, Uri uri, Drawable placeholder) {
  19. ImageLoader.loadWithCircle(GithubApplication.this, uri, imageView);
  20. }
  21. });
  22. }
  23. }

当前冷启动效果: 

 
code_start_before_optimize

可以看到启动时白屏了很长时间.

2, Traceview上场

接下来我们结合我们 上文 的理论知识, 和介绍的Traceview工具, 来分析下Application的onCreate耗时.

在onCreate开始和结尾打上trace.

  1. Debug.startMethodTracing("GithubApp");
  2. ...
  3. Debug.stopMethodTracing();

运行程序, 会在sdcard上生成一个"GithubApp.trace"的文件.

注意: 需要给程序加上写存储的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

通过adb pull将其导出到本地

adb pull /sdcard/GithubApp.trace ~/temp

广告: adb的众多用法, 可以参考我的 另一篇文

打开DDMS分析trace文件


ddms_open_trace

分析trace文件


traceview_ui
  1. 在下方的方法区点击"Real Time/Call", 按照方法每次调用耗时降序排.
  2. 耗时超过500ms都是值得注意的.
  3. 看左边的方法名, 可以看到耗时大户就是我们用的几大平台的初始化方法, 特别是Bugly, 还加载native的lib, 用ZipFile操作等.
  4. 点击每个方法, 可以看到其父方法(调用它的)和它的所有子方法(它调用的).
  5. 点击方法时, 上方的该方法执行时间轴会闪动, 可以看该方法的执行线程及相对时长.

3, 调整Application onCreate再试

既然已经知道了哪些地方耗时长, 我们不妨调整下Application的onCreate实现, 一般来说我们可以将这些初始化放在一个单独的线程中处理, 为了方便今后管理, 这里我用了一个InitializeService的IntentService来做初始化工作.

明确一点, IntentService不同于Service, 它是工作在后台线程的.

InitializeService.java代码如下:

  1. package com.anly.githubapp.compz.service;
  2. import android.app.IntentService;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.graphics.drawable.Drawable;
  6. import android.net.Uri;
  7. import android.widget.ImageView;
  8. import com.anly.githubapp.common.wrapper.AppLog;
  9. import com.anly.githubapp.common.wrapper.CrashHelper;
  10. import com.anly.githubapp.common.wrapper.FeedbackPlatform;
  11. import com.anly.githubapp.common.wrapper.ImageLoader;
  12. import com.anly.githubapp.common.wrapper.PushPlatform;
  13. import com.anly.githubapp.common.wrapper.SharePlatform;
  14. import com.mikepenz.materialdrawer.util.AbstractDrawerImageLoader;
  15. import com.mikepenz.materialdrawer.util.DrawerImageLoader;
  16. /**
  17. * Created by mingjun on 16/8/25.
  18. */
  19. public class InitializeService extends IntentService {
  20. private static final String ACTION_INIT_WHEN_APP_CREATE = "com.anly.githubapp.service.action.INIT";
  21. public InitializeService() {
  22. super("InitializeService");
  23. }
  24. public static void start(Context context) {
  25. Intent intent = new Intent(context, InitializeService.class);
  26. intent.setAction(ACTION_INIT_WHEN_APP_CREATE);
  27. context.startService(intent);
  28. }
  29. @Override
  30. protected void onHandleIntent(Intent intent) {
  31. if (intent != null) {
  32. final String action = intent.getAction();
  33. if (ACTION_INIT_WHEN_APP_CREATE.equals(action)) {
  34. performInit();
  35. }
  36. }
  37. }
  38. private void performInit() {
  39. AppLog.d("performInit begin:" + System.currentTimeMillis());
  40. // init Drawer image loader
  41. DrawerImageLoader.init(new AbstractDrawerImageLoader() {
  42. @Override
  43. public void set(ImageView imageView, Uri uri, Drawable placeholder) {
  44. ImageLoader.loadWithCircle(getApplicationContext(), uri, imageView);
  45. }
  46. });
  47. // init crash helper
  48. CrashHelper.init(this.getApplicationContext());
  49. // init Push
  50. PushPlatform.init(this.getApplicationContext());
  51. // init Feedback
  52. FeedbackPlatform.init(this.getApplication());
  53. // init Share
  54. SharePlatform.init(this.getApplicationContext());
  55. AppLog.d("performInit end:" + System.currentTimeMillis());
  56. }
  57. }

GithubApplication的onCreate改成:

  1. public class GithubApplication extends MultiDexApplication {
  2. @Override
  3. public void onCreate() {
  4. super.onCreate();
  5. // init logger.
  6. AppLog.init();
  7. InitializeService.start(this);
  8. }
  9. }

看看现在的效果:

 
improved-1.gif

可以看到提升了很多, 然后还有一点瑕疵, 就是起来的时候会有一个白屏, 如果手机较慢的话, 这个白屏就会持续一段时间, 不太友好.

那么还有没有什么办法优化呢?

4, 给我们的应用窗口弄一个PlaceHolder

Android最新的Material Design有这么个 建议 的. 建议我们使用一个placeholder UI来展示给用户直至App加载完毕.

怎么做呢?

给Window加上背景

如第3节所言, 当App没有完全起来时, 屏幕会一直显示一块空白的窗口(一般来说是黑屏或者白屏, 根据App主题).

前文理论基础 有说到, 这个空白的窗口展示跟主题相关, 那么我们是不是可以从首屏的主题入手呢? 恰好有一个windowBackground的主题属性, 我们来给Splash界面加上一个主题, 带上我们想要展示的背景.

做一个logo_splash的背景:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  3. <!-- 底层白色 -->
  4. <item android:drawable="@color/white" />
  5. <!-- 顶层Logo居中 -->
  6. <item>
  7. <bitmap
  8. android:gravity="center"
  9. android:src="@drawable/ic_github" />
  10. </item>
  11. </layer-list>

弄一个主题:

  1. <style name="SplashTheme" parent="AppTheme">
  2. <item name="android:windowBackground">@drawable/logo_splash</item>
  3. </style>

将一个什么不渲染布局的Activity作为启动屏

写一个什么都不做的LogoSplashActivity.

  1. public class LogoSplashActivity extends BaseActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. // 注意, 这里并没有setContentView, 单纯只是用来跳转到相应的Activity.
  6. // 目的是减少首屏渲染
  7. if (AppPref.isFirstRunning(this)) {
  8. IntroduceActivity.launch(this);
  9. }
  10. else {
  11. MainActivity.launch(this);
  12. }
  13. finish();
  14. }
  15. }

在AndroidManifest.xml中设置其为启动屏, 并加上主题:

  1. <activity
  2. android:name=".ui.module.main.LogoSplashActivity"
  3. android:screenOrientation="portrait"
  4. android:theme="@style/SplashTheme">
  5. <intent-filter>
  6. <action android:name="android.intent.action.MAIN"/>
  7. <category android:name="android.intent.category.LAUNCHER"/>
  8. </intent-filter>
  9. </activity>

5, 最终的效果

让我们来看下最终的效果:

 
improved-2.gif

相比之前, 呈现给用户的不再是一个白屏了, 带上了logo, 当然这个背景要显示什么, 我们可以根据实际情况来自定义.

这种优化, 对于有些Application内的初始化工作不能移到子线程做的情况, 是非常友好的. 可以避免我们的App长时间的呈现给用户一个空白的窗口.

6, 结语

照例, 总结下. 
这次关于App启动时间的优化, 写了两篇. 写这么多, 还是想传达下个人做技术的思想, 也算是个人的经验回顾, 抛砖引玉.

实际场景可能远比这个复杂,在此更多的提供一种分析思路~欢迎扩展

矫情了, 还是总结下本文相关的吧:

  1. Application的onCreate中不要做太多事情.
  2. 首屏Activity尽量简化.
  3. 善用工具分析.
  4. 多阅读官方文档, 很多地方貌似无关, 实际有关联, 例如这次就用了Material Design文档中的解决方案.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/291775
推荐阅读
相关标签
  

闽ICP备14008679号