Android性能优化系列之apk瘦身

利用TraceView分析启动时间


在onCreate开始和结尾打上trace.

Debug.startMethodTracing("TestApp"); ... Debug.stopMethodTracing();

  • 1

  • 2

  • 3

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

  • 1

  • 2

  • 3

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

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

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

  • 1

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

  • 1

通过adb pull将其导出到本地

adb pull /sdcard/TestApp.trace ~/testSpeed.trace

  • 1

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

  • 1

打开DDMS分析trace文件,会出现以下的界面

这里写图片描述

这里写图片描述

展开后,大多数有以下两个类别:

Parents:调用该方法的父类方法

Children:该方法调用的子类方法

如果该方法含有递归调用,可能还会多出两个类别:

Parents while recursive:递归调用时所涉及的父类方法

Children while recursive:递归调用时所涉及的子类方法

开发者最关心的数据有:

很重要的指标:Calls + Recur Calls / Total , 最重要的指标: Cpu Time / Call

因为我们最关心的有两点,一是调用次数不多,但每次调用却需要花费很长时间的函数。这个可以从Cpu Time / Call反映出来。另外一个是那些自身占用时间不长,但调用却非常频繁的函数。这个可以从Calls + Recur Calls / Total 反映出来。

然后我们可以通过这样的分析,来查看启动的时候,哪些进行了耗时操作,然后进行相应的处理,比如能不在主线程中做的,放入子线程中,还有一些懒加载处理,比如有些Application中做了支付SDK的初始化,用户又不会一打开App就要支付,对其进行懒加载。

启动页优化


平时我们在开发App时,都会设置一个启动页SplashActivity,然后2或3秒后,并且SplashActivity里面可以去做一些MainActivity的数据的预加载,然后需要通过意图传到MainActivity。

优点:启动速度有所加快

缺点:最终还是要进入首页,在进入首页的时候,首页复杂的View渲染以及必须在UI线程执行的业务逻辑,仍然拖慢了启动速度。启动页简单执行快,首页复杂执行慢,前轻后重。

思路:能否在启动页的展示的同时,首页的View就能够被加载,首页的业务逻辑就能够被执行?

优化方向:

把SplashActivity改成SplashFragment,应用程序的入口仍然是MainActivity,在MainActivity中先展示SplashFragment,当SplashFragment显示完毕后再将它remove,同时在SplashFragment的2S的友好时间内进行网络数据缓存,在窗口加载完毕后,我们加载activity_main的布局,考虑到这个布局有可能比较复杂,耽误View的解析时间,采用ViewStub的形式进行懒加载。这样一开始只要加载SplashFragment所展示的布局就Ok了。

代码:

MainActivity .Java

