Android App Performance and Optimisation
Improve your app’s performance for a smoother user experience and increased visibility.
Performance plays a major role in the success of any application. Providing a smooth and seamless experience to users should be the priority of a developer.
In this article, we will go through some of the tricks on how we can improve the performance of our application and attract more users.
App Size
Before the user starts using our app, they will need to download the app and install it on their device. If your app takes 100–200 mb of data to install, then many users are hesitant before actually downloading it. Also, app size affects how quickly your app loads.
Tips for reducing application size
- Use Android App Bundles(AAB) instead of APK to ensure the inclusion of only the necessary resources for a particular device configuration, thereby reducing the initial download size.
- Prefer SVG’s over PNG or another format, as it can scale without actually affecting the quality.
- Remove any unused resources like images, videos, or text files.
- Do not depend on third-party libraries for any smaller or basic functionalities. Try to use it only when necessary.
- Remove any unused code or unnecessary XML files.
- Use ProGuard or R8 to minimize the size of the code.
App Launch Time
The first thing we notice in any application is its launch time. Users expect their apps to be responsive and fast to load. If an app fails to load quickly,, it can disappoint users, and lead to uninstalls and poor ratings on PlayStore.
According to this article, 70% of users expect an app to be launched within 3 seconds, and 63% will abandon an app that takes more than 5 seconds.
How to optimize app launch time?
- Use a Splash Screen that displays a branded image or animation. This lets the user know that your app hasn’t frozen and keeps them engaged.
- Load only the data which is necessary to start the app.
- Avoid file access and JSON deserialization during app startup.
- Avoid any unnecessary initialization in the application’s onCreate method.
- Avoid creating global static objects. Instead, move to a singleton pattern, where the app initializes objects only the first time it accesses them.
Optimise Layouts
The more nesting you use while creating your Views, the more time it takes to render it. Always try to simplify and avoid nesting of views as much as possible. For example, using a nested instance of LinearLayout can lead to an excessively deep view hierarchy. Instead, use ConstraintLayout while managing complex or nested UIs.
How can you optimize your layout?
- Use data binding and view binding. These will help you directly inject the data into the UI instead of the old traditional findViewById method.
- Use the ViewStub class for complex views that are rarely used or visible in your UI. It will make sure it is loaded only when needed.
- Reuse your common views using <include>.
- Use <merge> to reduce nested views.
- Use Constraint Layout instead of nesting other layouts multiple times.
Minimise Network Requests
Most of our apps depend on API calls or downloading and uploading data over the internet. Frequent API calls and high usage of cellular data or wifi can result in battery drainage as well.
Techniques which can be used to prevent this
- To avoid the main thread being blocked, which might result in poor performance, use background threads for networking queries.
- Load textual data first and then go for images.
- Use caching to save frequently used data and images. You can use libraries like Picasso and Glide for this.
- Use Paging to load data in batches instead of requesting all the data from the server at once. This will allow users to interact with the app while other data is being fetched in the background.
- When interacting with an API or database, request only the specific data you want instead of having all of the fields.
Take Care of Memory Usage
Memory usage is the most important part of app development. A lot of times, developers are mostly focused on completing the feature and ignore the usage of memory. When not handled properly, it can lead to unwanted crashes or performance issues.
Things to watch when dealing with memory issues
- Use tools such as Android Profiler or LeakCanary to identify any unwanted memory leaks.
- Do not use third-party libraries until it’s required.
- Review your memory usage.
- Use primitive data types when possible.
- Optimise Bitmaps - Use scaled-down images to reduce memory usage.
- Avoid tight coupling.
- Implement Pagination or Lazy Loading instead of loading everything into memory at once.
- Regularly review your codebase to identify any memory leaks.
Review and Optimize Code
To make our app run smoothly, we should try to keep our code as optimized as we can. Using the correct data structure while performing a basic search can also impact how your app works. For example, using a linear search algorithm to search through a large dataset will be much slower than using a binary search algorithm.
Tips on how you can optimize your code
- Try to get the heavy operations done on your backend instead of having all the operations on the front end.
- Use StringBuilders to insert, append, or modify any string instead of String concatenation.
- Use Asynchronous programming techniques to offload time-consuming tasks to background threads.
- If you’re using a local database, then Profile and debug your database queries to write optimized queries that can get data faster.
- Keep your SDK and third-party libraries up to date for performance improvements and security updates.
Use Android Profiler Tools
Android Studio has an Android Profiler to monitor the CPU, memory, network, and battery used by the Android app.
How it can be helpful
- CPU Profiler: This will help you understand how the app uses the CPU resources of the device. It will provide detailed information on CPU usage, thread activity, and system events.
- Memory Profiler: This will help you to track the memory usage of the app. It details object allocations, memory leaks, and garbage collection events. This can help identify memory leaks in your app. You can also use Leak Canary to detect memory leaks in your app.
- Network Profiler: This helps you monitor the network activity of the app. It provides information about network requests, responses, and data transfer rates. You can use this to identify network-related issues, such as slow API calls or wrong data transfers.
- Energy Profiler: This helps you track the energy usage of the app, which can contribute to battery drain.
Conclusion
App performance is the most important aspect of app development. Ignoring it can lead to poor user experience and, ultimately, poor ratings on PlayStore or App Store. If you spend some time following the above practices, you can ensure that your app runs smoothly and has great reviews.
For more updates on the latest development trends, follow the Simform Engineering blog.