TomerZ
TomerZ

Reputation: 635

Android app slow initial startup time

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...)

Upvotes: 10

Views: 9884

Answers (3)

miqueloi
miqueloi

Reputation: 698

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.

Upvotes: 11

Embydextrous
Embydextrous

Reputation: 1661

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.

Upvotes: 9

Inverce
Inverce

Reputation: 1496

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.

Upvotes: 2

Related Questions