How to optimize Roboguice apps to start faster?
Background
I have to use Roboguice for an app, which handles a lot of injections on many classes.
The app also has a splash screen class which extends from RoboSplashActivity .
The problem
As the app got more and more complex, more time was spent on the splash screen activity, and it even shows its content layout after a while , meaning the user sees a blank screen about 1-2 seconds before there is even a splash image.
What I've tried
At the beginning I thought it was because the splash image was too have (since it had multiple layers of images) so I tried setting a simple color. Turns out it's not the reason.
Then I thought that it might be that the app takes a lot of space, so I created a totally new project with the same size, and it worked fine. so a large app isn't the reason for slow start up.
Then I thought that it's the RoboSplashActivity's fault, so I've replaced it with a new activity (extending Activity instead ) that only shows a solid color background. the background showed after a while , almost the same time as using RoboSplashActivity . Still not the reason for the blank screen.
Now what I think is that it's Roboguice's fault, and that I should somehow delay its initialization to the time that something is shown on the screen, so that at least the user will see something while it's loading.
The question
Is it possible to optimize Roboguice to have the minimal start time ?
Maybe delay its initialization that is done on other files ?
Solved
I'm using Guice and I had the same problem kind of problem. I'm sure the solution to your problem is very similar.
The thing that is taking a lot of time is to create the dependency graph as defined in your modules. There's a lot of reflection going on and it will take some time to analyze all your bindings. You need to to move your dependency injection setup to a separate thread/task that you kick-off in your SplashActivity.
Since you're using RoboGuice there's already an activity designed specifically for this. Have a look at the RoboSplashActivity.
UPDATE: I can't believe that I overlooked the part where you wrote that you already extend RoboSplashActivity. Sorry about that. As I wrote in the comment:
Do you subclass Application and do any dependency injection there?
As a side-note when it comes to startup time: You may want to look into the Stage setting for controlling the way the dependency injection is set up. For Guice there's three modes, each with different startup times. It seems like RoboGuice has the same and it defaults to Stage.PRODUCTION.
Comments
Post a Comment