Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding multiple file providers in Android application

guys, I am working on an android application for which I need external application dependency(.aar file) library application has its own file provider and my application have its own.

library work well when I run it as a separate app but when I include it my application then camera functionality not working. I get the following error

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

following the manifest file of the 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/file_paths" />
        </provider>

and the following is a manifest file of the lib module

<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>

both application and lib module have different package name how I add this 2 file providers into the application I also try including multiple file providers using, but I get XML parse error on app compilation.

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

how to solve this.

like image 530
dev_swat Avatar asked Nov 26 '18 14:11

dev_swat


1 Answers

Guys I found solution on this link Possible to use multiple authorities with FileProvider?

Basically problem is my library and application have same name android.support.v4.content.FileProvider so it colliding in manifest merger, to avoid this I need to defile empty class extending File provider and use it provider name

<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>

and declare class like this

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
}
like image 111
dev_swat Avatar answered Oct 20 '22 13:10

dev_swat