在 Android 应用程序中添加多个文件提供程序

Adding multiple file providers in Android application

伙计们,我正在开发一个需要外部应用程序依赖项(.aar 文件)的 android 应用程序 库应用程序有自己的文件提供程序,我的应用程序也有自己的文件提供程序。

当我 运行 它作为一个单独的应用程序时,库运行良好,但是当我将它包含在我的应用程序中时,相机功能无法正常工作。我收到以下错误

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference

遵循应用程序的清单文件

<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/file_paths" />
        </provider>

下面是lib模块的manifest文件

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.ma.bot.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

应用程序和 lib 模块都有不同的包名 我如何将这 2 个文件提供程序添加到应用程序中 我也尝试使用多个文件提供程序,但我在应用程序编译时遇到 XML 解析错误。

android:authorities="${applicationId}.provider;com.ma.bot.provider"

如何解决这个问题。

伙计们,我在这个问题上找到了解决方案 link

基本上问题是我的库和应用程序有相同的名称 android.support.v4.content.FileProvider 所以它在清单合并中发生冲突,为了避免这种情况我需要 defile empty class extending File provider and使用它提供者名称

<provider
            android:name="com.MAG.MAGFileProvider"
            android:authorities="${applicationId}.provider;"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

并像这样声明class

import android.support.v4.content.FileProvider;
public class MAGFileProvider extends FileProvider
{
    //we extend fileprovider to stop collision with chatbot library file provider
    //this class is empty and used in manifest
}