dotnet/android

Android 14 permission changes causes SecurityException crash even when following the guidelines

DavidMarquezF opened this issue · 2 comments

Android framework version

net8.0-android

Affected platform version

android 34.0.95/8.0.100, VS 17.10.35013.160

Description

When I run a foreground service in API 34 or above I get the following error:

java.lang.SecurityException: Starting FGS with type dataSync callerApp=ProcessRecord{a19ceba 5781:com.x.x/u0a246} targetSDK=34 requires permissions: all of the permissions allOf=true [android.permission.FOREGROUND_SERVICE_DATA_SYNC]

Following the permission requirements for Android 14, I add foregroundType to my service:

    [Service(ForegroundServiceType =Android.Content.PM.ForegroundService.TypeDataSync)]

And I call the correct StartForeground function in OnStartCommand:

 ServiceCompat.StartForeground(this, NotificationUtils.SERVICE_SYNC_RUNNING_NOTIFICATION_ID, GenerateNotification(0, T._("Preparing...")), (int)ForegroundService.TypeDataSync);

I checked the generated Android Manifest with the Android Studio apk explorer and saw the following:

  <service
      android:name="crc64ea54f08625ef32da.SyncAndroidService"
      android:foregroundServiceType="0x1" />

From what I understand, it should say dataSync, not 0x1, which is the enum int value.

I saw that some issues around ForegroundServiceType were fixed here: #8412, but it shouldn't affect the data type that I was using
Am I missing something?

Steps to Reproduce

  1. Create an Android Application
  2. Declare an Android Service with [Service(ForegroundServiceType =Android.Content.PM.ForegroundService.TypeDataSync)]
  3. Call ServiceCompat.StartForeground(this, <notifId>, <notif>, (int)ForegroundService.TypeDataSync);
  4. Start the service with context.StartForegroundService(intent);
  5. Run the app in an Android API >= 34 (Android 14)

Did you find any workaround?

I believe I can specify the service name in the attribute and then hardcode the foregroundServiceType in the AndroidManifest myself

Relevant log output

No response

Oh this is interesting. I just checked our app and it also has the strange type:

<service android:name="LoggedInService" android:exported="false" android:foregroundServiceType="9"/>

Well, actually I was doing a very stupid mistake, I forgot to add the uses permission for android.permission.FOREGROUND_SERVICE_DATA_SYNC (bang head against wall)

Either way, the code in the manifest is actually the correct android value (see https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/content/pm/ServiceInfo.java#172)

I guess Android takes either the string or the enum value because now everything works as expected