Reputation: 2829
I have scenario where I have to download apk in background and install it without prompting any dialog to user. However when I try to install it using below code
File file = new File(filename);
if(file.exists()){
try {
String command;
command = "pm install -r " + filename;
Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", command });
proc.waitFor();
Config.debug("Apk installed");
} catch (Exception e) {
e.printStackTrace();
}
}
It ask for super user access. Is there any way to install apk without super user and prompting to user?
Upvotes: 2
Views: 8914
Reputation:
You should first look into the Android's native Package Installer. I think you just need to extract the required functionality.
Specifially, if you look at this method and its OnClickListener:
public void onClick(View v) {
if(v == mOk) {
...
startActivity(newIntent);
finish();
} else if(v == mCancel) {
// Cancel and finish
setResult(RESULT_CANCELED);
finish();
}
}
Then you may notice the InstallAppProgress class on where the actual installer is located and the final thing to do is to call the PackageManager
's installPackage(...) function.
public void initView() {
...
pm.installPackage(mPackageURI, observer, installFlags, installerPackageName);
}
Next step is to inspect PackageManager
which is an abstract class. You will find that installPackage(...) function there. The bad news is that this is marked with @hide
. This means it's not directly available to external developers.
/**
* @hide
**/
public abstract void installPackage(Uri packageUri, IPackageInstallObserver installObserver, int flags, String installerPackageName);
But you will be able to access the method with using reflection.
You will reflect it using yourContext.getPackageManager()
. Then you may call the installPackage(...) function.
The following code calls the installation:
try {
getPackageManager().installPackage(packageUri, myObserver, PackageManager.INSTALL_REPLACE_EXISTING, "com.your.package.name");
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
For the whole thing to work you need to declare this to your manifest or the code will fail silently.
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
to get this permission you must install the APK as system which requires ROOT.
however after you have installed the APK as system it seem to work WITHOUT ROOT.
To do this, i created a signed APK and pushed it on:
adb push C:\Users\Example01\Desktop\release\app-release.apk /system/priv-app/MyApp.apk
with this i copied it to /system/priv-app
which requires write access this is why the ROOT required.
I tested it using the debug version and when i tried it, i got the SecurityException
.
You should use a rooted device to install that as an sytem priveligied app.
Hope this helps :)
Upvotes: -2
Reputation: 220
Yes you can install the apk silently without rooting by signing your application with system signature.
Upvotes: 0
Reputation: 840
You can silently install apps starting Android Marshmallow (find info here). You would have to be a device owner in order to accomplish the task.
You can find more help about device owner here. Thanks to Florent Dupont.
Upvotes: 1
Reputation: 1006914
Is there any way to install apk without super user and prompting to user?
Fortunately, no, for obvious security reasons.
UPDATE: One exception is on Android 5.0+ devices, if you are using the device owner system, I think there are some APIs for installing packages that are available to device owner apps. I haven't played with these yet, and it is unclear whether they would meet your needs anyway.
I am asking this becoz the user base that I have, have never used app or device before
Then either do not give them this technology or teach them how to use this technology.
Upvotes: 2