Search code examples
javaandroidxmlkotlinandroid-manifest

Application cannot be installed because of multiple FileProviders


I have 2 product flavors defined in my build.gradle and i created 2 different fileproviders for each flavor. It seems to cause conflicts when i install another app flavor on the device.

The error i get when trying to install:

The application could not be installed: INSTALL_FAILED_CONFLICTING_PROVIDER Installation failed due to: 'Error code: 'INSTALL_FAILED_CONFLICTING_PROVIDER', message='INSTALL_FAILED_CONFLICTING_PROVIDER: Scanning Failed.: Can't install because provider name com.flavor2.app.fileprovider2 (in package com.flavor2.app) is already used by com.flavor1.app''

My providers in the manifest:

<provider
    android:authorities="com.flavor1.app.fileprovider1"
    android:name="androidx.core.content.FileProvider"
    android:grantUriPermissions="true"
    android:exported="false">

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

<provider
    android:authorities="com.flavor2.app.fileprovider2"
    android:name="androidx.core.content.FileProvider"
    android:grantUriPermissions="true"
    android:exported="false">

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

Solution

  • i have one manifest and defined 2 providers for 2 flavors

    That means if you were somehow to successfully install both apps, you would have four providers in total:

    App Flavor Authorities
    1 1 com.flavor1.app.fileprovider1 and com.flavor2.app.fileprovider2
    2 2 com.flavor1.app.fileprovider1 and com.flavor2.app.fileprovider2

    That will not work, as you cannot have two providers installed at the same time declaring the same authority.

    What I presume that you want is:

    App Flavor Authorities
    1 1 com.flavor1.app.fileprovider1
    2 2 com.flavor2.app.fileprovider2

    IOW, the Flavor 1 app has the com.flavor1 authority, and the Flavor 2 app has the com.flavor2 authority.

    To make that work, you need a total of three manifests:

    • One in your flavor1 source set, that contains the com.flavor1.app.fileprovider1 <provider> in an otherwise-empty <application>
    • One in your flavor2 source set, that contains the com.flavor2.app.fileprovider2 <provider> in an otherwise-empty <application>
    • One in your main source set, containing everything else

    This sample project demonstrates having source sets with manifests, though in my case it is to have varying <queries> elements (which are children of <manifest>, so you will not see the empty <application> element).