Android--Power Consumption Optimization

  1. Who is consuming the battery power of your mobile phone

The applications we write do not directly consume the battery power of a mobile phone, but rather use the hardware modules of the mobile phone to consume the power. Examples: CPU, WIFI module, GPS module, NetWork module, WakeLock module, audio and video module, Screen...

  1. Power and Applications

The formula for calculating the power consumption is: **Voltage = Voltage * Current * Time **.

In general, the voltage of the mobile phone is constant, so the power is only related to the product of current and time. Therefore, the formula for calculating module power is: **Module power = Module current * Module usage time **.
Different modules consume different currents per unit time. How can I get the current consumed per unit time of a module?

The Android system requires phone manufacturers to configure current values for different modules in / frameworks/base/core/res/res/xml/power_profile.xml. In addition, Android provides API PowerProfile to get power, which is calculated by reading the value of power_profile configuration. At the same time, we can also get it without API by following steps:

1. Export from mobile phone /system/framework/framework.apk
    adb pull /system/framework/framework.apk 
2. Using tools apktool or jadx 
    apktool d framework.apk // Generate a framework folder in the current directory
    jadx View directly apk file
3. See res/xml/power_profile.xml file

If we want to see the detailed power consumption directly, we can use the following commands

// When you need to test a scenario separately, call this command and disconnect the power supply (to prevent other conditions from causing inaccurate data).
adb shell dumpsys batterystats --reset // Reset Power Statistics

// Call this command before generating a zip file using the bugreport command
adb shell dumpsys batterystats > batterystats.txt // Enter power consumption into batterystats.txt
  1. Power Consumption Analysis

If you want to optimize your power consumption, you need to know which modules consume a lot of power. If you don't, guessing like a headless fly, wasting time and emotion will not have any effect. How to do power consumption analysis?

To do power consumption analysis, use bugreport+historian to generate power consumption pages.

1. bugreport
    Android Provides a tool for counting all the information of a mobile phone at a time. So, its size Not small, over 10 M That's small. If we're going to analyze a power consumption, we'll start at a few tenths M Finding locations in files is too inefficient. Therefore, Android Also developed historian Tools are used to help us analyze power consumption.
2. historian
    Android Provides a tool to analyze power consumption. It will be based on bugreport Generate a file, generate a html On the web page, we can intuitively see the power consumption of each module, and even the power consumption of the specified application module.

Environment installation and use of historian tools

1. Visit[historian](, see reademe
2. [download docker](
    This step follows the steps of the official website, step by step, don't worry.
    adb apt-get update 
    adb apt-get install docker-ce // Install docker engine
    apt-cache madison docker-ce // View the current docker version
    sudo apt-get install docker-ce="5:20.10.9~3-0~unbuntu-focal" docker-ce-cli="5:20.10.9~3-0~ubuntu-focal" // Install docker engine for specified version
    sudo docker run -p 9999:9999 --port 9999 // Set port number 9999 as the dedicated port for the historian analysis tool. I used VPN for this step.
3. Use bugreport generate zip or txt file
    android 7.0 Use later *adb bugreport*, android 7.0 Previous use *adb bugreport bugreportxx.txt*. 
4. Enter in Browser: localhost:9999 open historian Webpage.
5. Generated in the third step or bugreportxx.txt Put in the page that opens in step 4, and then submit,Finally, a visualization of power consumption is generated.
  1. Power consumption optimization

The so-called power consumption optimization is actually to reduce the power consumption of the application and increase the user's lifetime. According to the previous power calculation formula, it is found that the power consumption is only related to the current and time. In addition, the current is manufacturer-made, so we can only optimize the power consumption from the time latitude.

Usage time: There are two aspects of usage time here: background usage time and foreground usage time
    1. Background power consumption
        As the name implies, station power consumption is not perceptible to the user, it steals mobile phone power without the user knowing it. Under what circumstances can it cause background power consumption? For example: advertising SDK Always try to get a location behind the scenes to push accurate ads for us; push services will also be held behind the scenes WakeLock,To send notifications at specified times; download services to do something behind the scenes...We can only be impressed with these businesses, but for those that may cause background power consumption, we should think more about technology alternatives.
    2. Front Desk Power Consumption
        Compared to background power consumption, foreground power consumption is easier to analyze and locate problems. For example, when we are in historian See in CPU When consuming more power, see what our code does CPU Can transitions be used to reduce this? What behaviors can cause this? CPU Usage is too high. Here are some:
            1. Repeated network requests
            2. UI Layout too complex
            3. Animation forgot to stop, or the animation caused the entire page to redraw (You can view updates using the display interface update feature that comes with your phone )
            4. Load large pictures
            5. . . . 
        The power consumption of the foreground is usually caused by a bug in our code, so more code is submitted before review A few times is necessary.

Keywords: Android Optimize

Added by Kilgore Trout on Wed, 13 Oct 2021 20:07:10 +0300