Enable downgrade when installing apk using intent on Android 4.2+
Asked Answered
A

2

8

Is possible to enable downgrade when installing apk using intent on Android 4.2+? I found out that it is possible, when installing app via command shell (using -d) adb install -r -d <link to apk>, so I hope it is somehow possible also via Intent. I was looking for some flag or something, but I didin't find anything helpful.

This is my intent to open package installer:

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri applicatonFileUri = Uri.fromFile(applicationFile);
intent.setDataAndType(applicatonFileUri, PACKAGE_TYPE);
startActivity(intent);
Angle answered 22/10, 2013 at 15:28 Comment(1)
If this has upset your enterprise customers as much as it has mine, star the issue here: code.google.com/p/android/issues/detail?id=62545Fiction
S
14

That's not possible for non-platform (3rd-party) apps: you have to make the installation request directly to PackageManager.

PackageManager has non-public API, installPackage() (line 2584 as of this writing):

/**
 * @hide
 *
 * Install a package. Since this may take a little while, the result will
 * be posted back to the given observer.  An installation will fail if the calling context
 * lacks the {@link android.Manifest.permission#INSTALL_PACKAGES} permission, if the
 * package named in the package file's manifest is already installed, or if there's no space
 * available on the device.
 *
 * @param packageURI The location of the package file to install.  This can be a 'file:' or a
 * 'content:' URI.
 * @param observer An observer callback to get notified when the package installation is
 * complete. {@link IPackageInstallObserver#packageInstalled(String, int)} will be
 * called when that happens.  observer may be null to indicate that no callback is desired.
 * @param flags - possible values: {@link #INSTALL_FORWARD_LOCK},
 * {@link #INSTALL_REPLACE_EXISTING}, {@link #INSTALL_ALLOW_TEST}.
 * @param installerPackageName Optional package name of the application that is performing the
 * installation. This identifies which market the package came from.
 */
public abstract void installPackage(
        Uri packageURI, IPackageInstallObserver observer, int flags,
        String installerPackageName);

where one of the possible flags is INSTALL_ALLOW_DOWNGRADE:

/**
 * Flag parameter for {@link #installPackage} to indicate that it is okay
 * to install an update to an app where the newly installed app has a lower
 * version code than the currently installed app.
 *
 * @hide
 */
public static final int INSTALL_ALLOW_DOWNGRADE = 0x00000080;

All these APIs are hidden and not accessible for 3rd-party apps. Now, you may try reflection, but I am pretty positive that platform will restrict access to them anyway.

Syllogistic answered 28/10, 2013 at 11:13 Comment(1)
PackageManager and PackageInstaller are available to apps that are the "device owner".Tabulate
L
0

Another possible solution is to have another app which uninstall your app first and then install it again. I couldn't find another way, if someone find a better solution, please let me know :)

Lukin answered 2/3, 2018 at 11:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.