Android 14 foreground service

Declaring foreground services and permissions in the manifest is now mandatory for targeting Android 14, along with an override of onStartComand to call startForeground(). Has anyone been down this road yet to figure out how this can be done with a juce android app (except by avoiding the issue by targeting Android 13)?

1 Like

Are you able to provide any more information about your use-case? Are you trying to add your own custom service, or are you running into problems using services added by JUCE, such as the JuceFirebaseMessagingService or JuceFirebaseInstanceIdService? I assume the former, because the JUCE-provided services do not seem to be “foreground services”.

To declare the service in the manifest, perhaps you could try pasting the modified manifest into the “Custom Manifest XML Content” field of the Projucer’s Android exporter. This will merge the field content with any Projucer-generated content, which should allow you to insert the android:foregroundServiceType field into the generated file.

To override onStartCommand, I believe you’d need to add a .java file to your project that contains a class extending Service or a subclass of Service.

Hello Reuk - thanks for replying.

I have a JUCE based cross platform mobile app (iOS / Android) that acts as a remote control to a macOS audio app, so the most fitting foreground service that might apply in the manifest file could be:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />

The projucer works well to merge these custom entries into the manifest file :+1: so that much is easy.

The bit that blocked me is the onStartCommand override since there is no reference to it in the JUCE code. You have kindly confirmed that a .java file and byte code generation would be necessary to achieve this, which is quite advanced stuff. However as I understand it, this is actually a change that JUCE ought to directly implement anyway because otherwise any JUCE android example apps targeted to API 34+ are not upload-able to the play console as is, not without that advanced surgery.

The thing is that Google Play console deprecates android apps faster these days, and force you to rebuild for a recent target API level, otherwise you can’t upload your maintenance changes. I don’t actually want to declare any foreground services at all, but have to because of the API 34 changes. So unless anyone else has worked out an easier solution, it looks like JUCE needs to provide direct support for API 34+ foreground services, otherwise everyone with a juce based android app will be forced to fork juce and generate java byte code to deal with this.

I am by no means an Android expert, so if I am labouring under a misapprehension, please do let me know!

best regards, Nicolas

I’m not sure about this. The docs say:

If your app targets Android 14, it must specify appropriate foreground service types.

The services overview tells us that services may be of three different kinds: foreground, background, and bound.

The docs for the <service> manifest element indicate that:

android:foregroundServiceType Specifies that the service is a foreground service that satisfies a particular use case.

My interpretation of all of this is that foregroundServiceType is not required on every <service> element, but rather just those elements that declare foreground services.

By default, JUCE doesn’t add any <service> tags to the manifest. If remote push-notifications are enabled, then a couple of services are added, but as far as I can tell these are not foreground services, and so do not require the foregroundServiceType tag. Therefore, I don’t expect this issue to affect JUCE apps in general. I think this likely only affects your project because you’re adding your own <service> which is a foreground service.

I have confirmed that you are correct.
Declaration of foreground services is only necessary in the manifest file if you are actually using a Service class instance in foreground mode.
The android documentation is not very clear on that point, emphasising instead the mandatory nature of the declarations!
Having removed all the foreground service related manifest file entries that I thought I needed, I can confirm the app targeted to API 34 now uploads to Play Console without foreground-service related errors.
Thank you for your insight!
All the best.

1 Like