For the last few years, i have been working on post development activities for one of my personal Android application. I have understood performance optimization plays an important role in making an application successful.
Optimizing the code is the most important thing in performance improvement tasks. I will share few tips on how to make your app runs faster and responsive to the user interactions thus it creates better user experience.
- Lazy Data loading
- Lazy Object initialization
- Delayed View Creation
- Image Caching
- Progaurd Tool
- WebP and Compressed PNG files
1. Lazy data loading
Lazy data loading is related to database reading or Http requests. All these long running tasks should be done in background thread or worker thread. That means do not block UI thread.
We can move the long running tasks to background thread and also we can execute them in parallel if they do not depend on each other.
For eg: on dashboard page of an application may contain information from multiple sections of the app for giving a quick overview to the user. These sections may not depended on any other section on the dashboard. In this case, we can execute them as separate background tasks in parallel.
2. Lazy Object initialization
Like lazy UI element loading, we can also lazy load objects like database, Http client, image loading libraries, serialize and de-serialize library. You can find lot of objects that is not required to be ready from the time of UI creation. Those objects can be moved to background threads or on-demand initialization blocks.
For eg, In the case of facebook share button, we don’t really need to initialize its dialog or its callbacks in Activity onCreate method. We can move facebook object initialization and callback listener registrations to facebook button click event.
Dependency Injection framework like Dagger is another way to improve the run time performance of your app. It generates code at compile time rather than runtime thus it avoid the need of reflection API usage. Reflection API is considered to be a reason for slowing down the application performance.
JetPack introduced, dependency injection framework called Hilt. Hilt is built on top of the popular DI library Dagger and it helps you to avoid writing boilerplate codes of Dagger.
3. Delayed View Inflation
This is the one that can give you an immediate result. You can clearly notice the performance improvement for UI loading.
You can implement this concept using ViewStub. Inflate it only when user try to interact with its UI component .
4. Image Caching
Always use caching library if you show images in your application. All the popular image loading library comes with cache options.
If your app does not contain too many images, then you can go with memory caching option. If you are displaying list of images or lot of images in your app then i would recommend for disk cache.
Glide, Fresco, Picasso are popular image loading libraries. All comes with built in cache memory and disk cache options.
When memory cache enabled, you cannot notice image reload issue.
5. Progaurd Tool
Every one is familiar with this tool. It is mainly for source code obfuscation/hardening. But as part of minimizing the source code for obfuscation it removes unused code lines. This can improve the runtime performance of an app.
This also have an option to shrink bundled resources in the apk. This will also positively impact the run time performance.
6. WebP and Compressed PNGs
WebP is google’s new compressed format for images. It reduce the size of the image without losing the quality.
Android Studio can convert your images into WebP with a single click. When size of the content reduces, UI performance improves.
Use online compressing tools for PNG images.
App with faster start up and light weight UI can give very good user experience. App performance improvement should be part of your daily coding process, a contentious integration to your development process.
Firebase’s perfomance monitoring is very useful tool for real time perofmance monotoring like app startup time, screen rendering time, frame freezing etc.
Also code level profiling also available for Firebase performance monitoring.
A small downloadable + faster startup + lazy loading with proper user feedback = 🙂 on user face.