admin管理员组

文章数量:1356753

I’m encountering a persistent issue in Android Studio where BatteryStatsManager and related classes (BatteryUsageStats, BatteryUsageStatsQuery, UidBatteryConsumer) result in a Cannot resolve symbol error, even though my project is configured with compileSdk = 35 (API 35, Android 15) and minSdk = 28 (API 28, Android 9.0), where BatteryStatsManager was introduced.

Problem

Error: Android Studio flags BatteryStatsManager with Cannot resolve symbol at the import and usage lines.

Investigation: I checked \AppData\Local\Android\Sdk\platforms\android-35\android.jar (and android-28\android.jar) using 7-Zip: android/os/ contains Bundle.class, Build.class, BatteryManager.class, but not BatteryStatsManager.class.

Expectation: Since BatteryStatsManager is a public API (docs) introduced in API 28, it should be in android.jar for compileSdk >= 28.

Steps Tried

Reinstalled SDK:

Uninstalled API 35 via Tools > SDK Manager > SDK Platforms, reapplied, and repeated for API 28.

No change— BatteryStatsManager.class still missing.

Full Reset:

Uninstalled Android Studio, deleted \AppData\Local\Android\Sdk, reinstalled everything fresh.

Same result: android.jar lacks BatteryStatsManager.class.

Invalidated Caches:

File > Invalidate Caches / Restart —fixed other import errors, but not this one. Checked API 28:

Set compileSdk = 28, installed API 28 SDK, checked android-28\android.jar—still no BatteryStatsManager.class.

Questions:

Why is BatteryStatsManager.class not in android.jar?

It’s a public API, and other android.os classes are present. Is this a bug in the SDK, or is it intentionally excluded from the compile-time stub? Why the Cannot resolve symbol error?

With compileSdk = 35 (or 28), Android Studio should recognize it if it’s in the SDK. Is my android.jar corrupted, or is BatteryStatsManager handled differently?

How to fix this?

My app is a system app with BATTERY_STATS permission, so I can use reflection (getSystemService("batterystats")), but I’d prefer direct access for cleaner code and better IDE support.

本文标签: