Android app slow initial startup time

流过昼夜 提交于 2020-01-03 15:21:22

问题


After I started using UI elements from the android support design library the app initial load time has become really slow (about 8 seconds!) and I am really not sure why.

I ran method tracking during most of the startup (It takes time for android studio to start running the cpu monitor) and found it spend 4 seconds on: dalvik.system.DexFile.openDexFile, Im not sure why this is taking so long.

Any ideas? (I didnt add any code since there is a lot of code in my app and I dont know where the problem is coming from...)


回答1:


I experienced an increase in speed when building the release app instead of the debug version that I normaly run.

I'm not sure why it works, but I guess it has to be because of the way the compiler links the ext libs into the apk. I once saw an interview with Chet Haase who is one of the devs on the android ui platform, who explained how they were trying to show the first activity on the app as quickly as possible to avoid dull splash screen. Maybe that feature is somehow enabled in the release build process.

EDIT: The correct answer is written below by @Embydextrous. It is cause by the dexing of the app in debug mode.




回答2:


This usually happens with debug builds but can also happen with release builds.

In case of multiple dex files. The primary dex file (classes.dex) is loaded first which then loads other dex files.

It usually does not show in release builds because of proguard which removes unused methods and reduces method count so only a single .dex is made. In debug build proguard is not used so there are multiple dex files.

However in very large apps like AirBnb, Facebook, twitter, etc. there are multiple dex files. And hence app launch delays which can be optimized using dex optimizers.




回答3:


As name suggest openDexFile is android process that loads method from dex file, the bigger and the more files that are the longer load.

So my answer to your question is just reduce number of methods you have and prefer lazy load of libraries instead of initializing them in Application

So instead of initializing your net lib in App.onCreate initialize it on first request, do not create your db until its needed.

Also use android:windowBackground in your app thame to show user preloader instead of whitescreen :)

How to better see method counts:

In android studio under plugins install "Android Methods Count", that is very simple plugi, and i do think that its name suggest what it does :)

Now given your knowladge of your project reduce, number of libraries that you are using right now, you may want to use gradle->root->Tasks->android->AndroidDependencies to see what additional libraries are added to your project.

Also remember not to use core play-services and only libs that you are actually using.




回答4:


You can check whats you initialized in Application file



来源:https://stackoverflow.com/questions/38475179/android-app-slow-initial-startup-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!