Analysis of Android Power Consumption Source Code and Measurement Methods
The article examines Android power consumption by analyzing the BatteryStatsHelper source code, explains how CPU, wakelock, Wi‑Fi, mobile data and sensor usage contribute to energy drain, and demonstrates practical measurement techniques using adb commands, dumpsys batterystats, and third‑party tools such as PowerTutor and BetterBatteryStats.
The article begins with a 2016 Android version distribution chart, showing that while Android 4.x still dominates the market, the share of 5.0 and newer versions is increasing, so power‑consumption analysis must cover both 4.x and 5.0+ devices.
Before Android 4.4 Google did not expose any public API for power statistics, so studying power consumption on those versions requires reading the Android source code; from Android 5.0 onward the information can be obtained directly via ADB commands.
Android Power Acquisition Source Code Analysis
In the system settings activity the battery information is displayed by the package com.android.settings.fuelaguge . The class PowerUsageSummary selects the top ten power‑hungry apps, while the actual per‑app calculation is performed in BatteryStatsHelper.processAppUsage . Examining this method reveals that an app’s energy use is composed of CPU, wakelock, Wi‑Fi, mobile‑network and other sensor contributions.
CPU Power Calculation
The CPU power consumption is computed as the product of the CPU running time and the per‑second power consumption for the current CPU speed tier (different speed steps have different power rates).
Wakelock Power Calculation
Wakelock power is calculated as the number of partial wake‑lock acquisitions multiplied by the power cost of each acquisition. Only partial wakelocks are considered in the source, although Android defines several types (partial, screen‑dim, screen‑bright, full).
Wi‑Fi Power Calculation
Wi‑Fi power consists of three parts: traffic‑related power, maintenance power, and scan power, with traffic being the dominant factor.
Mobile‑Network Power Calculation
For Android 4.4 the mobile‑network power is derived from the total uploaded and downloaded bytes multiplied by a per‑byte power factor. From Android 5.0 onward the calculation uses the number of packets transferred.
Sensor Power Calculation
Sensor power is the sum of each sensor’s multiplier (unit‑time power) multiplied by its active time ( sensorTime ). GPS sensors use a different calculation method from other sensors.
Using dumpsys batterystats to Obtain Power Data (Android 5.0+)
Obtain the app’s PID and UID with adb shell ps |find "com.baidu.example" . The first column of the output is the UID; remove any leading underscore to get the standard UID.
Retrieve the app’s power consumption since the last data reset with adb shell dumpsys batterystats "com.baidu.example" |find "***" , replacing *** with the UID. The example shows a result of 6.47 mAh.
To view the complete battery‑stats data, save the full output of dumpsys batterystats to a local file.
The saved file contains detailed information such as battery history (how long each app kept the device awake, what operations were performed, etc.).
Additional sections of the output include per‑PID statistics (wake time, estimated power per user) and lists of all kernel and partial wakelocks.
High‑Power Consumption Diagnosis Tools
Two third‑party tools are introduced for deeper analysis (both require a rooted device):
PowerTutor – Developed by Google, it records per‑minute power data for all apps, covering LCD, CPU, GPS, 3G, etc. It only supports a few device models (G1, G2, Nexus One) and labels other devices as “Unknown”.
BetterBatteryStats – Developed by asksven, it focuses on standby power usage, showing wakelock, alarm and network statistics per app, helping to identify the main causes of battery drain.
Google Battery History Tool (Android 5.0+)
By downloading the battery‑history script, initializing data collection, performing the desired operations, and then exporting the bug‑report, the data can be converted to an HTML file that plots time on the X‑axis and various power‑related parameters on the Y‑axis.
The article concludes with a reminder that the content is provided by the Baidu Quality Department subscription account, encouraging readers to reply with keywords such as “评测”, “CI”, or “移动测试” to receive additional evaluation articles, CI experience sharing, and mobile testing tool reviews.
Baidu Intelligent Testing
Welcome to follow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.