Install apps silently, with granted INSTALL_PACKAGES permission
Solution 1:
Your first bet is to look into Android's native PackageInstaller. I would recommend modifying that app the way you like, or just extract required functionality.
Specifically, if you look into PackageInstallerActivity and its method onClickListener
:
public void onClick(View v) {
if(v == mOk) {
// Start subactivity to actually install the application
Intent newIntent = new Intent();
...
newIntent.setClass(this, InstallAppProgress.class);
...
startActivity(newIntent);
finish();
} else if(v == mCancel) {
// Cancel and finish
finish();
}
}
Then you'll notice that actual installer is located in InstallAppProgress class. Inspecting that class you'll find that initView
is the core installer function, and the final thing it does is call to PackageManager
's installPackage
function:
public void initView() {
...
pm.installPackage(mPackageURI, observer, installFlags, installerPackageName);
}
Next step is to inspect PackageManager, which is abstract class. You'll find installPackage(...)
function there. The bad news is that it's marked with @hide. This means it's not directly available (you won't be able to compile with call to this method).
/**
* @hide
* ....
*/
public abstract void installPackage(Uri packageURI,
IPackageInstallObserver observer,
int flags,String installerPackageName);
But you will be able to access this methods via reflection.
If you are interested in how PackageManager
's installPackage
function is implemented, take a look at PackageManagerService.
Summary
You'll need to get package manager object via Context
's getPackageManager()
. Then you will call installPackage
function via reflection.
Solution 2:
I have been implementing installation without user consent recently - it was a kiosk application for API level 21+ where I had full control over environment.
The basic requirements are
- API level 21+
- root access to install the updater as a system privileged app.
The following method reads and installs APK from InputStream:
public static boolean installPackage(Context context, InputStream in, String packageName)
throws IOException {
PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
PackageInstaller.SessionParams.MODE_FULL_INSTALL);
params.setAppPackageName(packageName);
// set params
int sessionId = packageInstaller.createSession(params);
PackageInstaller.Session session = packageInstaller.openSession(sessionId);
OutputStream out = session.openWrite("COSU", 0, -1);
byte[] buffer = new byte[65536];
int c;
while ((c = in.read(buffer)) != -1) {
out.write(buffer, 0, c);
}
session.fsync(out);
in.close();
out.close();
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("info", "somedata"); // for extra data if needed..
Random generator = new Random();
PendingIntent i = PendingIntent.getActivity(context, generator.nextInt(), intent,PendingIntent.FLAG_UPDATE_CURRENT);
session.commit(i.getIntentSender());
return true;
}
The following code calls the installation
try {
InputStream is = getResources().openRawResource(R.raw.someapk_source);
installPackage(MainActivity.this, is, "com.example.apk");
} catch (IOException e) {
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
for the whole thing to work you desperately need INSTALL_PACKAGES
permission, or the code above will fail silently
<uses-permission
android:name="android.permission.INSTALL_PACKAGES" />
to get this permission you must install your APK as System application which REQUIRES root (however AFTER you have installed your updater application it seem to work WITHOUT root)
To install as system application I created a signed APK and pushed it with
adb push updater.apk /sdcard/updater.apk
and then moved it to system/priv-app
- which requires remounting FS (this is why the root is required)
adb shell
su
mount -o rw,remount /system
mv /sdcard/updater.apk /system/priv-app
chmod 644 /system/priv-app/updater.apk
for some reason it didn't work with simple debug version, but logcat shows useful info if your application in priv-app
is not picked up for some reason.
Solution 3:
I have checked how ADB installs apps.
- It copies the APK to /data/local/tmp
- it runs 'shell:pm install /data/local/tmp/app.apk'
I have tried to replicate this behaviour by doing: (on pc, using usb-cable)adb push app.apk /sdcard/app.apk
adb shell
$ pm install /sdcard/app.apk
This works. The app is installed.
I made an application (named AppInstall) which should install the other app.
(installed normally, non-rooted device)
It does:Runtime.getRuntime().exec("pm install /sdcard/app.apk").waitFor();
But this gives the error:java.lang.SecurityException: Neither user 10019 nor current process has android.permission.INSTALL_PACKAGES.
It seems like the error is thrown by pm, not by AppInstall.
Because the SecurityException is not catched by AppInstall and the app does not crash.
I've tried the same thing on a rooted device (same app and AppInstall) and it worked like a charm.
(Also normally installed, not in /system or anything)
AppInstall didn't even ask root-permission.
But thats because the shell is always #
instead of $
on that device.
Btw, you need root to install an app in /system, correct?
I tried adb remount on the non-rooted device and got: remount failed: Operation not permitted.
That's why I could not try the /system thing on the non-rooted device.
Conclusion: you should use a rooted device
Hope this helps :)
Solution 4:
You should define
<uses-permission
android:name="android.permission.INSTALL_PACKAGES" />
in your manifest, then if whether you are in system partition (/system/app) or you have your application signed by the manufacturer, you are going to have INSTALL_PACKAGES permission.
My suggestion is to create a little android project with 1.5 compatibility level used to call installPackages via reflection and to export a jar with methods to install packages and to call the real methods. Then, by importing the jar in your project you will be ready to install packages.
Solution 5:
I tried on rooted Android 4.2.2 and this method works for me:
private void installApk(String filename) {
File file = new File(filename);
if(file.exists()){
try {
final String command = "pm install -r " + file.getAbsolutePath();
Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", command });
proc.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}
}