Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add foregroundServiceType to a library manifest while maintaining backwards compatibility?

My library project has a location service, and per Android Q requirements it sets the android:foregroundServiceType="location" attribute in the manifest. When an app module uses my library and compiles against API level 28, it fails with the following error:

AndroidManifest.xml:57: AAPT: error: attribute android:foregroundServiceType not found.

How can my library maintain compatibility with older versions, while making sure the functionality works on Android Q?

like image 673
black Avatar asked Jun 13 '19 11:06

black


People also ask

How do you declare a service in manifest?

You declare a service in your app's Manifest, by adding a <service> element as a child of your <application> element. There's a list of attributes that you can use to control a service's behavior, but as a minimum you'll need to provide the service's name (android:name) and a description (android:description).

What is foregroundServiceType?

android:foregroundServiceType. Specify that the service is a foreground service that satisfies a particular use case. For example, a foreground service type of "location" indicates that an app is getting the device's current location, usually to continue a user-initiated action related to device location.

What is exported in manifest file?

The exported attribute is used to define if an activity, service, or receiver in your app is accessible and can be launched from an external application. As a practical example, if you try to share a file you'll see a set of applications available.

Is it possible to add a foregroundservicetype in Android?

No ForegroundServiceType is available Workaround right now is to remove [Service] from your service and add it manually to the AndroidManifest. Sorry, something went wrong. Hi @Cheesebaron can you please provide a project that repros it?

What is the service foregroundservicetype?

The service has a foregroundServiceType of mediaPlayback, mediaProjection, or phoneCall. The service provides a use case related to phone calls, navigation, or media playback, as defined in the notification's category attribute.

What is the purpose of the compatibility section in the manifest?

Thank you. Windows 7 introduces a new section in the application manifest called "Compatibility." This section helps Windows determine the versions of Windows that an application was designed to target, and enables Windows to provide the behavior that the application expects based on the version of Windows that the application targeted.

What is a foregroundservicestartnotallowedexception?

If an app tries to start a foreground service while the app is running in the background, and the foreground service doesn't satisfy one of the exceptional cases, the system throws a ForegroundServiceStartNotAllowedException.


2 Answers

I had same error and after migrating to Androidx and updating compileSdkVersion from 28 to 29 my issue was resolved. So please do these changes and you can get your solution

like image 117
Harjinder Bains Avatar answered Sep 19 '22 06:09

Harjinder Bains


My goal is to avoid breaking anyone's build when the library version is updated, and at the same time avoid forcing the developer to compile against API 29. It seems I have two choices:

  • Provide a separate library so that developers compiling against API 28 and lower don't get impacted;
  • warn developers targeting the new version to replace the service definition in the manifest using tools:node="replace".

The problem with the first approach is that I will need to maintain two libraries for some time. The problem with the second is that developers must remember to revert the change once they update the SDK version.

In my case, I will go with the second approach. By passing an explicit foreground service type to the startForeground method when targeting Android Q, I can cause a crash if the type is not set in the manifest. The developer can therefore catch this when targeting Android Q and revert the manifest change to fix it.

like image 43
black Avatar answered Sep 18 '22 06:09

black