Android Studio 4.0 android:foregroundServiceType not found when targetSdkVersion 28

Viewed 7276

I am trying to build my app for Android and targeting Android API 28. But since I have updated a library dependency my app builds fail with the following error:

AAPT: error: attribute android:foregroundServiceType not found.

I have read that for some services(eg. location services) on Android Q(API 29) the user has to specify the type of foreground service for the permissions to work correctly, but I'm targeting API 28 and my AndroidManifest.xml doesn't contain that code.

The library I am using is Shake(https://shakebugs.com/):

implementation 'com.shakebugs.android:shake:13.0.1'

I have used an older version(12.0.3) of the library and there wasn't this issue.

Any help would be appreciated.

2 Answers

I solved changing:

compileSdkVersion 29

I hope it work for you, good day and happy coding.

Looks like you’re missing some code in order to get things to work! According to Shake documentation (https://www.shakebugs.com/docs/android/setup#caveat-for-apps-targeting-sdk-version-28-and-lower), their screen recording feature requires the library to specify an attribute on the service node in AndroidManifest.xml. This allows for Android 10+ support. You’ll have to override the service in your own manifest and the error will be resolved. Just add the following line to AndroidManifest.xml, inside the application node:

<service android:name="com.shakebugs.shake.internal.service.ScreenRecordingService" 
            tools:node="replace"/>

The other way to resolve the issue would be to update your targetSdkVersion to 29.

Detailed explanation

Android 10 (API 29) introduced changes to some services as a privacy improvement. That is why a service that uses the MediaProjectionManager API (and some others) has to be a foreground service and why the service definition in the app manifest file has to have the foregroundServiceType attribute defining what type of foreground service it is.

When developing a library that will be used with targetSdkVersion that is lower than 29 the service definition in the library manifest has to have one extra attribute (tools:node="replace"). This attribute will enable the developer to replace the library service definition with their own in their app manifest file.

If that node is not present when the app build is running the manifest merger will fail. So to add backward compatibility the library developer should add the following attributes to the service definition:

<service android:name=".ServiceName"
            android:foregroundServiceType="THE_TYPE_OF_FOREGROUND_SERVICE_TYPE"
            tools:node="replace"/>
Related