Issue with adding my FileProvider

2019-05-24 08:28发布

问题:

If I try to add my FileProvider like this:

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.myfileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths" />
    </provider>

I got the following issue:

Caused by: java.lang.RuntimeException: Manifest merger failed with multiple errors, see logs
    at com.android.builder.core.AndroidBuilder.mergeManifestsForApplication(AndroidBuilder.java:524)
    at com.android.build.gradle.tasks.MergeManifests.doFullTaskAction(MergeManifests.java:143)
    at com.android.build.gradle.internal.tasks.IncrementalTask.taskAction(IncrementalTask.java:106)
    at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)
    at org.gradle.api.internal.project.taskfactory.IncrementalTaskAction.doExecute(IncrementalTaskAction.java:50)
    at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:39)
    at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:26)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$1.run(ExecuteActionsTaskExecuter.java:131)
    at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:317)
    at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:309)
    at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:185)
    at org.gradle.internal.operations.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:97)
    at org.gradle.internal.operations.DelegatingBuildOperationExecutor.run(DelegatingBuildOperationExecutor.java:31)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:120)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:99)

If I remove the "authorities" line I get this:

 Manifest merger failed : Attribute meta-data#android.support.FILE_PROVIDER_PATHS@resource value=(@xml/provider_paths) from AndroidManifest.xml:374:17-55
is also present at [io.smooch:ui:5.12.5] AndroidManifest.xml:50:17-51 value=(@xml/file_paths).
Suggestion: add 'tools:replace="android:resource"' to <meta-data> element at AndroidManifest.xml:372:13-374:58 to override.

I also added 'tools:replace="android:resource"' but then it just goes back to the old error. What can I do to merge them correctly?

EDIT:

This is my providers_path.xml file:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path
        name="external_files"
        path="." />
</paths>

And this is the file I need to take: Environment.getExternalStorageDirectory() + "/Passenger/passenger_log.txt";

What do I need to add in my provider path exactly? Trying to understand the logic of external-path.

At this moment I get this:

06-11 11:30:51.483: E/AndroidRuntime(16656): 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
06-11 11:30:51.483: E/AndroidRuntime(16656):    at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:604)
06-11 11:30:51.483: E/AndroidRuntime(16656):    at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:578)
06-11 11:30:51.483: E/AndroidRuntime(16656):    at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:416)
06-11 11:30:51.483: E/AndroidRuntime(16656):    at nl.hgrams.passenger.fragments.PSSettingsFragment$3$1$1.onResponse(PSSettingsFragment.java:311)
06-11 11:30:51.483: E/AndroidRuntime(16656):    at nl.hgrams.passenger.utils.Utils$12.onAnimationEnd(Utils.java:552)
06-11 11:30:51.483: E/AndroidRuntime(16656):    at android.view.animation.Animation$3.run(Animation.java:382)
06-11 11:30:51.483: E/AndroidRuntime(16656):    at android.os.Handler.handleCallback(Handler.java:808)
06-11 11:30:51.483: E/AndroidRuntime(16656):    at android.os.Handler.dispatchMessage(Handler.java:101)
06-11 11:30:51.483: E/AndroidRuntime(16656):    at android.os.Looper.loop(Looper.java:166)
06-11 11:30:51.483: E/AndroidRuntime(16656):    at android.app.ActivityThread.main(ActivityThread.java:7425)
06-11 11:30:51.483: E/AndroidRuntime(16656):    at java.lang.reflect.Method.invoke(Native Method)
06-11 11:30:51.483: E/AndroidRuntime(16656):    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
06-11 11:30:51.483: E/AndroidRuntime(16656):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

This code is causing the crash, at the FileProvider line:

  File file = new File(Environment.getExternalStorageDirectory() + "/Passenger/log.zip");
                            Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                            emailIntent.setType("plain/text");
                            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"support@psngr.co"});
                            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Psngr Android Logs");
                            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Thanks for helping us improve Psngr!\n Please describe the issue you found");
                            emailIntent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(getActivity(), Constants.PROVIDER_AUTHORITY, file));
                            startActivity(Intent.createChooser(emailIntent, "Send Feedback..."));
                            PSApplicationClass.getInstance().showSavingPopup = getString(R.string.email_sent);

回答1:

In your code this line

 android:authorities="${applicationId}.myfileprovider" 

should be replace by

 android:authorities="${applicationId}.provider"

your xml file

    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <external-path
            name="external_files"
            path="." />
<root-path
        name="external_files"
        path="/storage/"/>
    </paths>