关于android:AndroidManifest使用FileProvider合并错误

AndroidManifest merge error using FileProvider

我正在尝试使用一个在其AndroidManifest.xml中具有此功能的库:

1
2
3
4
5
6
7
8
9
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.imagepicker.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>

我的应用程序的AndroidManifest.xml中具有相同的标签:

1
2
3
4
5
6
7
8
9
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="@string/FileProviderAuthority"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"></meta-data>
    </provider>

因此它显然会给出清单合并错误:

1
2
Error:Execution failed for task ':app:processDebugManifest'.
> Manifest merger failed with multiple errors, see logs

建议在AndroidManifest.xml的元素上"添加'tools:replace =" android:authorities"'以覆盖"。

因此,我在应用程序的AndroidManifest.xml中这样添加tools:replace="android:authorities"

1
2
3
4
5
6
7
8
9
10
<application
    tools:replace="android:authorities"
    android:name=".appcontroller.AppController"
    android:allowBackup="true"
    android:hardwareAccelerated="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

然后我得到这个错误:

1
2
Error:
tools:replace specified at line:25 for attribute android:authorities, but no new value specified

tools:replace="android.support.v4.content.FileProvider"也是如此。

我想念什么?


要解决此问题,有两种选择:要么将清单更改为使用与android.support.v4.content.FileProvider不同的类,要么创建PR /问题,要求lib的所有者更改其(最好的情况)方案)。

如果要快速修复,只需创建一个扩展FileProvider的类,例如:

1
2
3
4
import android.support.v4.content.FileProvider;

public class MyFileProvider extends FileProvider {
}

,并在清单文件中将其更改为:

1
<provider android:name=".MyFileProvider" ... >


添加到AndroidManifest.xml文件中的application标记内:

1
2
3
4
5
6
7
8
9
10
11
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true"
        tools:replace="android:authorities">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"
            tools:replace="android:resource" />
    </provider>


对于使用sdk 28及更高版本的用户,如果要迁移到androidx,解决??方案是:

1
2
3
4
5
6
7
8
9
10
<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true"
    tools:replace="android:authorities">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths"/>
</provider>

由于您没有更改提供程序中的任何值,因此不需要在清单中包括它。只需删除这些行即可。