title: Possible to use multiple authorities with FileProvider?
body: <h2>Background</h2>
  
  <p>I maintain a <a href="https://github.com/stkent/bugshaker-android" rel="noreferrer">library</a> whose core functionality involves sharing programmatically-captured screenshots to external email applications.</p>
  
  <p>I use a <code>FileProvider</code> to accomplish this, which means my library's manifest contains <a href="https://github.com/stkent/bugshaker-android/blob/df05e919882713c93a44af61f947fb7ac3e99fbc/bugshaker/src/main/AndroidManifest.xml" rel="noreferrer">a <code>&lt;provider&gt;</code> tag</a>:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;provider
      android:name="android.support.v4.content.FileProvider"
      android:authorities="${applicationId}.bugshaker.fileprovider"
      android:exported="false"
      android:grantUriPermissions="true"&gt;
      &lt;meta-data
          android:name="android.support.FILE_PROVIDER_PATHS"
          android:resource="@xml/filepaths" /&gt;
  &lt;/provider&gt;
  </code></pre>
  
  <p><code>filepaths.xml</code> is defined as follows:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;paths&gt;
      &lt;files-path path="bug-reports/" name="bug-reports" /&gt;
  &lt;/paths&gt;
  </code></pre>
  
  <p>A consumer of my library has an application which itself uses a <code>FileProvider</code> to share files. My expectation was that it should be possible to allow both providers to share files if the consuming application used the following manifest <code>&lt;provider&gt;</code> tag:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;provider
      android:authorities="${applicationId}.fileprovider;${applicationId}.bugshaker.fileprovider"
      android:exported="false"
      android:grantUriPermissions="true"
      android:name="android.support.v4.content.FileProvider"
      tools:replace="android:authorities"&gt;
      &lt;meta-data
          android:name="android.support.FILE_PROVIDER_PATHS"
          android:resource="@xml/file_paths"
          tools:replace="android:resource" /&gt;
  &lt;/provider&gt;
  </code></pre>
  
  <p>This manifest entry:</p>
  
  <ul>
  <li>specifies two <code>Provider</code> authorities, <code>${applicationId}.fileprovider</code> (for application file sharing) and <code>${applicationId}.bugshaker.fileprovider</code> (for library file sharing);</li>
  <li>references an updated <code>filepaths.xml</code>, which contains separate directory definitions for application-generated files and library-generated files:</li>
  </ul>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;paths&gt;
      &lt;external-path
          name="redacted"
          path="" /&gt;
      &lt;files-path
          name="bug-reports"
          path="bug-reports/" /&gt;
  &lt;/paths&gt;
  </code></pre>
  
  <p>After building the application, we have confirmed that the generated manifest has had the correct nodes replaced with these updated values.</p>
  
  <p>However, when the application using this configuration is assembled (successfully) and run, we see a crash on launch:</p>
  
  <pre class="lang-none prettyprint-override"><code>E: FATAL EXCEPTION: main
     Process: com.stkent.bugshakertest, PID: 11636
     java.lang.RuntimeException: Unable to get provider android.support.v4.content.FileProvider: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.PackageItemInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
         at android.app.ActivityThread.installProvider(ActivityThread.java:5856)
         at android.app.ActivityThread.installContentProviders(ActivityThread.java:5445)
         at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5384)
         at android.app.ActivityThread.-wrap2(ActivityThread.java)
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545)
         at android.os.Handler.dispatchMessage(Handler.java:102)
         at android.os.Looper.loop(Looper.java:154)
         at android.app.ActivityThread.main(ActivityThread.java:6119)
         at java.lang.reflect.Method.invoke(Native Method)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.PackageItemInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
         at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:583)
         at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:557)
         at android.support.v4.content.FileProvider.attachInfo(FileProvider.java:375)
         at android.app.ActivityThread.installProvider(ActivityThread.java:5853)
         at android.app.ActivityThread.installContentProviders(ActivityThread.java:5445) 
         at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5384) 
         at android.app.ActivityThread.-wrap2(ActivityThread.java) 
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545) 
         at android.os.Handler.dispatchMessage(Handler.java:102) 
         at android.os.Looper.loop(Looper.java:154) 
         at android.app.ActivityThread.main(ActivityThread.java:6119) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
  </code></pre>
  
  <p>Using the debugger, I am able to see that the method <code>FileProvider.parsePathStrategy</code> invokes <a href="https://developer.android.com/reference/android/content/pm/PackageManager.html#resolveContentProvider(java.lang.String,%20int)" rel="noreferrer"><code>PackageManager.resolveContentProvider</code></a> with the authority string <code>"${applicationId}.fileprovider;${applicationId}.bugshaker.fileprovider"</code>. <code>resolveContentProvider</code> then returns null, leading to this NPE.</p>
  
  <p>If I manually call <code>resolveContentProvider</code> while paused at this instruction and pass either <code>"${applicationId}.fileprovider"</code> <em>or</em> <code>"${applicationId}.bugshaker.fileprovider"</code>, <code>resolveContentProvider</code> instead returns a non-null <code>ProviderInfo</code> instance (which would seem to be the expected result).</p>
  
  <p>This difference confuses me because the <a href="https://developer.android.com/guide/topics/manifest/provider-element.html" rel="noreferrer"><code>&lt;provider&gt;</code> element documentation</a> states that multiple authorities are supported:</p>
  
  <blockquote>
    <p>A list of one or more URI authorities that identify data offered by the content provider. Multiple authorities are listed by separating their names with a semicolon. To avoid conflicts, authority names should use a Java-style naming convention (such as com.example.provider.cartoonprovider). Typically, it's the name of the ContentProvider subclass that implements the provider</p>
    
    <p>There is no default. At least one authority must be specified.</p>
  </blockquote>
  
  <h2>Questions</h2>
  
  <ul>
  <li>Is it possible to have a single application expose a <code>FileProvider</code> with multiple authorities and file paths?
  
  <ul>
  <li>If so, what do I need to change to make that work?</li>
  <li>If not, are there other ways to configure file sharing within my library that avoid conflicts such as this one?</li>
  </ul></li>
  </ul>