I'm using LiveKit Flutter SDK to implement screen sharing in my Flutter app. On Android 14 (SDK 34 and above), the app crashes every time I try to enable screen sharing with:
await room!.localParticipant?.setScreenShareEnabled(true);
The crash log shows:
SecurityException: Starting FGS with type mediaProjection requires android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION
at android.app.ContextImpl.startForegroundServiceCommon
at android.app.ContextImpl.startForegroundService
...
This happens every time I try to start screen sharing. It looks like the app (or flutter_background service) tries to start a Foreground Service with MediaProjection before user consent is granted.
I temporarily avoided the crash by wrapping startForeground() in a try/catch inside IsolateHolderService.kt, but that only hides the issue — it doesn’t fix it.
What I tried
Updated both livekit_client and flutter_background to their latest versions.
Added the required permission: android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION.
Added foregroundServiceType="mediaProjection" to services.
Verified flutter_background is initialized after user consent.
Tried delaying screen share start — still crashed every time.
Question
How do I properly request MediaProjection permission before enabling screen share in Flutter using LiveKit, so it works correctly on Android 14+ without crashing?