Baseflow / flutter-geolocator

Android and iOS Geolocation plugin for Flutter
https://baseflow.com/
MIT License
1.24k stars 656 forks source link

[Bug]: Subscribe to getServiceStatusStream throw an exception on Android 14 #1472

Closed michaelferwer closed 6 months ago

michaelferwer commented 7 months ago

Please check the following before submitting a new issue.

Please select affected platform(s)

Steps to reproduce

  1. On an Android 14 device, call the function Geolocator.getServiceStatusStream() could not open the stream and throw an exception.

Expected results

The call should return a valid Stream.

Actual results

The stacktrace :

E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): Failed to open event stream E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): java.lang.SecurityException: com.test: One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at android.os.Parcel.createExceptionOrNull(Parcel.java:3182) E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at android.os.Parcel.createException(Parcel.java:3166) E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at android.os.Parcel.readException(Parcel.java:3149) E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at android.os.Parcel.readException(Parcel.java:3091) E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at android.app.IActivityManager$Stub$Proxy.registerReceiverWithFeature(IActivityManager.java:5784) E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1853) E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1793) E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1781) E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:756) E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at com.baseflow.geolocator.LocationServiceHandlerImpl.onListen(LocationServiceHandlerImpl.java:58) E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at io.flutter.plugin.common.EventChannel$IncomingStreamRequestHandler.onListen(EventChannel.java:218) E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at io.flutter.plugin.common.EventChannel$IncomingStreamRequestHandler.onMessage(EventChannel.java:197) E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295) E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322) E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12) E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at android.os.Handler.handleCallback(Handler.java:959) E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at android.os.Handler.dispatchMessage(Handler.java:100) E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at android.os.Looper.loopOnce(Looper.java:232) E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at android.os.Looper.loop(Looper.java:317) E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at android.app.ActivityThread.main(ActivityThread.java:8501) E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at java.lang.reflect.Method.invoke(Native Method) E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552) E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878) E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): Caused by: android.os.RemoteException: Remote stack trace: E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at com.android.server.am.ActivityManagerService.registerReceiverWithFeature(ActivityManagerService.java:14337) E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:2615) E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2798) E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at android.os.Binder.execTransactInternal(Binder.java:1496) E/EventChannel#flutter.baseflow.com/geolocator_service_updates_android(27984): at android.os.Binder.execTransact(Binder.java:1440)

Code sample

Code sample ```dart Geolocator.getServiceStatusStream().listen((event) { print(event); }); ```

Screenshots or video

Screenshots or video demonstration [Upload media here]

Version

11.0.0

Flutter Doctor output

Doctor output ```console Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 3.16.5, on macOS 14.3.1 23D60 darwin-arm64, locale fr-FR) [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) [✓] Xcode - develop for iOS and macOS (Xcode 15.2) [✓] Chrome - develop for the web [✓] Android Studio (version 2023.2) [✓] IntelliJ IDEA Ultimate Edition (version 2023.3.6) [✓] VS Code (version 1.87.0) [✓] Connected device (4 available) [✓] Network resources • No issues found! ```
viniciusaro commented 7 months ago

@michaelferwer I had the same problem and noticed that although my dependency on geolocator was also pointing to version 11.0.0, the underlying geolocator_android dependency was still being resolved to a version that still had this problem. More specifically my pubspec.lock was something like:

...
geolocator:
  dependency: transitive
  description:
    name: geolocator
  version: "11.0.0"
geolocator_android:
  dependency: transitive
  description:
    name: geolocator_android
  version: "4.3.1"
...

In geolocator_android 4.3.1 the receiver flag is not setup: https://github.com/Baseflow/flutter-geolocator/blob/c941b6cb74a1dde5acee8c5560ef73974c5d4d95/geolocator_android/android/src/main/java/com/baseflow/geolocator/LocationServiceHandlerImpl.java#L58

I've updated the transitive dependency to 4.5.4 and hope this will fix the issue.

NijatTagizada commented 7 months ago

Same problem

mvanbeusekom commented 7 months ago

Hi @michaelferwer, @NijatTagizada,

Can you please confirm that the application is using the latest version of the geolocator_android package. You can find the version used in the pubspec.lock file as described by @viniciusaro.

If not try running flutter pub upgrade to update to the latest version. We would appreciate it if you can confirm if this resolves the issue.

NijatTagizada commented 7 months ago

Hi @mvanbeusekom. Here is my lock

geolocator:
    dependency: "direct main"
    description:
      name: geolocator
      sha256: "694ec58afe97787b5b72b8a0ab78c1a9244811c3c10e72c4362ef3c0ceb005cd"
      url: "https://pub.dev"
    source: hosted
    version: "11.0.0"
  geolocator_android:
    dependency: transitive
    description:
      name: geolocator_android
      sha256: "93906636752ea4d4e778afa981fdfe7409f545b3147046300df194330044d349"
      url: "https://pub.dev"
    source: hosted
    version: "4.3.1"

I ran flutter pub upgrade but it did not work. Still geolocator_android is 4.3.1

thorito commented 7 months ago

