Power consumption of Android applications

Do your users complain about the app draining their phone power very quickly? A running background service suddenly stops? Messages from FCM not reaching the user? What connects these three serious questions? The answer is simple - incorrectly structured work with the power consumption of the application.



Let's take a look at the main points related to this topic. Perhaps this will allow you in the future to avoid mistakes that most developers of mobile applications faced.



There is a huge amount of scattered information on the Internet, collecting which into a single general guide was one of the main goals of this article.



image



general information



Android has the following platform features to optimize power consumption:





Android 6 introduces two features to conserve battery power by controlling the behavior of applications when the device is not on charge:



  • Doze Mode.
  • App Standby.


Doze Mode



Doze, (maintenance window). .



- , Doze. , , Jobs, Alarms.



Doze, . (maintenance window) , Jobs, Alarms .





maintenance windows, , .



Doze :





Doze:





App StandBy, App StandBy Buckets



App StandBy , , . App StandBy , :



  • .
  • ( Foreground service, Activity).
  • , .
  • .


, Standby, . , .



, «» Samsung.



Android 9 . :





targetSdk.



App StandBy Buckets , . . , , , .



, :



  • Active. , . .. Activity, Foreground service, synchronized adapter, , . , .
  • Working set. , , . .
  • Frequent. , , . , FCM .
  • Rare. , . FCM . .
  • Never. , , . .


.



, , . . , Rare Active, , . , .



App StandBy Buckets:



  • , .
  • Launcher Activity, .
  • . , .
  • high-priority FCM-, , . high-priority, , normal-priority.


Firebase Cloud Messaging App StandBy Doze



FCM . FCM Doze App StandBy FCM-. , Doze App StandBy. , .





Doze Mode



  • ADB (android device bridge) :


export PATH=«~/Library/Android/sdk/platform-tools»:$PATH


  • :


adb shell dumpsys deviceidle force-idle


  • :


adb shell dumpsys deviceidle unforce


  • :


adb shell dumpsys battery reset


  • .


App StandBy Android < 9



  • App StandBy:


$ adb shell dumpsys battery unplug
$ adb shell am set-inactive <package_name> true


  • :


$ adb shell am set-inactive <package_name> false
$ adb shell am get-inactive <package_name>


  • . , . , .


App Standby Buckets



App StandBy bucket :



adb shell am set-standby-bucket <package_name> active|working_set|frequent|rare


, :



adb shell am get-standby-bucket <package_name>




  • :


adb shell cmd appops set <package_name> RUN_ANY_IN_BACKGROUND ignore


  • :


adb shell cmd appops set <package_name> RUN_ANY_IN_BACKGROUND allow


Battery safety



  • :




  • :


adb shell settings put global low_power 1


  • :


adb shell dumpsys battery reset


.



, Android 7:



  • `CONNECTIVITY_ACTION`, receiver . receiver , .
  • `ACTION_NEW_PICTURE` `ACTION_NEW_VIDEO`.


, Android 9:



, , . :



  • wake locks.
  • .


.



Battery Historian



Battery Historian . , , .



Battery Historian



Docker.



  • ADB (android device bridge) :


export PATH=«~/Library/Android/sdk/platform-tools»:$PATH


  • .
  • ADB-.


adb kill-server


  • :



adb devices


  • :





adb shell dumpsys batterystats --reset


  • .
  • .
  • , :


adb devices


  • :


adb shell dumpsys batterystats > [path/b]batterystats.txt


  • :


adb bugreport [path/]bugreport.zip


  • ( ):


docker run -p 5554:5554 gcr.io/android-battery-historian/stable:3.0 --port 5554




  • BatteryHistorian:




, , wake locks, JobScheduler . , , . - , .



Energy Profiler



Energy Profiler — Android Studio . , . , .



BatteryStats + UI-



, BatteryStats UI-.



  • bash-:


echo Write test class path e.g. <____>
read testName
export PATH=«~/Library/Android/sdk/platform-tools»:$PATH
adb shell dumpsys battery unplug
adb shell dumpsys batterystats --reset
adb shell am instrument -w  \ -e class $testName \ com.myapp.test/androidx.test.runner.AndroidJUnitRunner
adb shell dumpsys batterystats | awk -f BatteryStatsParseScript.awk > BatteryTestsResult.txt
adb shell dumpsys batterystats > BatteryTestsResultFull.txt
adb shell dumpsys batterystats reset
echo You can find the output file in the parent directory named BatteryTestsResult.txt


  • . , `com.myApp.MyTestEspressoTest`.
  • ADB.
  • .
  • BatteryStats.
  • , , , .
  • .awk-. BatteryTestsResultFull.txt ( , ).
  • .
  • BatteryStats.
  • !


, , .awk-. , .. , , . BatteryTestsResult.txt :



Estimated battery capacity: 3700 mAh

Time on battery: 32s 609ms (100.0%) realtime, 32s 610ms (100.0%) uptime

App Uid u0a358
Cpu Usage: 1.56 mAh
Radio Usage:  mAh
WiFi Usage: 0.0476 mAh
Wake Usage:  mAh
Sensor Usage:  mAh
GPS Usage: 0.0417 mAh
Total App Usage: 1.65mAh

Total time in seconds: 32 seconds
Usage per second: 0.0515625 mAh/seconds

User activity: 14 touch

Wi-Fi network: 335.22KB received, 342.84KB sent (packets 745 received, 758 sent)


, BatteryStats. .awk- .



P.S. Samsung



https://dontkillmyapp.com, , . , , Samsung . .



Samsung S8 App Power Monitor. , whitelist. Samsung — « ».



, :





, Android-, , , . , — , . .




All Articles