Intro
From time to time, I find myself analysing battery issues on Android devices. Did a battery drain quickly because of poor network connectivity? A worn-out battery? A poorly programmed app? Repeated GPS fixing? High screen brightness? Etc, etc.
An Android app can only gather a limited amount of relevant information from a device, which makes creating an app to do on-the-fly analysis not an option. This makes Battery Historian the clear choice for analysing a small amount of devices. It requires manual labour, but the results are very detailed.
The tl;dr of this blog post is as follows:
Battery Historian cannot handle TXT files with UTF-16 encoding, which is the default encoding for PowerShell output.
Using Battery Historian
To be clear, this blog post is not about using Battery Historian, there are many sources that nicely describe the process. This blog post is about one very specific issue I encountered. However, very quickly, this is how to user Battery Historian on Windows:
- Connect the Android device to your computer via ADB.
- In Powershell, run (for Android 7+) “adb bugreport bugreport.zip” or (for Android 5 and 6) “adb bugreport > bugreport.txt”. This creates bugreport.zip (or bugreport.txt) on your computer, which contains a lot of information from the Android Device since it’s last full battery charge (the device clears this information every time its battery hits 100%).
- Assuming Docker is installed, run “docker — run -p 9999:9999 gcr.io/android-battery-historian/stable:3.1 –port 9999” in PowerShell.
- Open http://localhost:9999 in your browser and upload bugreport.zip (or bugreport.txt)
- Done. Click around and look at the fancy graphs.
An often encountered issue
Now that has been cleared up, on to the reason of this blog post. My adventures in using Android Battery Historian.
Using Battery Historian was not without struggle. I encountered multiple issues, one of them is quite widespread. The bugreport file was not being accepted, both a zip and txt file. Using my browser’s Developer Tools I found out the underlying issue was that d3 was not defined.
“Luckily” there were many people before me that ran into this issue. This is because the Battery Historian Docker image uses an external resource that cannot be reached. A possible solution is changing the code a bit to use a different URL to load the d3 JavaScript file. Another easy solution is using a custom made Docker Image found in the GitHub comments.
The(!) encountered issue
For another issue I did not find any online sources describing or resolving it. I think that is a good reason for this little writeup.
On Android 5 and 6 a TXT file is created, while newer Android versions create a ZIP file. I was creating bugreport.txt files from an Android 6 device, in PowerShell. All of these files could not be loaded in Battery Historian and I could not figure out why. Per chance I ran the “adb bugreport > bugreport.txt” command from Windows CMD once and this file did load perfectly in Battery Historian. Upon investigating I noticed running the command in PowerShell resulted in a file roughly 2 times the size of when I’d run the command in Windows CMD. The content of the file was largely identical, implying an encoding difference.
The output file of PowerShell is recognized by Notepad++ as using encoding “UCS-2 LE BOM” and the output file of CMD “UTF-8”, which makes sense because PowerShell uses UTF-16 as its default encoding. I saved the PowerShell output file with UTF-8 encoding and Battery Historian read it without any issue.
Conclusion: Battery Historian cannot read TXT files which use UTF-16 encoding.
Possible workarounds
Since Battery Historian cannot handle UTF-16 encoding, we have several options to create UTF-8 files on Windows.
- Change the encoding of the created UTF-16 file to UTF-8. For example, by opening the file in Notepad++, change the Encoding in the top menu and save the file.
- Use the Windows Command Prompt (CMD) instead.
- Change the default encoding of PowerShell. A quick internet search should be able to help you with that.
- Call the CEO of Google every night to demand an update to Battery Historian
To reflect:
On modern Android devices this is not an issue. Android 7+ devices do not produce TXT files, but ZIP files, and how many people still use Android 5 or 6 devices anyway? Still, this was an immensely annoying issue to figure out and I am very annoyed I spent so much time on, ultimately, such a trivial issue. I’m even slightly disappointed that Google does not support reading UTF-16 encoded TXT files in Battery Historian.
Hopefully this blog post will save precious time for the next person that tries to use Battery Historian with an Android 5 or 6 bugreport file in UTF-16 encoding.
Relevant resources:
- Android Developer page: https://developer.android.com/topic/performance/power/setup-battery-historian
- Github repository: https://github.com/google/battery-historian
- Battery Historian 3.1: https://console.cloud.google.com/gcr/images/android-battery-historian/GLOBAL/stable
Edit: I found another poor soul that (I suspect) encountered the same issue as I did. He/she has my sympathies.