Why Battery Historian is important for any android app?
What is Battery Historian?
Battery Historian is a tool developed and maintained by Google itself.It shows very detailed battery usage of device running android version 5 or later.
Why should an android developer use Battery Historian?
The information visualised by Battery Historian plays a vital role in debugging what part of an app is draining the battery of device.If a user gets frequent warning from device OS regarding battery drain due to a particular app, he/she is more likely to uninstall the app.Further more it also helps to figure out overheating of device while using the app ,network usage and many more vital stats.
GitHub:- Click Here
Getting Historian up and running
There are different ways to run Battery Historian,in this example I’ll use Docker on a Linux machine
Make sure you have the android device with you and the application installed on it for sufficient time to get battery usage of the particular app
Step 1:-To use Battery Historian via Docker, we need to set up docker first.You can refer here to install docker on your machine as it includes various ways to do the same.
Step 2:-After docker is installed run the following command
docker -- run -p <port>:9999 gcr.io/android-battery-historian/stable:3.0 --port 9999
Here replace <port> with the port of your choice on which Battery Historian will be available.I will use port number 8080.
Step 3:-Open up a browser and go to
http://localhost:8080/
The page will look something like this
Step 4:-Now connect your device get the Bug report of your device using adb command for android version higher than android 6
adb bugreport report.zip
for version 6 and less
adb bugreport > report.txt
Note — To reset battery stats run
adb shell dumpsys batterystats --resetThis will reset battery stats and you can analyse stats from this point to next bug report
Step 5:-Click on the browse button and select the report obtained from previous step and then press Submit button
It will take a few moment to analyse the report and show a page that looks like this
This may looks very confusing but we don’t need everything right now,so let’s filter out .This report have 17 hours battery consumption of app in background with WiFi connected throughout the interval.
Click on Choose an application on left tab and select your app.
Now lets go through sections one by one
- Network Info
The network info shows how many times app interacted with internet ,how much data it sent and how much data it received during the period.Using mobile internet uses more battery than using WiFi ,so using WiFi for uploads and downloads reduces battery consumption significantly
- Wakelocks
A wake lock is a mechanism to indicate that your application needs to have the device stay on.Keep a watch on counts and make sure your app is not keeping the device awake for long times.
- Services
Android service is a component that is used to perform operations on the background such as playing music, handle network transactions, interacting content providers etc. It doesn’t has any UI . Services should not have frequent launches
- Processes Info
When an application component starts and the application does not have any other components running, the Android system starts a new Linux process for the application with a single thread of execution. By default, all components of the same application run in the same process and thread (called the “main” thread).The historian gives info on ANRs ,Crashes and count of starting the process.
- Scheduled Jobs
This info shows how many time your work manager was triggered,having lots of background jobs increases the battery consumption,so adding constraint like not low battery ,while charging only to your background jobs and decreasing background jobs reduces unnecessary strain on the battery and prevent System OS from triggering high battery consumption warning for your app.
So these were some of the basic info that one should be aware of to reduce battery consumption.
I would like to give a example of how Battery Historian helped to reduce battery consumption significantly.
Below are the old reports of the same app for which System OS frequently reported high battery consumption.This data is for a ~12 hour session,now compare it with data shared above for the same app for ~17 hour session.
As depicted above the count for jobs and services went more than 100,and all this due to minor bugs like,getting interval for job scheduler in minutes from server ,some failed null checks etc.
So use the tool to your advantage and see what all things in your app causes battery drains ;)