How do I self install APK fixing error parsing APK?

Issue

I have an Android mobile app on a Zebra device that will stay in house. All that works well but I have been struggling with the updates. I have code to check version numbers and can download a new signed APK to the device in a folder (MyFolder). That works. I can install the update from that folder if need be but the users will need it to just update without having to learn that process. I have read and tried many things in this group and others, I’m sure it is something super simple. I make sure the device has the file, the device allows the application to install, and has access to the device storage. I get the message error parsing package and I’m just not sure where to go next. Here is code I have

AndroidManifest.xml

    <provider android:name"android.support.v4.content.FileProvider" android:authorities"${applicationId}.provider" android:exported"false" android:grantUriPermissions"true">

MainActivity.cs (ProcessUpdate())

    Android.Net.Uri fileUri  Android.Support.V4.Content.FileProvider.GetUriForFile(context, context.PackageName+".provider", myDir);

    bool binstall  context.PackageManager.CanRequestPackageInstalls();

    if (!binstall)
    {
      context.StartActivity(new Intent(Android.Provider.Settings.ActionManageUnknownAppSources));
    }
    Intent intent  new Intent(Intent.ActionView);

    intent.AddFlags(ActivityFlags.NewTask);
    intent.PutExtra(Intent.ExtraNotUnknownSource, true);
    intent.SetDataAndType(fileUri, "application/vnd.android.package-archive");
    intent.SetFlags(ActivityFlags.ClearTask | ActivityFlags.NewTask);
    intent.AddFlags(ActivityFlags.GrantReadUriPermission);

    try
    {
        context.StartActivity(intent);
    }
    catch (ActivityNotFoundException ex)
    {
        WritetoLog("ActivityNotFoundException ProcessUpdate - " + ex.Message);
    }
    catch (Exception ex)
    {
    WritetoLog("Exception ProcessUpdate - " + ex.Message);
    }

Solution

I replaced the intent with PackageInstaller and ensured that the Activity was doing LaunchMode LaunchMode.SingleTop on the CS page. It does ask the user if they want to install, upon accepting, it closes the app and does not relaunch, but for now that is an easy workaround to relaunch it.

    PackageInstaller packageInstaller  context.PackageManager.PackageInstaller;
    PackageInstaller.SessionParams sessionParams  new PackageInstaller.SessionParams(PackageInstallMode.FullInstall);
    int sessionId  packageInstaller.CreateSession(sessionParams);
    var session  packageInstaller.OpenSession(sessionId);

    AddApkToInstallSession(newFile.ToString(), session);

    // Create an install status receiver.
    Intent intent  new Intent(this, this.Class);
    intent.SetAction(PACKAGE_INSTALLED_ACTION);
    PendingIntent pendingIntent  PendingIntent.GetActivity(this, 0, intent, 0);
    IntentSender statusReceiver  pendingIntent.IntentSender;

    // Commit the session (this will start the installation workflow).
    session.Commit(statusReceiver);

Answered By – Cami

Leave a Comment