0

I tried adding an instant app feature to my app, but when launching my app as an instant app I'm getting this error :

RuntimeException: Unable to get provider android.support.v4.content.FileProvider: java.lang.SecurityException: Provider must grant uri permissions

Here is how it's used in my feature Manifest:

<manifest>

    <application>

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">

            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />

        </provider>

    </application>

</manifest>

And my instant-app build.gradle:

apply plugin: 'com.android.instantapp'

android {
    compileSdkVersion rootProject.compileSdk

    defaultConfig {
        applicationId "applicationId" // same than my app's APK module
        minSdkVersion rootProject.minSdk
        targetSdkVersion rootProject.compileSdk
        versionCode rootProject.versionCode
        versionName rootProject.versionName
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

    }
}
dependencies {
    implementation project(':feature') // app's feature
}

What is wrong? Thanks for your help.

EDIT : Here is the complete logcat:

02-13 17:51:28.501 28992-28992/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: package, PID: 28992
                                                   java.lang.RuntimeException: Unable to get provider android.support.v4.content.FileProvider: java.lang.SecurityException: Provider must grant uri permissions
                                                       at android.app.ActivityThread.installProvider(ActivityThread.java:6206)
                                                       at android.app.ActivityThread.installContentProviders(ActivityThread.java:5721)
                                                       at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5639)
                                                       at android.app.ActivityThread.-wrap2(ActivityThread.java)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1637)
                                                       at android.os.Handler.dispatchMessage(Handler.java:105)
                                                       at android.os.Looper.loop(Looper.java:156)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6523)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)
                                                    Caused by: java.lang.SecurityException: Provider must grant uri permissions
                                                       at android.support.v4.content.FileProvider.attachInfo(FileProvider.java:375)
                                                       at android.app.ActivityThread.installProvider(ActivityThread.java:6203)
                                                       at android.app.ActivityThread.installContentProviders(ActivityThread.java:5721) 
                                                       at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5639) 
                                                       at android.app.ActivityThread.-wrap2(ActivityThread.java) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1637) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                       at android.os.Looper.loop(Looper.java:156) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:6523) 
                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831) 
02-13 17:51:28.535 28932-28944/? E/Isotope: UID: [10188]  PID: [28932] IActivityManagerProxy : Instant app: package crashed: java.lang.RuntimeException: Unable to get provider android.support.v4.content.FileProvider: java.lang.SecurityException: Provider must grant uri permissions
                                                at android.app.ActivityThread.installProvider(ActivityThread.java:6206)
                                                at android.app.ActivityThread.installContentProviders(ActivityThread.java:5721)
                                                at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5639)
                                                at android.app.ActivityThread.-wrap2(ActivityThread.java)
                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1637)
                                                at android.os.Handler.dispatchMessage(Handler.java:105)
                                                at android.os.Looper.loop(Looper.java:156)
                                                at android.app.ActivityThread.main(ActivityThread.java:6523)
                                                at java.lang.reflect.Method.invoke(Native Method)
                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)
                                             Caused by: java.lang.SecurityException: Provider must grant uri permissions
                                                at android.support.v4.content.FileProvider.attachInfo(FileProvider.java:375)
                                                at android.app.ActivityThread.installProvider(ActivityThread.java:6203)
                                                at android.app.ActivityThread.installContentProviders(ActivityThread.java:5721) 
                                                at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5639) 
                                                at android.app.ActivityThread.-wrap2(ActivityThread.java) 
                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1637) 
                                                at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                at android.os.Looper.loop(Looper.java:156) 
                                                at android.app.ActivityThread.main(ActivityThread.java:6523) 
                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941) 
                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831) 
Rob
  • 4,123
  • 3
  • 33
  • 53
  • Possible duplicate of : https://stackoverflow.com/questions/21275898/securityexception-with-granturipermission-when-sharing-a-file-with-fileprovider – iamgopal Feb 13 '18 at 16:45
  • @iamgopal I tried to do what's described in this thread but couldn't get it working – Rob Feb 13 '18 at 16:49
  • Well actually I don't really know where to put this code since my app crashes even before launching – Rob Feb 13 '18 at 16:54
  • I've added the logcat. I tried setting a breakpoint on a line reported in the logcat but the execution doesn't stop there... – Rob Feb 13 '18 at 17:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/165076/discussion-between-iamgopal-and-someday). – iamgopal Feb 13 '18 at 17:15
  • Please follow this: https://stackoverflow.com/q/18249007/2550246 – iamgopal Feb 13 '18 at 17:29

2 Answers2

1

It looks like the FileProvider isn't supported on O either because other apps aren't allowed to see its ContentProvider.

I have a test app that works as an installed app but not as an instant app. It looks like instant apps aren't allowed to use grantUriPermission(grant URI permissions) (which makes sense given the policy that instant apps can't be visible to installed apps and ContentProvider is not visible).

This means that instant apps can't retrieve pictures taken with ACTION_IMAGE_CAPTURE (because they can't read external storage and the camera app can't write to instant apps' private storage).

Prags
  • 2,457
  • 2
  • 21
  • 38
  • It seems to be the actual problem, I solved it by removing this part of the base `Manifest` and putting it in the APK's `Manifest`. – Rob Feb 15 '18 at 09:36
0

Add grantUriPermissions and exported in your AndroidManifest.xml file.

        <provider
            android:exported="false"
            android:grantUriPermissions="true"
            android:authorities="${applicationId}.fileprovider"
            android:name="androidx.core.content.FileProvider">
            <meta-data
                android:resource="@xml/file_paths"
                android:name="android.support.FILE_PROVIDER_PATHS"
                />
        </provider>
stramin
  • 2,183
  • 3
  • 29
  • 58
Leeda
  • 1
  • 1
  • 2
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 16 '21 at 05:36