(#) Missing `foregroundServiceType` attribute in manifest
!!! ERROR: Missing `foregroundServiceType` attribute in manifest
This is an error.
Id
: `ForegroundServiceType`
Summary
: Missing `foregroundServiceType` attribute in manifest
Severity
: Error
Category
: Correctness
Platform
: Android
Vendor
: Android Open Source Project
Feedback
: https://issuetracker.google.com/issues/new?component=192708
Since
: 8.2.0 (November 2023)
Affects
: Kotlin and Java files and manifest files
Editing
: This check runs on the fly in the IDE editor
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/ForegroundServiceTypesDetector.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/ForegroundServiceTypesDetectorTest.kt)
For `targetSdkVersion` >= 34, to call `Service.startForeground()`, the
element in the manifest file must have the
`foregroundServiceType` attribute specified.
(##) Example
Here is an example of lint warnings produced by this check:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text
src/test/pkg/MyService.java:8:Error: To call Service.startForeground(),
the element of manifest file must have the
foregroundServiceType attribute specified [ForegroundServiceType]
startForeground(1, null);
---------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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="test.pkg">
<uses-sdk android:targetSdkVersion="34" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application>
<service
android:exported="true"
android:name="test.pkg.MyService">
</service>
</application>
</manifest>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
`src/test/pkg/MyService.java`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~java linenumbers
package test.pkg;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startForeground(1, null);
return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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/ForegroundServiceTypesDetectorTest.kt)
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="ForegroundServiceType"` 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"`.
* Using a suppression annotation like this on the enclosing
element:
```kt
// Kotlin
@Suppress("ForegroundServiceType")
fun method() {
startForeground(...)
}
```
or
```java
// Java
@SuppressWarnings("ForegroundServiceType")
void method() {
startForeground(...);
}
```
* Using a suppression comment like this on the line above:
```kt
//noinspection ForegroundServiceType
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="ForegroundServiceType" 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 'ForegroundServiceType'
}
```
In Android projects this should be nested inside an `android { }`
block.
* For manual invocations of `lint`, using the `--ignore` flag:
```
$ lint --ignore ForegroundServiceType ...`
```
* Last, but not least, using baselines, as discussed
[here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).