[XPOSED MODULE][MOD] Enable BATTERY_STATS In KitKat

Search This thread
Why is this needed? / Do I need this?

Are you running KitKat or later? Is your battery monitor of choice working? If so, you do not need this. If not, it likely will do the trick - if it does not, contact the battery monitor developer and ask that they read the 'Developers' section of this app.

Google decided to change the BATTERY_STATS permission to be signature|system, which on KitKat means an app cannot access battery statistics. Functions such as identifying which apps are consuming more resources than others will not function. https://code.google.com/p/android/issues/detail?id=61975

Alternatively, copy your app into /system/priv-app which will provide the app access to BATTERY_STATS. Google also changed the name of the service however, so if your app hasn't been updated for KitKat, then using this XPosed module is the best way to go.


What does this do?

This uses the Xposed framework to provide an alternate implementation of the com.android.server.am.BatteryStatsService.getStatistics() method which will not enforce that the calling application have the BATTERY_STATS permission.

In addition, this allows legacy apps who use the 'batteryinfo' service rather than the
'batterystats' service to continue to function, since the only change was in the name.

How do I install this?

This mod requires that your device is rooted, that the Xposed framework is installed, and that this module is enabled via the Xposed installer app. After a phone reboot, your battery monitoring apps should start to work as they did before KitKat rolled around.

1. You must have ROOT on your phone. If you do not, this is not for you.
2. Install this package
3. Download and install the Xposed installer if you do not yet have it: XDA Thread
4. Install the “Framework" in the "Xposed Installer" and activate "Enable BatteryStats Permission" in "Modules".
5. Reboot
6. Run your battery monitor of choice.

This app does not 'launch', so don't be surprised to not see any way to start this app.


Is this safe?

Yes - the code is open source, and very simple. Do keep in mind however that the Xposed framework allows you to enable modules that may be very dangerous - but this module is not one of them. You can find the source here: https://github.com/GSamLabs/EnableBatteryStatsPermission

I'm a developer, but my app still doesn't work in KitKat!

Check your logcat. Google also changed how network statistics were retrieved,
removing some methods and replacing them with others (getTcpBytesReceived was
removed for example).

I'm a developer, can I pull this code directly into my app?

Yes - by all means. Follow the great tutorial that the Xposed author has, and feel
free to copy HookGetStatisticsMethodCall class directly into your app.
https://github.com/rovo89/XposedBridge/wiki/Development-tutorial

That said - I recommend simply copying your app into /system/priv-app/, or using a
helper app similar (or identical) to:
https://github.com/GSamLabs/GSamBatteryMonitor-RootCompanion

Where do I download it?

I've attached the APK to this thread, but will also make it available in the Xposed modules repo.
 

Attachments

  • EnableBatteryStatsPermission_1.2.apk
    15 KB · Views: 13,738
Last edited:

ahrion

Retired Forum Moderator / Recognized Developer
Jul 19, 2013
3,101
5,128
How does one make an Xposed module for a framework that isn't even compatible yet? Better yet, how does one test it?

Sent from my LG-E980 using Tapatalk
 
How does one make an Xposed module for a framework that isn't even compatible yet? Better yet, how does one test it?

Sent from my LG-E980 using Tapatalk
Fair question - this module is independent of KitKat per se. It works just fine on JellyBean, and other older android versions to enable test, although in the apk I posted it restricts it to KitKat only via the manifest.

One of the goals of this thread - in addition to helping users, is to help developers of monitoring apps with an alternative while we await a decision from Google as to whether they'll restore the battery_stats permission.
 

jayRokk

Senior Member
Jan 12, 2012
3,193
2,214
35
Rack City
Xposed doesn't work with Kit Kat.
This is useless and misleading.
Change the title.

Sent from my Nexus 4 using xda app-developers app
 
  • Like
Reactions: gsamtan

crazyguns

Senior Member
Aug 24, 2011
292
91
Northampton
Not sure if this module works as it should, my battery monitor widget still shows n/a for screen on time.. Other modules like notification mod and xblast work so xposed is working..

Running cataclysm 4.4 rooted of course

-Nexus 5 32gb
 

jayRokk

Senior Member
Jan 12, 2012
3,193
2,214
35
Rack City
Not sure if this module works as it should, my battery monitor widget still shows n/a for screen on time.. Other modules like notification mod and xblast work so xposed is working..

Running cataclysm 4.4 rooted of course

-Nexus 5 32gb

Now it does work.
For the other guy going crazy over comment, please check my posting date. At the time xposed and all xposed modules were not working.