I'm having the same problem. Device: Pixel 7 Pro

compileSdkVersion: 34

Android Settings Geolocator:

f (isAndroid) {
      _locationSettings = AndroidSettings(
        distanceFilter: 10,
        forceLocationManager: true,
        foregroundNotificationConfig: const ForegroundNotificationConfig(
          notificationChannelName: 'location',
          notificationText: 'App in background',
          notificationTitle: 'App in background',
          setOngoing: true,
          enableWakeLock: true,
        ),
      );

      return;
    }

   ...

Exception:

E/FlutterGeolocator(25630): Geolocator position updates started using Android foreground service
D/FlutterGeolocator(25630): Start service in foreground mode.
D/CompatibilityChangeReporter(25630): Compat change id reported: 160794467; UID 10635; state: ENABLED
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630): Failed to open event stream
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630): java.lang.SecurityException: Permission Denial: startForeground from pid=25630, uid=10635 requires android.permission.FOREGROUND_SERVICE
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630):  at android.os.Parcel.createExceptionOrNull(Parcel.java:3182)
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630):  at android.os.Parcel.createException(Parcel.java:3166)
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630):  at android.os.Parcel.readException(Parcel.java:3149)
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630):  at android.os.Parcel.readException(Parcel.java:3091)
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630):  at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:6861)
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630):  at android.app.Service.startForeground(Service.java:775)
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630):  at com.baseflow.geolocator.GeolocatorLocationService.enableBackgroundMode(GeolocatorLocationService.java:147)
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630):  at com.baseflow.geolocator.StreamHandlerImpl.onListen(StreamHandlerImpl.java:130)
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630):  at io.flutter.plugin.common.EventChannel$IncomingStreamRequestHandler.onListen(EventChannel.java:218)
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630):  at io.flutter.plugin.common.EventChannel$IncomingStreamRequestHandler.onMessage(EventChannel.java:197)
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630):  at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292)
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630):  at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319)
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630):  at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(D8$$SyntheticClass:0)
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630):  at android.os.Handler.handleCallback(Handler.java:959)
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630):  at android.os.Handler.dispatchMessage(Handler.java:100)
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630):  at android.os.Looper.loopOnce(Looper.java:232)
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630):  at android.os.Looper.loop(Looper.java:317)
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630):  at android.app.ActivityThread.main(ActivityThread.java:8501)
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630):  at java.lang.reflect.Method.invoke(Native Method)
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630):  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878)
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630): Caused by: android.os.RemoteException: Remote stack trace:
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630):  at com.android.server.am.ActivityManagerService.enforcePermission(ActivityManagerService.java:6125)
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630):  at com.android.server.am.ActiveServices.setServiceForegroundInnerLocked(ActiveServices.java:2050)
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630):  at com.android.server.am.ActiveServices.setServiceForegroundLocked(ActiveServices.java:1704)
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630):  at com.android.server.am.ActivityManagerService.setServiceForeground(ActivityManagerService.java:13684)
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630):  at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:3430)
E/EventChannel#flutter.baseflow.com/geolocator_updates_android(25630): 

======== Exception caught by services library ======================================================
The following PlatformException was thrown while activating platform stream on channel flutter.baseflow.com/geolocator_updates_android:
PlatformException(error, Permission Denial: startForeground from pid=25630, uid=10635 requires android.permission.FOREGROUND_SERVICE, null, null)

When the exception was thrown, this was the stack: 
#0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:651:7)
#1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:334:18)
<asynchronous suspension>
#2      EventChannel.receiveBroadcastStream.<anonymous closure> (package:flutter/src/services/platform_channel.dart:676:9)
<asynchronous suspension>
====================================================================================================
W/Sensors (25630): Cannot get virtualdevice_native service

image

michaelferwer commented 6 months ago

Hi @michaelferwer, @NijatTagizada,

Can you please confirm that the application is using the latest version of the geolocator_android package. You can find the version used in the pubspec.lock file as described by @viniciusaro.

If not try running flutter pub upgrade to update to the latest version. We would appreciate it if you can confirm if this resolves the issue.

Hello @mvanbeusekom,

I have successfully upgraded geolocator dependencies.

I can confirm versions geolocator 11.0.0 and geolocator_android 4.5.4 fix this issue.

For those who flutter pub upgrade didn't make the job, that's maybe you have conflict with some dependencies. It's was the case for me.

NijatTagizada commented 6 months ago

Hi @michaelferwer, @NijatTagizada, Can you please confirm that the application is using the latest version of the geolocator_android package. You can find the version used in the pubspec.lock file as described by @viniciusaro. If not try running flutter pub upgrade to update to the latest version. We would appreciate it if you can confirm if this resolves the issue.

Hello @mvanbeusekom,

I have successfully upgraded geolocator dependencies.

I can confirm versions geolocator 11.0.0 and geolocator_android 4.5.4 fix this issue.

For those who flutter pub upgrade didn't make the job, that's maybe you have conflict with some dependencies. It's was the case for me.

hi. can you share your pubspec file, please?

thorito commented 6 months ago

