1

I try programmatically to install the APK file. I put it into the "download" folder of the device. I make everything according to the instructions from this link Installing APK

i.e. I put the xml file in Resources/xml/provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="external_files" path="."/>
</paths>

I add the manifest inside application tags

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

And then I try to install APK in code

public void OpenApk()
{
    var path =
        Android.OS.Environment.GetExternalStoragePublicDirectory(
        Android.OS.Environment.DirectoryDownloads);

    Java.IO.File file = new Java.IO.File(path + "/", "test.apk");

    Intent install = new Intent(Intent.ActionView);

    Android.Net.Uri apkURI = FileProvider.GetUriForFile(Forms.Context, Forms.Context.ApplicationContext.PackageName + ".provider", file);
    install.SetDataAndType(apkURI, "application/vnd.android.package-archive");
    install.AddFlags(ActivityFlags.NewTask);
    install.AddFlags(ActivityFlags.GrantReadUriPermission);

    Forms.Context.StartActivity(install);
}

But the installation fails with "There was a problem parsing the package" What can be wrong?

user2273044
  • 161
  • 2
  • 17

2 Answers2

0

I had done a simple and reproduced your problem. The cause of it is the apk file in the media Download folder.There are two solutions for you.

  1. Move the apk file to your app's own folder, such as: var path = GetExternalFilesDir(null).ToString();

  2. Grant your app the permission about accessing all the files.

    Add the permission into the AndroidManifest.xml:

    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
    

    Use the following code to request the permission before you install the apk:

    if (!Android.OS.Environment.IsExternalStorageManager)
            {
                Intent intent = new Intent();
                intent.SetAction(Android.Provider.Settings.ActionManageAppAllFilesAccessPermission);
                Android.Net.Uri uri = Android.Net.Uri.FromParts("package", this.PackageName, null);
                intent.SetData(uri);
                StartActivity(intent);
            }
    

Update:

This is the OnCreate method of my MainActivity and it worked well:

  protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        LoadApplication(new App());
        if (!Android.OS.Environment.IsExternalStorageManager)
        {
            Intent intent = new Intent();
            intent.SetAction(Android.Provider.Settings.ActionManageAppAllFilesAccessPermission);
            Android.Net.Uri uri = Android.Net.Uri.FromParts("package", this.PackageName, null);
            intent.SetData(uri);
            StartActivity(intent);
        }
    }
Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14
0

-> In Archive for Publishing, the Distribution Channel dialog presents two choices for distribution. Select Ad-Hoc -> After Ad-Hoc is selected, Visual Studio opens the Signing Identity page of the dialog as shown in the next screenshot. To publish the .APK, it must first be signed with a signing key (also referred to as a certificate).

-> An existing certificate can be used by clicking the Import button and then proceeding to Sign the APK. Otherwise, click the click the + button to create a new certificate -> The Create Android Key Store dialog is displayed; use this dialog to create a new signing certificate that can be used for signing Android applications. Now sign in and then run on android