public class MainActivity extends FragmentActivity { private MyHandler mHandler=new MyHandler(this); public static final String TAG="MainActivity"; private ProgressBar mNetLoadingBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG,"onCreate"); setContentView(R.layout.activity_main); final SplashFragment splashFragment = new SplashFragment(); final ViewStub mainLayout = (ViewStub) findViewById(R.id.content_viewstub); //1、一上来首先显示启动页面 FragmentManager supportFragmentManager = getSupportFragmentManager(); if (supportFragmentManager != null) { FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); if (fragmentTransaction != null) { fragmentTransaction.replace(R.id.container, splashFragment); fragmentTransaction.commit(); } } //2、如果主页有网络等耗时操作,可以现在就开始 new Thread(new Runnable() { @Override public void run() { //耗时3500 SystemClock.sleep(3000); mHandler.sendEmptyMessage(0); } }).start(); //3、渲染完毕后,立刻加载主页布局 getWindow().getDecorView().post(new Runnable() { @Override public void run() { Log.d(TAG," getWindow().getDecorView().post"); View mainView = mainLayout.inflate(); initView(mainView); } }); //4、 启动页有动画,延迟一下,播放完动画,执行remove getWindow().getDecorView().post(new Runnable() { @Override public void run() { mHandler.postDelayed(new DelayRunnableImpl(MainActivity.this, splashFragment), 2000); } }); } @Override protected void onResume() { super.onResume(); Log.d(TAG,"onResume"); } /** * 初始化主页View */ private void initView(View pMainView) { if (pMainView != null) { mNetLoadingBar = (ProgressBar) pMainView.findViewById(R.id.progressbar); mNetLoadingBar.setVisibility(View.VISIBLE); } } private static class MyHandler extends Handler { private WeakReference<MainActivity> wRef; private MyHandler(MainActivity pActivity) { this.wRef = new WeakReference<MainActivity>(pActivity); } @Override public void handleMessage(Message msg) { MainActivity mainActivity = wRef.get(); if (mainActivity != null) { mainActivity.mNetLoadingBar.setVisibility(View.GONE); } } } private class DelayRunnableImpl implements Runnable { WeakReference<Context> contextWref; WeakReference<Fragment> fragmentWref; private DelayRunnableImpl(Context pContext, Fragment pFragment) { this.contextWref = new WeakReference<>(pContext); this.fragmentWref = new WeakReference<>(pFragment); } @Override public void run() { FragmentActivity context = (FragmentActivity) contextWref.get(); if (context != null) { FragmentManager supportFragmentManager = context.getSupportFragmentManager(); if (supportFragmentManager != null) { FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); SplashFragment fragment = (SplashFragment) fragmentWref.get(); if (fragment != null) { fragmentTransaction.remove(fragment); fragmentTransaction.commit(); } } } } } @Override protected void onDestroy() { super.onDestroy(); if (mHandler != null) { mHandler.removeCallbacksAndMessages(null); } } }

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 17

  • 18

  • 19

  • 20

  • 21

  • 22

  • 23

  • 24

  • 25

  • 26

  • 27

  • 28

  • 29

  • 30

  • 31

  • 32

  • 33

  • 34

  • 35

  • 36

  • 37

  • 38

  • 39

  • 40

  • 41

  • 42

  • 43

  • 44

  • 45

  • 46

  • 47

  • 48

  • 49

  • 50

  • 51

  • 52

  • 53

  • 54

  • 55

  • 56

  • 57

  • 58

  • 59

  • 60

  • 61

  • 62

  • 63

  • 64

  • 65

  • 66

  • 67

  • 68

  • 69

  • 70

  • 71

  • 72

  • 73

  • 74

  • 75

  • 76

  • 77

  • 78

  • 79

  • 80

  • 81

  • 82

  • 83

  • 84

  • 85

  • 86

  • 87

  • 88

  • 89

  • 90

  • 91

  • 92

  • 93

  • 94

  • 95

  • 96

  • 97

  • 98

  • 99

  • 100

  • 101

  • 102

  • 103

  • 104

  • 105

  • 106

  • 107

  • 108

  • 109

  • 110

  • 111

  • 112

  • 113

  • 114

  • 115

  • 116

  • 117

  • 118

  • 119

  • 120

  • 121

  • 122

  • 123

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 17

  • 18

  • 19

  • 20

  • 21

  • 22

  • 23

  • 24

  • 25

  • 26

  • 27

  • 28

  • 29

  • 30

  • 31

  • 32

  • 33

  • 34

  • 35

  • 36

  • 37

  • 38

  • 39

  • 40

  • 41

  • 42

  • 43

  • 44

  • 45

  • 46

  • 47

  • 48

  • 49

  • 50

  • 51

  • 52

  • 53

  • 54

  • 55

  • 56

  • 57

  • 58

  • 59

  • 60

  • 61

  • 62

  • 63

  • 64

  • 65

  • 66

  • 67

  • 68

  • 69

  • 70

  • 71

  • 72

  • 73

  • 74

  • 75

  • 76

  • 77

  • 78

  • 79

  • 80

  • 81

  • 82

  • 83

  • 84

  • 85

  • 86

  • 87

  • 88

  • 89

  • 90

  • 91

  • 92

  • 93

  • 94

  • 95

  • 96

  • 97

  • 98

  • 99

  • 100

  • 101

  • 102

  • 103

  • 104

  • 105

  • 106

  • 107

  • 108

  • 109

  • 110

  • 111

  • 112

  • 113

  • 114

  • 115

  • 116

  • 117

  • 118

  • 119

  • 120

  • 121

  • 122

  • 123

SplashFragment .java

public class SplashFragment extends Fragment { public SplashFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View ret = inflater.inflate(R.layout.fragment_splash, container, false); initView(ret); return ret; } private void initView(View ret) { if (ret != null) { ImageView imageView = (ImageView) ret.findViewById(R.id.laucher_logo); playAnimator(imageView); } } private void playAnimator(ImageView pView) { if (pView != null) { PropertyValuesHolder pvhA = PropertyValuesHolder.ofFloat("alpha", 1f, 0.7f, 0.1f); // PropertyValuesHolder pvhR= PropertyValuesHolder.ofFloat("rotationX", 0.0F, 360.0F); ObjectAnimator.ofPropertyValuesHolder(pView, pvhA).setDuration(2000).start(); } } }

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 17

  • 18

  • 19

  • 20

  • 21

  • 22

  • 23

  • 24

  • 25

  • 26

  • 27

  • 28

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 17

  • 18

  • 19

  • 20

  • 21

  • 22

  • 23

  • 24

  • 25

  • 26

  • 27

  • 28

activity_mian.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="zhangwan.wj.com.myshare.activity.MainActivity"> <ViewStub android:id="@+id/content_viewstub" android:layout="@layout/main_layout" android:layout_width="match_parent" android:layout_height="match_parent"/> <FrameLayout android:id="@+id/container" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 17

  • 18

  • 19

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 17

  • 18

  • 19

fragment.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="zhangwan.wj.com.myshare.fragment.SplashFragment"> <ImageView android:id="@+id/laucher_logo" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/launch" /> </FrameLayout>

/> `

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 17

  • 18

  • 19

[外链图片转存中…(img-vIlRNH80-1716492202956)]

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 17

  • 18

  • 19

fragment.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="zhangwan.wj.com.myshare.fragment.SplashFragment"> <ImageView android:id="@+id/laucher_logo" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/launch" /> </FrameLayout>

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值