It works now, the beta just came out.



Sent from my Nexus 4 using xda app-developers app
 
Not sure if this module works as it should, my battery monitor widget still shows n/a for screen on time.. Other modules like notification mod and xblast work so xposed is working..

Running cataclysm 4.4 rooted of course

-Nexus 5 32gb

I posted a new update (1.1) that also hooks the ServiceManager.getService method which will allow apps using the old 'batteryinfo' service to work. I've confirmed this works with a few different apps now...so pull the latest and give it another try...
 

crazyguns

Senior Member
Aug 24, 2011
292
91
Northampton
Can confirm it works!
yqa3emu2.jpg


-Nexus 5 32gb
 

GermainZ

Inactive Recognized Developer / Retired Forum Mod
Aug 3, 2012
6,170
8,805
Guys, please keep it civil. There's no need to make a fuss out of nothing - thread cleaned. :)
 
  • Like
Reactions: gsamtan

juliatan

Senior Member
Nov 6, 2012
303
59
I was hoping with this module Gsam Battery Monitor would be working again on my Nex7/2 with KitKat 4.4. But it doesn't. Gsam only works with its app Root Companion. But with Gsam installed in System future OTA will crash. So I hope this module will soon work with Gsam as a 'normal' app.

One thing off the record is for shure. Since KitKat I have now a little less batterytime (and system is little slower). I was expecting the opposite. Certainly dissappointing after reading all the great things about supposed to be better system optimalisation in KK.
 
  • Like
Reactions: RusherDude
I was hoping with this module Gsam Battery Monitor would be working again on my Nex7/2 with KitKat 4.4. But it doesn't. Gsam only works with its app Root Companion. But with Gsam installed in System future OTA will crash. So I hope this module will soon work with Gsam as a 'normal' app.

One thing off the record is for shure. Since KitKat I have now a little less batterytime (and system is little slower). I was expecting the opposite. Certainly dissappointing after reading all the great things about supposed to be better system optimalisation in KK.

GSam Battery Monitor definitely works with this Xposed module (this is how I originally tested GSam on KitKat) - make sure you're running version 3.0 of the battery monitor, and the latest (1.2) version of this module. And make sure after enabling the module that you restart your phone....
 
  • Like
Reactions: RusherDude

RusherDude

Senior Member
Aug 24, 2012
2,013
623
Got last version of Gsam Battery Monitor installed, and this module enabled on xposed (and rebooted after and it appears as enabled), and gsam can't still gather the battery data and asks me for gsam root companion :S

Edit: Oh, you need to join the beta testing to test the Gsam monitor version that works with the xposed module :S.

Could you please update Gsam to read the stats at least officialy? I mean, whats the point to release the xposed module if the app still doesn't support it? :(
 
Last edited:

