Problem
Identifying the problem
— Slow rendering
— Frozen Frames
— ANR
Find Frozen Frames
— Firebase Performance Monitoring
— Identify Long Ui Thread operations
Fixing the problem
— View Stub
— Avoid Ui Thread
Conclusion
Results
Problem
As Android users App Performance is a primary concern for users, if we do not pay attention to performance issues we can lose potential users.
By inspecting performance, you can understand what is happening in an app and ensure it meets your expectations.
Android provides several tools you can use to inspect your app’s performance. Here are the points where we can investigate:
App startup
Slow rendering (jank)
Screen transitions and navigation events
Long-running work
Operations in the background, such as I/O and networking
Identifying the problem
First, we must collect the data on which screen or module has a problem.
Collect User Feedback
Check the Google Play Console
Firebase Performance Monitoring
BenchMarking
After Analysing the above areas we can find there is a delay in launching in Particular Screen, Sometimes it’s sluggish too, and the possible cause of these issues can be :
Slow rendering
Frozen Frames
ANR
Slow rendering
UI Rendering is the act of generating a frame from your app and displaying it on the screen. To ensure that a user’s interaction with your app is smooth, your app should render frames in under 16ms to achieve 60 frames per second. If your app suffers from slow UI rendering, then the system is forced to skip frames and the user will perceive stuttering in your app. We call this jank.
Frozen Frames
Frozen frames are UI frames that take longer than 700ms to render. This is a problem because your app appears to be stuck and is unresponsive to user input for almost a full second while the frame is rendering. We typically recommend apps to render a frame within 16ms to ensure smooth UI. However, when your app is starting up or transitioning to a different screen, it’s normal for the initial frame to take longer than 16ms to draw because your app must inflate views, lay out the screen, and perform the initial draw all from scratch. That’s why Android tracks frozen frames separately from slow rendering. No frames in your app should ever take longer than 700ms to render.
ANRs
When the UI thread of an Android app is blocked for too long, an “Application Not Responding” (ANR) error is triggered. If the app is in the foreground, the system displays a dialog to the user, as shown in the figure. The ANR dialog gives the user the opportunity to force quit the app.
Find Frozen Frames
To find the Frozen Frames in a Screen, we have done a few things in our App :
Firebase Performance Monitoring
Firebase Performance Monitoring is a service that helps you to gain insight into the performance characteristics of your Apple, Android, and web apps.
You use the Performance Monitoring SDK to collect performance data from your app, then review and analyze that data in the Firebase console. Performance Monitoring helps you to understand in real time where your app’s performance can be improved so that you can use that information to fix performance issues.
By using Firebase performance Monitoring we added serval custom traces to investigate the launch time of Activity/Fragment.
e.g
import com.google.firebase.perf.FirebasePerformance;
import com.google.firebase.perf.metrics.Trace;**
val myTrace = Firebase.performance.newTrace("test_trace")
myTrace.start()
// code that you want to trace
myTrace.stop()
Identify Long Ui Thread operations
Instead of checking the code base we can start doing profiling and use an Android profiler to identify the issues janks.
If the app suffers from slow UI rendering, the system is forced to skip frames. When this happens, the user perceives a recurring flicker on their screen, which is called jank.
When a jank occurs, it’s usually because of some deceleration or blocking async call on the UI thread (in most apps, it’s the main thread). You can use system traces to identify where the problem is.
Now we know that which part is taking too much time we can use prepare a strategy to fix that part.
Fixing the Problem
In Our case, we were inflating every UI component even if it’s not visible to the user, a few of them contributing a lot in slowing our Activity .
View Stub
Even if a view is in a Gone state it will inflate with UI and contribute to Janks, for fixing we need a solution that inflates views on demand.
When the ViewStub is inflated, it replaces itself in its parent view with the inflated layout resource. Its visibility will exist in the view hierarchy only when setVisibility(int) or inflate() is invoked.
We migrated multiple heavy UI components to View Stub as a result we were able to improve the Performance of Activity/Fragment.
Avoid UI Thread:
All Android apps use a main thread to handle UI operations. Calling long-running operations from this main thread can lead to freezes and unresponsiveness. For example, if your app makes a network request from the main thread, your app’s UI is frozen until it receives the network response.
Results
We were able to improve the performance of our App by 30%, we monitored Firebase Performance Monitor to check the Improvement.