Hi @michaelferwer, @NijatTagizada, Can you please confirm that the application is using the latest version of the geolocator_android package. You can find the version used in the pubspec.lock file as described by @viniciusaro. If not try running flutter pub upgrade to update to the latest version. We would appreciate it if you can confirm if this resolves the issue.

Hello @mvanbeusekom, I have successfully upgraded geolocator dependencies. I can confirm versions geolocator 11.0.0 and geolocator_android 4.5.4 fix this issue. For those who flutter pub upgrade didn't make the job, that's maybe you have conflict with some dependencies. It's was the case for me.

hi. can you share your pubspec file, please? Hi, My pubspec.yaml:

environment:
  sdk: '>=3.0.0 <4.0.0'
  flutter: '>=3.13.0'

dependencies:
  battery_plus: ^6.0.1
  equatable: ^2.0.5
  flutter:
    sdk: flutter
  flutter_activity_recognition: ^3.0.0
  geolocator: ^11.0.0
  logger: ^2.2.0
  permission_handler: ^11.3.1
  sensors_plus: ^5.0.1

dev_dependencies:
  flutter_test:
    sdk: flutter
  mocktail: ^1.0.3
  very_good_analysis: ^5.1.0

My pubspec.lock:

  geolocator:
    dependency: "direct main"
    description:
      name: geolocator
      sha256: "694ec58afe97787b5b72b8a0ab78c1a9244811c3c10e72c4362ef3c0ceb005cd"
      url: "https://pub.dev"
    source: hosted
    version: "11.0.0"
  geolocator_android:
    dependency: transitive
    description:
      name: geolocator_android
      sha256: f15d1536cd01b1399578f1da1eb5d566e7a718db6a3648f2c24d2e2f859f0692
      url: "https://pub.dev"
    source: hosted
    version: "4.5.4"
  geolocator_apple:
    dependency: transitive
    description:
      name: geolocator_apple
      sha256: bc2aca02423ad429cb0556121f56e60360a2b7d694c8570301d06ea0c00732fd
      url: "https://pub.dev"
    source: hosted
    version: "2.3.7"
  geolocator_platform_interface:
    dependency: transitive
    description:
      name: geolocator_platform_interface
      sha256: "009a21c4bc2761e58dccf07c24f219adaebe0ff707abdfd40b0a763d4003fab9"
      url: "https://pub.dev"
    source: hosted
    version: "4.2.2"
  geolocator_web:
    dependency: transitive
    description:
      name: geolocator_web
      sha256: "49d8f846ebeb5e2b6641fe477a7e97e5dd73f03cbfef3fd5c42177b7300fb0ed"
      url: "https://pub.dev"
    source: hosted
    version: "3.0.0"
  geolocator_windows:
    dependency: transitive
    description:
      name: geolocator_windows
      sha256: "53da08937d07c24b0d9952eb57a3b474e29aae2abf9dd717f7e1230995f13f0e"
      url: "https://pub.dev"
    source: hosted
    version: "0.2.3"
thorito commented 6 months ago

Sorry, my AndroidManifest.xml

<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <uses-permission android:name="android.permission.INTERNET"/>

    <!-- Local Notifications -->
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

    <!-- Activity Recognition -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
    <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />

    <!-- Location -->
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!--
        <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
        <uses-permission android:name="android.permission.WAKE_LOCK" />
        <uses-permission  android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
    -->

    <queries>
        <intent>
            <action
                android:name="android.intent.action.PROCESS_TEXT"/>
            <data
                android:mimeType="text/plain"/>
        </intent>
    </queries>
    <application
        tools:replace="android:label"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:label="@string/app_name">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:enableOnBackInvokedCallback="true"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize"
            android:showWhenLocked="true"
            android:turnScreenOn="true"
            android:exported="true">
            <meta-data
                android:name="io.flutter.embedding.android.NormalTheme"
                android:resource="@style/NormalTheme"/>
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/ic_notification" />
       <!-- <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="high_importance_channel" />-->
        <!--
          <meta-data
           android:name="io.flutter.embedding.android.EnableImpeller"
           android:value="true"/>
       -->

        <!-- Local Notifications -->
        <receiver
            android:exported="false"
            android:name="com.dexterous.flutterlocalnotifications.ActionBroadcastReceiver"
            />

        <service
            android:name="com.dexterous.flutterlocalnotifications.ForegroundService"
            android:exported="false"
            android:stopWithTask="false"
            />

    </application>
</manifest>
NijatTagizada commented 6 months ago

@thorito thank you

NijatTagizada commented 6 months ago

@thorito you are right. It was a dependencies problem. In my case, 3 packages (also geolocator) depend on uuid package. I just updated these packages to the latest version. I used this command to find out dependent packages flutter pub deps

mvanbeusekom commented 6 months ago

Hi @michaelferwer, @NijatTagizada and @thorito,

Thank you for confirming that upgrading resolved the issue. I will go ahead and close this issue now.

parth-darji commented 4 months ago

You can override the dependency like this in pubspec.yaml

dependency_overrides:
  geolocator_android: ^4.6.0

Rebuild the whole app by running flutter clean and flutter pub get