1

I'm having really hard time trying to open downloaded apk

I'm trying to use FileProvider (https://developer.android.com/reference/android/support/v4/content/FileProvider), I've followed this steps but I think I'm doing something wrong. I change this code in different ways many times, but unsuccessful.

Here's what I have:

File downloaded in:

app.setPathUpdate(Environment.getExternalStorageDirectory() + 
                           File.separator + "trovata/update/" +
                           this.getApplicationInfo().packageName + File.separator)

The code above make this outcome: /storage/emulated/0/trovata/update/br.com.trovata.milano.elite/

Here's what I'm doing:

SincronizacaoActivity.java

File FileAppInst = new File(app.getPathUpdate() + "atualizador");
Uri apkUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID+".provider", FileAppInst);
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setData(apkUri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);

AndroidManifest.xml

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<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/filepaths" />
</provider>

filepaths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="atualizador" path="/trovata/update/${applicationId}/"/>
</paths>

Error:

...
Caused by: java.lang.IllegalArgumentException: Failed to find configured 
root that contains /storage/emulated/0/trovata/update/br.com.trovata.milano.elite/atualizador
...

Could you help me?

2 Answers2

0
path="/trovata/update/${applicationId}/"

${applicationId} only turns into your application ID in AndroidManifest.xml. You cannot use manifest placeholders in resource files. So, replace ${applicationId} with your actual application ID.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • thanks, I get it, I have many buildVariants for different apps setup. Is there a way to do it automatically? Because the application ID change for each one. – Carlos Santos May 08 '20 at 20:11
  • @CarlosSantos: Perhaps you should not be putting the application ID into the path this way. Or, as iCantC suggests, you could truncate `path` in the XML to be `/trovata/update`. – CommonsWare May 08 '20 at 20:13
0

In all the encounters with FileProvider it occurs that in most of the cases it only worked when the path was set to path=".".

Replace your filepaths.xml with this

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

And give it a try.

iCantC
  • 2,852
  • 1
  • 19
  • 34