title: How to share current app's APK (or of other installed apps) using the new FileProvider?
body: <h2>Background</h2>
  
  <p>In the past, it was easy to share an APK file with any app you wanted, using a simple command:</p>
  
  <pre><code>startActivity(new Intent(Intent.ACTION_SEND,Uri.fromFile(filePath)).setType("*/*"));
  </code></pre>
  
  <h2>The problem</h2>
  
  <p>If your app targets Android API 24 (Android Nougat) or above, the above code will cause a crash, caused by FileUriExposedException (written about it <a href="https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en" rel="nofollow noreferrer"><strong>here</strong></a>, and an example solution for opening an APK file can be found <a href="https://stackoverflow.com/a/40936215/878126"><strong>here</strong></a>) .</p>
  
  <p>This actually worked for me fine, using below code:</p>
  
  <pre><code>    File apkFile = new File(apkFilePathFromSomewhereInExternalStorage);
      Intent intent = new Intent(Intent.ACTION_SEND);
      Uri fileUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", apkFile);
      intent.setType("text/plain");
      intent.putExtra(Intent.EXTRA_STREAM, fileUri);
      intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
      startActivity(intent);
  </code></pre>
  
  <p>However, the problem is that I also wish to be able to share the current app's APK (and also other installed apps).</p>
  
  <p>For getting the path of the current app's APK, we use this:</p>
  
  <pre><code>        final PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
          File apkFile=new File(packageInfo.applicationInfo.publicSourceDir);
          ...
  </code></pre>
  
  <p>This APK is accessible to all apps without the need of any permission, and so does the APK of every installed app.</p>
  
  <p>But when I use this file with the above code for sharing using the FileProvider, I get this exception:</p>
  
  <pre><code>IllegalArgumentException: Failed to find configured root that contains ...
  </code></pre>
  
  <p>The same goes for when I use a symlinked file to the APK, as such:</p>
  
  <pre><code>        File apkFile=new File(packageInfo.applicationInfo.publicSourceDir);
          final String fileName = "symlink.apk";
          File symLinkFile = new File(getFilesDir(), fileName);
          if (!symLinkFile.exists())
              symLinkPath = symLinkFile.getAbsolutePath();
          createSymLink(symLinkPath, apkFile.getAbsolutePath());
          Uri fileUri= FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", symLinkFile);
          ...
  
  public static boolean createSymLink(String symLinkFilePath, String originalFilePath) {
      try {
          if (VERSION.SDK_INT &gt;= VERSION_CODES.LOLLIPOP) {
              Os.symlink(originalFilePath, symLinkFilePath);
              return true;
          }
          final Class&lt;?&gt; libcore = Class.forName("libcore.io.Libcore");
          final java.lang.reflect.Field fOs = libcore.getDeclaredField("os");
          fOs.setAccessible(true);
          final Object os = fOs.get(null);
          final java.lang.reflect.Method method = os.getClass().getMethod("symlink", String.class, String.class);
          method.invoke(os, originalFilePath, symLinkFilePath);
          return true;
      } catch (Exception e) {
          e.printStackTrace();
      }
      return false;
  }
  </code></pre>
  
  <h2>What I've tried</h2>
  
  <p>I tried to configure the provider_paths.xml file with various combinations of what I thought would help, such as any of those :</p>
  
  <pre><code>&lt;external-path name="external_files" path="."/&gt;
  &lt;external-path path="Android/data/lb.com.myapplication/" name="files_root" /&gt;
  &lt;external-path path="." name="external_storage_root" /&gt;
  &lt;files-path name="files" path="." /&gt;
  &lt;files-path name="files" path="" /&gt;
  </code></pre>
  
  <p>I also tried to disable the strictMode that's associated with this mechanism:</p>
  
  <pre><code>    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
      StrictMode.setVmPolicy(builder.build());
  </code></pre>
  
  <h2>The question</h2>
  
  <p>How can I share any APK file, from every possible path that's accessible to my app, including using symlinked files ? </p>