background_locator_fixed icon indicating copy to clipboard operation
background_locator_fixed copied to clipboard

Foreground Service location (Android 14) crash

Open shashikiran918 opened this issue 1 year ago • 5 comments

Fatal Exception: java.lang.RuntimeException Unable to create service yukams.app.background_locator_2.IsolateHolderService: java.lang.SecurityException: Starting FGS with type location callerApp=ProcessRecord{87dfcc9 20206:com.icore.cproll/u0a738} targetSDK=34 requires permissions: all of the permissions allOf=true [android.permission.FOREGROUND_SERVICE_LOCATION] any of the permissions allOf=false [android.permission.ACCESS_COARSE_LOCATION, android.permission.ACCESS_FINE_LOCATION] and the app must be in the eligible state/exemptions to access the foreground only permission.

AndroidManifest.xml

`<manifest` xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xxx.xxxx">
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" `/>`
    <uses-permission `android:name="android.permission.WAKE_LOCK"/>`
    <uses-permission android:name="android.permission.VIBRATE" `/>`
    <uses-permission `android:name="android.permission.INTERNET"/>`
    <uses-permission `android:name="android.permission.FOREGROUND_SERVICE"/>`
    <uses-permission `android:name="android.permission.FOREGROUND_SERVICE_LOCATION"/>`
    <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" `/>`
    <uses-permission `android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>`
    <uses-permission `android:name="android.permission.POST_NOTIFICATIONS"/>`
    <uses-permission `android:name="android.permission.SCHEDULE_EXACT_ALARM"/>`
    <application
        android:name="${applicationName}"
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="xxxxx">
        <activity
            android:name=".MainActivity"
         android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:exported="true"
            android:hardwareAccelerated="true"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:showWhenLocked="true"
            android:turnScreenOn="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <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>
        <service android:name="yukams.app.background_locator_2.IsolateHolderService"
            android:permission="android.permission.FOREGROUND_SERVICE"
            android:exported="true"
            android:foregroundServiceType = "location"/>
        <receiver android:name="yukams.app.background_locator_2.BootBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>
        <receiver android:exported="false" android:name="com.dexterous.flutterlocalnotifications.ActionBroadcastReceiver" />
        <receiver android:exported="false" android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
        <receiver android:exported="false" android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
            </intent-filter>
        </receiver>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

build.gradle


   defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.xxx.xxx"
        minSdkVersion 21
        targetSdkVersion 34
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

shashikiran918 avatar Aug 20 '24 05:08 shashikiran918

It's happening to me even with all the permissions added

GerlanStanley avatar Aug 28 '24 20:08 GerlanStanley

There's an issue with type information of "TypeToken" being lost during the code shrinking process (like ProGuard or R8).

To resolve this, We need to add "-keep" statement for "TypeToken" in "proguard-rules.pro" file.

Add the following in your "proguard-rules.pro" file.

# Retain generic signatures of TypeToken and its subclasses with R8 version 3.0 and higher. -keep,allowobfuscation,allowshrinking class com.google.gson.reflect.TypeToken -keep,allowobfuscation,allowshrinking class * extends com.google.gson.reflect.TypeToken

QL-Uday avatar Sep 30 '24 10:09 QL-Uday

For those who still have the Android 14 crash, I believe my fork may be helpful: https://github.com/dhiaCodes/background_locator_fixed.git @shashikiran918 @GerlanStanley

dhiaCodes avatar Jul 25 '25 09:07 dhiaCodes

oh that's great @dhiaCodes but while running i had gradle version compatibility issues i need gradle 8.0-all-zip not 7.6-all-zip

shashikiran918 avatar Jul 25 '25 10:07 shashikiran918

@shashikiran918 Yes, you encountered this issue when running the example project of the package. You can either update the gradle version, or use it in your own project with a compatible version. I personally utilized the package in my project with the latest Gradle version, and it functioned flawlessly.

dhiaCodes avatar Jul 25 '25 11:07 dhiaCodes