2

I'm facing a weird issue trying to provide a file in Android 10. My code works fine in Android 9 and 11 but in Android 10 only works 50% of the times. There are no difference on the system status when it works and when it doesn't.

The intent:

    val intent = Intent(Intent.ACTION_VIEW).apply {
        setDataAndType(file, UpdateHelper.APK_TYPE)
        addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    }

If I grant the permission explicitly, it always work. So my question is not how to fix it, but if there are any explanation for it that I'm missing. In my understanding granting the permission in the intent is the preferred way of doing it.

context?.grantUriPermission(packageName, file, Intent.FLAG_GRANT_READ_URI_PERMISSION)
Miguel Sesma
  • 750
  • 5
  • 15
  • Can you tell me more about your use case? I mean when you're using file provider to do what? Sharing the file, deleting the file, etc. – Dev4Life Jun 06 '22 at 08:58
  • 1
    This is happening on a kiosk mode app not downloaded from the Play Store. When an update is available, the app downloads the new APK from our servers and launches the `PackageInstaller` app to update it. At some point, the installer kills our app in order to install the new one, and that removes the permission if granted in the intent. – Miguel Sesma Jun 07 '22 at 09:10
  • Hmm...then it's not about file provider maybe. Check other attributes in application tag of manifest. – Dev4Life Jun 08 '22 at 03:31

1 Answers1

1

Reviewing the documentation, it seems to be a race condition.

Permissions granted in an Intent remain in effect while the stack of the receiving Activity is active. When the stack finishes, the permissions are automatically removed.

For some reason this does not affect Android 9 and 11. But in Android 10 does. The reason for my app to be finished is that I'm using this to pass a new apk to the packageInstaller in order to update the app.

Miguel Sesma
  • 750
  • 5
  • 15