(#) Battery Life Issues !!! WARNING: Battery Life Issues This is a warning. Id : `BatteryLife` Summary : Battery Life Issues Severity : Warning Category : Correctness Platform : Android Vendor : Android Open Source Project Feedback : https://issuetracker.google.com/issues/new?component=192708 Since : 2.2.0 (September 2016) Affects : Kotlin and Java files and manifest files Editing : This check runs on the fly in the IDE editor See : https://developer.android.com/topic/performance/background-optimization Implementation : [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/BatteryDetector.kt) Tests : [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/BatteryDetectorTest.java) This issue flags code that either * negatively affects battery life, or * uses APIs that have recently changed behavior to prevent background tasks from consuming memory and battery excessively. Generally, you should be using `WorkManager` instead. For more details on how to update your code, please see https://developer.android.com/topic/performance/background-optimization. (##) Example Here is an example of lint warnings produced by this check: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text AndroidManifest.xml:9:Warning: Declaring a broadcastreceiver for android.net.conn.CONNECTIVITY_CHANGE is deprecated for apps targeting N and higher. In general, apps should not rely on this broadcast and instead use WorkManager. [BatteryLife] <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> ------------------------------------ AndroidManifest.xml:10:Warning: Use of REQUEST_IGNORE_BATTERY_OPTIMIZATIONS violates the Play Store Content Policy regarding acceptable use cases, as described in https://developer.android.com/training/monitoring-device-state/doze-standby.html [BatteryLife] <action android:name="android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" /> ----------------------------------------------------- AndroidManifest.xml:11:Warning: Use of com.android.camera.NEW_PICTURE is deprecated for all apps starting with the N release independent of the target SDK. Apps should not rely on these broadcasts and instead use WorkManager [BatteryLife] <action android:name="com.android.camera.NEW_PICTURE" /> ------------------------------ AndroidManifest.xml:12:Warning: Use of android.hardware.action.NEW_PICTURE is deprecated for all apps starting with the N release independent of the target SDK. Apps should not rely on these broadcasts and instead use WorkManager [BatteryLife] <action android:name="android.hardware.action.NEW_PICTURE" /> ----------------------------------- src/test/pkg/BatteryTest.java:15:Warning: Use of REQUEST_IGNORE_BATTERY_OPTIMIZATIONS violates the Play Store Content Policy regarding acceptable use cases, as described in https://developer.android.com/training/monitoring-device-state/doze-standby.html [BatteryLife] Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); ------------------------------------------- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Here are the relevant source files: `AndroidManifest.xml`: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~xml linenumbers <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.google.myapplication"> <uses-sdk android:targetSdkVersion="24" /> <receiver android:name=".MyReceiver" > <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> <action android:name="android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" /> <action android:name="com.android.camera.NEW_PICTURE" /> <action android:name="android.hardware.action.NEW_PICTURE" /> <data android:mimeType="image/*" /> </intent-filter> </receiver> </manifest> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ `src/test/pkg/BatteryTest.java`: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~java linenumbers package test.pkg; import android.annotation.TargetApi; import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.Intent; import android.net.Uri; import android.os.Build; import android.provider.Settings; @SuppressWarnings("unused") public class BatteryTest extends Activity { @TargetApi(Build.VERSION_CODES.M) public void testNoNo() throws ActivityNotFoundException { Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); intent.setData(Uri.parse("package:my.pkg")); startActivity(intent); } } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You can also visit the [source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/BatteryDetectorTest.java) for the unit tests for this check to see additional scenarios. (##) Suppressing You can suppress false positives using one of the following mechanisms: * Adding the suppression attribute `tools:ignore="BatteryLife"` on the problematic XML element (or one of its enclosing elements). You may also need to add the following namespace declaration on the root element in the XML file if it's not already there: `xmlns:tools="http://schemas.android.com/tools"`. ```xml <?xml version="1.0" encoding="UTF-8"?> <manifest xmlns:tools="http://schemas.android.com/tools"> ... <action tools:ignore="BatteryLife" .../> ... </manifest> ``` * Using a suppression annotation like this on the enclosing element: ```kt // Kotlin @Suppress("BatteryLife") fun method() { problematicStatement() } ``` or ```java // Java @SuppressWarnings("BatteryLife") void method() { problematicStatement(); } ``` * Using a suppression comment like this on the line above: ```kt //noinspection BatteryLife problematicStatement() ``` * Using a special `lint.xml` file in the source tree which turns off the check in that folder and any sub folder. A simple file might look like this: ```xml <?xml version="1.0" encoding="UTF-8"?> <lint> <issue id="BatteryLife" severity="ignore" /> </lint> ``` Instead of `ignore` you can also change the severity here, for example from `error` to `warning`. You can find additional documentation on how to filter issues by path, regular expression and so on [here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html). * In Gradle projects, using the DSL syntax to configure lint. For example, you can use something like ```gradle lintOptions { disable 'BatteryLife' } ``` In Android projects this should be nested inside an `android { }` block. * For manual invocations of `lint`, using the `--ignore` flag: ``` $ lint --ignore BatteryLife ...` ``` * Last, but not least, using baselines, as discussed [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).