Top Liked Posts

  • There are no posts matching your filters.
  • 33
    Why is this needed? / Do I need this?

    Are you running KitKat or later? Is your battery monitor of choice working? If so, you do not need this. If not, it likely will do the trick - if it does not, contact the battery monitor developer and ask that they read the 'Developers' section of this app.

    Google decided to change the BATTERY_STATS permission to be signature|system, which on KitKat means an app cannot access battery statistics. Functions such as identifying which apps are consuming more resources than others will not function. https://code.google.com/p/android/issues/detail?id=61975

    Alternatively, copy your app into /system/priv-app which will provide the app access to BATTERY_STATS. Google also changed the name of the service however, so if your app hasn't been updated for KitKat, then using this XPosed module is the best way to go.


    What does this do?

    This uses the Xposed framework to provide an alternate implementation of the com.android.server.am.BatteryStatsService.getStatistics() method which will not enforce that the calling application have the BATTERY_STATS permission.

    In addition, this allows legacy apps who use the 'batteryinfo' service rather than the
    'batterystats' service to continue to function, since the only change was in the name.

    How do I install this?

    This mod requires that your device is rooted, that the Xposed framework is installed, and that this module is enabled via the Xposed installer app. After a phone reboot, your battery monitoring apps should start to work as they did before KitKat rolled around.

    1. You must have ROOT on your phone. If you do not, this is not for you.
    2. Install this package
    3. Download and install the Xposed installer if you do not yet have it: XDA Thread
    4. Install the “Framework" in the "Xposed Installer" and activate "Enable BatteryStats Permission" in "Modules".
    5. Reboot
    6. Run your battery monitor of choice.

    This app does not 'launch', so don't be surprised to not see any way to start this app.


    Is this safe?

    Yes - the code is open source, and very simple. Do keep in mind however that the Xposed framework allows you to enable modules that may be very dangerous - but this module is not one of them. You can find the source here: https://github.com/GSamLabs/EnableBatteryStatsPermission

    I'm a developer, but my app still doesn't work in KitKat!

    Check your logcat. Google also changed how network statistics were retrieved,
    removing some methods and replacing them with others (getTcpBytesReceived was
    removed for example).

    I'm a developer, can I pull this code directly into my app?

    Yes - by all means. Follow the great tutorial that the Xposed author has, and feel
    free to copy HookGetStatisticsMethodCall class directly into your app.
    https://github.com/rovo89/XposedBridge/wiki/Development-tutorial

    That said - I recommend simply copying your app into /system/priv-app/, or using a
    helper app similar (or identical) to:
    https://github.com/GSamLabs/GSamBatteryMonitor-RootCompanion

    Where do I download it?

    I've attached the APK to this thread, but will also make it available in the Xposed modules repo.
    2
    Reserved for future use

    Apps confirmed to work with this mod - I'll attempt to keep this list updated as I hear about others...

    • GSam Battery Monitor
    • BetterBatteryStats
    • Wakelock Detector
    2
    Not sure if this module works as it should, my battery monitor widget still shows n/a for screen on time.. Other modules like notification mod and xblast work so xposed is working..

    Running cataclysm 4.4 rooted of course

    -Nexus 5 32gb

    I posted a new update (1.1) that also hooks the ServiceManager.getService method which will allow apps using the old 'batteryinfo' service to work. I've confirmed this works with a few different apps now...so pull the latest and give it another try...
    2
    FYI, it's broken on Lollipop:

    Code:
    I/Xposed  ( 3743):   Loading class com.gsamlabs.xposed.mods.enablebatterystatspermission.HookGetStatisticsMethodCall
    I/Xposed  ( 3743): EnableBatteryStatsPermission failed to load a field - we won't be able to bypass any permissions.  This should NEVER happen.
    E/Xposed  ( 3743): java.lang.ClassNotFoundException: com.android.server.am.BatteryStatsService
    E/Xposed  ( 3743):      at java.lang.Class.classForName(Native Method)
    E/Xposed  ( 3743):      at java.lang.Class.forName(Class.java:308)
    E/Xposed  ( 3743):      at java.lang.Class.forName(Class.java:272)
    E/Xposed  ( 3743):      at com.gsamlabs.xposed.mods.enablebatterystatspermission.HookGetStatisticsMethodCall.<clinit>(HookGetStatisticsMethodCall.java:60)
    E/Xposed  ( 3743):      at java.lang.reflect.Constructor.newInstance(Native Method)
    E/Xposed  ( 3743):      at java.lang.Class.newInstance(Class.java:1572)
    E/Xposed  ( 3743):      at de.robv.android.xposed.XposedBridge.loadModule(XposedBridge.java:463)
    E/Xposed  ( 3743):      at de.robv.android.xposed.XposedBridge.loadModules(XposedBridge.java:418)
    E/Xposed  ( 3743):      at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:105)
    E/Xposed  ( 3743): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.android.server.am.BatteryStatsService" on path: DexPathList[[zip file "/data/app/com.gsamlabs.xposed.mods.enablebatterystatspermission-1.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
    E/Xposed  ( 3743):      at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    E/Xposed  ( 3743):      at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
    E/Xposed  ( 3743):      at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
    E/Xposed  ( 3743):      ... 9 more
    E/Xposed  ( 3743):      Suppressed: java.lang.ClassNotFoundException: Didn't find class "com.android.server.am.BatteryStatsService" on path: DexPathList[[zip file "/system/framework/XposedBridge.jar"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
    E/Xposed  ( 3743):              at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    E/Xposed  ( 3743):              at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
    E/Xposed  ( 3743):              at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
    E/Xposed  ( 3743):              ... 10 more
    E/Xposed  ( 3743):              Suppressed: java.lang.ClassNotFoundException: com.android.server.am.BatteryStatsService
    E/Xposed  ( 3743):                      at java.lang.Class.classForName(Native Method)
    E/Xposed  ( 3743):                      at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
    E/Xposed  ( 3743):                      at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
    E/Xposed  ( 3743):                      at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
    E/Xposed  ( 3743):                      ... 11 more
    E/Xposed  ( 3743):              Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
    1
    Xposed doesn't work with Kit Kat.
    This is useless and misleading.
    Change the title.

    Sent from my Nexus 4 using xda app-developers app