Installing an Android apk programmatically as part of a test framework

Samm Bennett picture Samm Bennett · Dec 8, 2011 · Viewed 7.9k times · Source

I'm attempting to install an apk programmatically, but I'm not having much luck. I'm setting up an automated test framework targeting physical devices, and I want to have the test devices retrieve the latest apk from the build server before running tests. While I am aware that there is no general way to actually install an apk without the user's consent, I'm curious if there might be some approach available in the case where a developer owns both the apk and device.

Approaches I've tried in the past (the apk has been downloaded to pathName/apkFilename):

String command = "adb install " + pathName + apkFilename;
Runtime.getRuntime().exec(command);

And:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(pathName + apkFilename)), "application/vnd.android.package-archive");
getActivity().startActivity(intent);

While I haven't been able to get the first approach to work, the second attempt creates a system dialog asking the user to confirm the installation (so almost there, but not quite). Since it is a System dialog, I, unfortunately, cannot use Robotium to confirm.

Answer

Merlyn Morgan-Graham picture Merlyn Morgan-Graham · Dec 8, 2011

A lot of people are trying to solve similar problems. I believe it may not be possible to install an APK without confirmation, at least not easily:

I've accepted for a while now that it's impossible to silently install an application on Android

You cannot silently install app, its not supported by Android for obvious reasons. Application installation requires user intervention to continue.

Workarounds?

You need the app to have the android.permission.INSTALL_PACKAGES permission.

There are some hints on those threads about how to do this if you have certain priveleges, though it might be hard to get your app to run that way. You might have to install to a special directory, and/or you might have to run as a special user (which might be hard to do).

One possible way to run the app with elevated permissions: How can I get root permissions through the Android SDK?

On this thread, they mention you might have to "root" your phone to enable that permission:

I wouldn't be surprised if this voids the warranty though. You mentioned in the comments on your post that you don't have "control over the device", so that might kill this option too.

There is some mention on this thread of exploits that some apps use, but I don't think they're supported. If they still work, they might stop working at some point.