Install / Unistall From Shell Command In Android
Solution 1:
As I promised here is the solution to this problem, without doing any forcing to the system other than having to install the whole application in the /system/app directory. I have followed, then did some fixing to the excellent article here: http://paulononaka.wordpress.com/2011/07/02/how-to-install-a-application-in-background-on-android/. I have downloaded the zip file referenced in the article then, (I tried to keep the same class names where possible):
- created a new project and a main activity as entry point
package com.example.silentinstuninst;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import com.example.instuninsthelper.ApplicationManager;
import com.example.instuninsthelper.OnDeletedPackage;
import com.example.instuninsthelper.OnInstalledPackage;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
publicclassMainActivityextendsActivityimplementsOnClickListener {
Process install;
Button btnInstall, btnUninstall;
EditText txtApkFileName, txtPackageName;
publicstaticfinalStringTAG="SilentInstall/Uninstall";
privatestatic ApplicationManager am;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeValues();
}
privatevoidinitializeValues() {
btnInstall = (Button) findViewById(R.id.btnInstall);
btnUninstall = (Button) findViewById(R.id.btnUninstall);
txtApkFileName = (EditText) findViewById(R.id.txtApkFilePath);
txtPackageName = (EditText) findViewById(R.id.txtPackageName);
btnInstall.setOnClickListener(this);
btnUninstall.setOnClickListener(this);
try {
am = newApplicationManager(this);
am.setOnInstalledPackage(newOnInstalledPackage() {
publicvoidpackageInstalled(String packageName, int returnCode) {
if (returnCode == ApplicationManager.INSTALL_SUCCEEDED) {
Log.d(TAG, "Install succeeded");
} else {
Log.d(TAG, "Install failed: " + returnCode);
}
}
});
am.setOnDeletedPackage(newOnDeletedPackage() {
publicvoidpackageDeleted(boolean succeeded) {
Log.d(TAG, "Uninstall succeeded");
}
});
} catch (Exception e) {
logError(e);
}
}
privatevoidlogError(Exception e) {
e.printStackTrace();
Toast.makeText(this, R.string.error+e.getMessage(), Toast.LENGTH_LONG).show();
}
@OverridepublicvoidonClick(View v) {
switch (v.getId())
{
case R.id.btnInstall:
// InstallUninstall.Install(txtApkFileName.getText().toString());try {
am.installPackage(Environment.getExternalStorageDirectory() +
File.separator + txtApkFileName.getText().toString());
} catch (IllegalArgumentException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InvocationTargetException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} // install packagebreak;
case R.id.btnUninstall:
// InstallUninstall.Uninstall(txtPackageName.getText().toString());try {
am.uninstallPackage(txtPackageName.getText().toString());
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logError(e);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logError(e);
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logError(e);
}
break;
}
}
}
- create in /src the package com.example.instuninsthelper. I have added there the ApplicationManager.java and OnInstalledPackage.java files
- inserted the following code inside the ApplicationManager class:
private OnDeletedPackage onDeletedPackage;
classPackageDeleteObserverextendsIPackageDeleteObserver.Stub {
publicvoidpackageDeleted(boolean succeeded)throws RemoteException {
if (onDeletedPackage != null) {
onDeletedPackage.packageDeleted(succeeded);
}
}
}
- created, under the same com.example.instuninsthelper package the file OnDeletedPackage.java with the following code:
package com.example.instuninsthelper;
publicinterfaceOnDeletedPackage {
publicvoidpackageDeleted(boolean succeeded);
}
- in the android.content.pm package (the namespace SHOULD not be changed) I modified the IPackageDeleteObserver.java, with this result:
package android.content.pm;
publicinterfaceIPackageDeleteObserverextendsandroid.os.IInterface {
publicabstractstaticclassStubextendsandroid.os.Binder implementsandroid.content.pm.IPackageDeleteObserver {
publicStub() {
thrownewRuntimeException("Stub!");
}
publicstatic android.content.pm.IPackageDeleteObserver asInterface(android.os.IBinder obj) {
thrownewRuntimeException("Stub!");
}
public android.os.IBinder asBinder() {
thrownewRuntimeException("Stub!");
}
publicbooleanonTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags)throws android.os.RemoteException {
thrownewRuntimeException("Stub!");
}
}
publicabstractvoidpackageDeleted(boolean succeeded)throws android.os.RemoteException;
}
- build the application in Eclipse and deploy it to the emulator
- in the emulator: home button > Settings > applications > ...uninstall the application (because it is not installed in /system/app, and we just needed the generation of the apk file)
- do the following to root the emulator (so that we can write in /system/app; other solution, that I have used, is to generate a custom ROM with this app included into the /system/app):
- download the su file from here http://forum.xda-developers.com/showthread.php?t=682828http://forum.xda-developers.com/showthread.php?t=682828. Rename it to su.zip
- then from the console: * adb shell mount -o rw,remount -t yaffs2 /dev/block/mtdblock03 /system * adb push su.zip /system/xbin/su * adb shell chmod 06755 /system * adb shell chmod 06755 /system/xbin/su
- from the console, go to the /bin directory of the project, then enter: * adb push .apk /system/app
- finally, always from the console, enter: * adb shell am start -n com.example.silentinstuninst/com.example.silentinstuninst.MainActivity
- enjoy!
Solution 2:
Don't know, but just a idea:
I think that you are writing in the standarout, not executing a command nor giving extra data to the process via its input. I think it should be:
Runtime.getRuntime().exec("pm install /mnt/sdcard/HelloAndroid.apk\n");
Hope this helps.
Solution 3:
Installing in the /system/app
directory is essentially the same as requiring root.
Assuming you have root, check out RootTools. Then you can do:
if (RootTools.isAccessGiven()) {
CommandCapture command = new CommandCapture(0, "pm install " + PATH_TO_APK);
RootTools.getShell(true).add(command).waitForFinish();
}
Note that waitForFinish()
is a blocking call!
Solution 4:
Well you can do this also with the PackageManager directly (requires root access):
- Create an app with a platform-sdk which has the interfaces publicly (create or download it, and configure eclipse)
- In the app directly call the hidden API functions which allow silent install/remove.
- Install the APK on your device as a system app by copying it to /system/app (root needed)
See this: http://forum.xda-developers.com/showthread.php?t=1711653
Solution 5:
Runtime.getRuntime().exec("pm install /mnt/sdcard/HelloAndroid.apk\n");
This works for me, although two more additional details have to be done:
Add android:sharedUserId="android.uid.system" in AndroidManifest.xml.
Signed the apk with the system key.
But in this way it seems there is no way to tell whether the installation is succeeded, so I will try @Ginger's method later.
Post a Comment for "Install / Unistall From Shell Command In Android"