Skip to content

FileProvider declared in image_picker collides with app-declared FileProvider, compilation fails (Android) #20481

@sir-boformer

Description

@sir-boformer

The image_picker plugin declares a FileProvider in its android manifest:

https://github.com/flutter/plugins/blob/99a2efadf212019519a3f2584e35b6b7325d00f3/packages/image_picker/android/src/main/AndroidManifest.xml#L8-L16

For my app I also needed a FileProvider, so I declared one in the manifest of my Flutter android module:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="boformer.downloadtest.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths"/>
</provider>

The problem is that when the manifest is merged with the app's manifest and other plugin manifests, it collides with other FileProvider declarations. Compilation fails:

Launching lib/main.dart on Android SDK built for x86 in debug mode...
Initializing gradle...
Resolving dependencies...
Running 'gradlew assembleDebug'...
/Users/felix/development/download_test/android/app/src/main/AndroidManifest.xml:21:13-65 Error:
	Attribute provider#android.support.v4.content.FileProvider@authorities value=(boformer.downloadtest.provider) from AndroidManifest.xml:21:13-65
	is also present at [:image_picker] AndroidManifest.xml:14:13-74 value=(boformer.downloadtest.flutter.image_provider).
	Suggestion: add 'tools:replace="android:authorities"' to <provider> element at AndroidManifest.xml:19:9-27:20 to override.
/Users/felix/development/download_test/android/app/src/main/AndroidManifest.xml:26:17-55 Error:
	Attribute meta-data#android.support.FILE_PROVIDER_PATHS@resource value=(@xml/provider_paths) from AndroidManifest.xml:26:17-55
	is also present at [:image_picker] AndroidManifest.xml:19:17-72 value=(@xml/flutter_image_picker_file_paths).
	Suggestion: add 'tools:replace="android:resource"' to <meta-data> element at AndroidManifest.xml:24:13-26:57 to override.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:processDebugManifest'.
> Manifest merger failed with multiple errors, see logs

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 14s
Finished with error: Gradle build failed: 1

There can only be one declaration for each FileProvider subclass. That means to declare multiple file providers without collisions, one must create a (empty) subclass of android.support.v4.content.FileProvider and use that as the provider name in the manifest.

In my opinion, android.support.v4.content.FileProvider should be reserved for the app itself so that developers can easily add a file provider to their app, without subclassing FileProvider. It's also used in the Android documentation and in StackOverflow answers.

image_picker and other plugins should subclass FileProvider to prevent collisions (also collisions between plugins).

This issue describes a similar problem in an Android library.

Changes to image_picker would be minimal:

<provider
    android:name="io.flutter.plugins.imagepicker.ImagePickerFileProvider"
    android:authorities="${applicationId}.flutter.image_provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/flutter_image_picker_file_paths"/>
</provider>
package io.flutter.plugins.imagepicker;

import android.support.v4.content.FileProvider;

public class ImagePickerFileProvider extends FileProvider {}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions