Tips, Tricks, and Tools for Android Application Optimization
Tips, Tricks, and Tools for Android Application Optimization
These tips for programming your Android mobile applications will help them run faster and avoid performance bottlenecks.
Join the DZone community and get the full member experience.Join For Free
Android devices have a lot of cores, so writing smooth apps in the Android operating system is a simple task for anyone, right?
As everything on Android can be done in a number of different ways, and from them picking the best option can be tough. If you want to choose the most suited optimal method, you have to know what’s happening under the hood. And for this luckily, you don’t have to rely on hit and trial methods, since there’s a lot of tools out there that can help you find bottlenecks. And thus measuring and describing what’s going on can help in properly optimizing app speed and therefore generating smooth apps. This in return results in greatly improving the user experience and also draining less battery.
Let’s see some points to be kept in mind by which we can improve the performance of our application.
Tips and Tricks to Help Optimize Your Application
Never, Ever Put Any Relative Layout Inside Relative Layout
This simply means, don't make nested relative layouts.
When Android draws any view, it takes two passes to calculate the size (height & width) of that view: measure pass and layout pass. Relative layout uses two measure passes to calculate a size of its child view. So, If you put nested layout then it takes a number of passes means more time. So, you must keep in mind not to make relative layouts which developing android applications.
Don't Put Time-Consuming Operations in onCreate or onStart Methods
Instead of this, use Thread/AsyncTask mechanism.
In simple and straight terms, Android draws view in/after an onResume method of Activity (I hope you are aware of Activity's lifecycle). Now, if we put heavy calculations which take much time in an onCreate method, we will see the delay in rendering of views in the Activity.
If you are eager to learn more about this point, you can do an experiment. All you need to do is implement the
Thread.sleep(10000); code snippet in an onCreate method and you will get my exact meaning.
Use WebP Image Format Instead of PNG
This is one of the important tips that one must keep in mind while developing an android application. The size of WebP image is less than the PNG. It is actually about 45% of PNG's size. So using WebP will result in the decrease of your application. It will also result in decreased usage of User's important data if images are downloading from server. Even a Facebook app is using all images in WebP format currently.
Note: Original support of WebP format was added in Android in API 17. So if you want to use before 17 then you have to convert WebP image to PNG programmatically before use.
Code According to Data Speed
I suggest coding according to data speed; if the user uses 2G data (yes, you read right. In India, 2G is still running), then fetch most important data only from a server. It will help you to decrease waiting time for the user. This will in some way help in increasing the speed of the android application.
Use Parcelable Over Serializable to Serialize/Deserialize Data
As we already know that both Parcelable and Serializable are used to serialize and deserialize the data. But Serializable uses reflection approach which is a slow process. The important aspect of this is that this mechanism also tends to create a lot of temporary objects and results in the cause the garbage collection. So it is suggested to use Parceable data instead of serializable data to serialize or deserialize the data. This is also a good approach to increase the speed of the android application.
Let's also have a look at some tools that can help find bottlenecks, help with site optimization, or show that something is wrong:
1. Android Monitor
This is a well-known tool built into Android Studio. By default, you can find the Android Monitor at the bottom left corner, and you can switch between 2 tabs “Logcat” and “Monitors”.
The “Monitors” section contains 4 different graphs for:
Network ->The Network part shows the incoming and outgoing traffic in KB/s.
CPU ->The CPU part displays the CPU usage in percent.
GPU ->The GPU monitor displays how much time it takes to render the frames of a UI window.
Memory ->Lastly we have the Memory monitor, which shows the current amount of Free and Allocated memory.
2. GPU Overdraw
This is a simple helper tool. We can activate this in Developer Options once we have enabled the developer mode. The way of doing this is: Select Debug GPU overdraw, “Show overdraw areas”, and your screen will get some random colors. These colors on the screen simply tell about how many times a particular area was overdrawn. Whereas, True color means that there was no overdraw and this is what we should aim for while developing an android application. Just for information, Blue means one overdraw, green means two, pink three, red four.
While seeking for the true color is considered as the best one, we will always see some overdraws. These overdraws will be especially around texts, navigation drawers, dialogs. The thing is if your app is bluish or greenish, that’s probably fine. However, if it is too much red, then investigate is needed. The main cause of the red color might be too many fragments stacked onto each other. So, we must replace them instead of keeping placing new in the stack. Drawing is usually the slowest part of apps, so drawing something more than 3 layers do not make any sense. Feel free to check out your favorite apps with it. You will notice that even apps with over a billion downloads have red areas, so just take it easy when you are trying to optimize.
3. GPU Rendering
This is another tool from the Developer options which is generally known as Profile GPU rendering.
How does it work? Upon selecting it, if we go for “On screen as bars”. You will notice some colored bars appearing on the main screen. Every application has separate bars, weirdly the status bar has its own colors. There will be different colors if we have any software navigation buttons and gets updated when we interact with the screen. If you are good enough, there is one more color above the orange. It is a green line representing the 16 ms threshold. Our goal should be running the app at 60 fps, you have 16 ms to draw every frame. If you don’t make it, some frames might be skipped, the app could become jerky, and the user would definitely notice. Pay special attention to animations and scrolling, that’s where the smoothness matters the most. With this, you can detect some skipped frames with this tool and can work on them, which is an indirect way leads to speed optimization of the application.
By following these tips and tricks and checking the speed status of the application via few mentioned tools, we can surely lead to increased android application speed.
Opinions expressed by DZone contributors are their own.