Skip to content

Instantly share code, notes, and snippets.

@Fuzion24
Created May 6, 2012 16:53
Show Gist options
  • Save Fuzion24/2623253 to your computer and use it in GitHub Desktop.
Save Fuzion24/2623253 to your computer and use it in GitHub Desktop.
Code that will install and remove apks from an Android device without user interaction
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.packagemanager.poc"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.DELETE_PACKAGES" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".PackageManagerActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
/*
* This file is auto-generated. DO NOT MODIFY.
* Original file: /Users/fuzion24/Development/workspace/PackageManager/src/android/content/pm/IPackageDeleteObserver.aidl
*/
package android.content.pm;
/**
* API for deletion callbacks from the Package Manager.
*
* {@hide}
*/
public interface IPackageDeleteObserver extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements android.content.pm.IPackageDeleteObserver
{
private static final java.lang.String DESCRIPTOR = "android.content.pm.IPackageDeleteObserver";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an android.content.pm.IPackageDeleteObserver interface,
* generating a proxy if needed.
*/
public static android.content.pm.IPackageDeleteObserver asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = (android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof android.content.pm.IPackageDeleteObserver))) {
return ((android.content.pm.IPackageDeleteObserver)iin);
}
return new android.content.pm.IPackageDeleteObserver.Stub.Proxy(obj);
}
public android.os.IBinder asBinder()
{
return this;
}
@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
{
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_packageDeleted:
{
data.enforceInterface(DESCRIPTOR);
boolean _arg0;
_arg0 = (0!=data.readInt());
this.packageDeleted(_arg0);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements android.content.pm.IPackageDeleteObserver
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
public void packageDeleted(boolean succeeded) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(((succeeded)?(1):(0)));
mRemote.transact(Stub.TRANSACTION_packageDeleted, _data, null, android.os.IBinder.FLAG_ONEWAY);
}
finally {
_data.recycle();
}
}
}
static final int TRANSACTION_packageDeleted = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
public void packageDeleted(boolean succeeded) throws android.os.RemoteException;
}
/*
* This file is auto-generated. DO NOT MODIFY.
* Original file: /Users/fuzion24/Development/workspace/PackageManager/src/android/content/pm/IPackageDeleteObserver.aidl
*/
package android.content.pm;
/**
* API for deletion callbacks from the Package Manager.
*
* {@hide}
*/
public interface IPackageDeleteObserver extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements android.content.pm.IPackageDeleteObserver
{
private static final java.lang.String DESCRIPTOR = "android.content.pm.IPackageDeleteObserver";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an android.content.pm.IPackageDeleteObserver interface,
* generating a proxy if needed.
*/
public static android.content.pm.IPackageDeleteObserver asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = (android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof android.content.pm.IPackageDeleteObserver))) {
return ((android.content.pm.IPackageDeleteObserver)iin);
}
return new android.content.pm.IPackageDeleteObserver.Stub.Proxy(obj);
}
public android.os.IBinder asBinder()
{
return this;
}
@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
{
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_packageDeleted:
{
data.enforceInterface(DESCRIPTOR);
boolean _arg0;
_arg0 = (0!=data.readInt());
this.packageDeleted(_arg0);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements android.content.pm.IPackageDeleteObserver
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
public void packageDeleted(boolean succeeded) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(((succeeded)?(1):(0)));
mRemote.transact(Stub.TRANSACTION_packageDeleted, _data, null, android.os.IBinder.FLAG_ONEWAY);
}
finally {
_data.recycle();
}
}
}
static final int TRANSACTION_packageDeleted = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
public void packageDeleted(boolean succeeded) throws android.os.RemoteException;
}
package com.packagemanager.poc;
import java.io.File;
import java.lang.reflect.Method;
import android.app.Activity;
import android.content.pm.IPackageDeleteObserver;
import android.content.pm.IPackageInstallObserver;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.net.Uri;
public class PackageManagerActivity extends Activity {
public static final int INSTALL_REPLACE_EXISTING = 0x00000002;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
String fileName = "/sdcard/test.apk";
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
installPackage(fileName);
}
public void installPackage(String fileName){
try {
PackageManager pkgManager = getPackageManager();
Method installPackage = pkgManager.getClass().getMethod("installPackage", android.net.Uri.class, IPackageInstallObserver.class ,int.class,java.lang.String.class);
Uri androidAPK = Uri.fromFile(new File(fileName));
Object[] params = new Object[] { androidAPK, null, INSTALL_REPLACE_EXISTING, "" };
installPackage.invoke(pkgManager, params);
} catch (Exception e) {
e.printStackTrace();
}
}
public void deletePackage(String packageName){
try {
PackageManager pkgManager = getPackageManager();
Method deletePackage = pkgManager.getClass().getMethod("installPackage",String[].class, IPackageDeleteObserver.class, int.class);
Object[] params = new Object[] {packageName, null, 0 };
deletePackage.invoke(pkgManager, params);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@BrianLusina
Copy link

Why is IPackageInstallObserver look like this?

public interface IPackageDeleteObserver extends android.os.IInterface{ ... }
Why is it similar to IPackageDeleteObserver?

@tshank30
Copy link

tshank30 commented Apr 2, 2018

I am using rooted device and made my app as system app. Install is properly working but Uninstall is not working at all. I am using android version 7.1.2 and device model is Moto E3 Power.

@emmanbol
Copy link

Hi, found this interesting, but a little confused.
Do I need to copy all this files into my project then run it right?

@arpit07531
Copy link

IPackageInstallObserver.java also has the definition of IPackageDeleteObserver inside,

@tombaki
Copy link

tombaki commented Jun 1, 2020

`/*

  • This file is auto-generated. DO NOT MODIFY.
  • Original file: frameworks/base/core/java/android/content/pm/IPackageInstallObserver.aidl
    /
    package android.content.pm;
    /
    *
  • API for installation callbacks from the Package Manager.
  • @hide
    /
    public interface IPackageInstallObserver extends android.os.IInterface
    {
    /
    * Local-side IPC implementation stub class. /
    public static abstract class Stub extends android.os.Binder implements android.content.pm.IPackageInstallObserver
    {
    private static final java.lang.String DESCRIPTOR = "android.content.pm.IPackageInstallObserver";
    /
    * Construct the stub at attach it to the interface. /
    public Stub()
    {
    this.attachInterface(this, DESCRIPTOR);
    }
    /
    *
  • Cast an IBinder object into an android.content.pm.IPackageInstallObserver interface,
  • generating a proxy if needed.
    */
    public static android.content.pm.IPackageInstallObserver asInterface(android.os.IBinder obj)
    {
    if ((obj==null)) {
    return null;
    }
    android.os.IInterface iin = (android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR);
    if (((iin!=null)&&(iin instanceof android.content.pm.IPackageInstallObserver))) {
    return ((android.content.pm.IPackageInstallObserver)iin);
    }
    return new android.content.pm.IPackageInstallObserver.Stub.Proxy(obj);
    }
    public android.os.IBinder asBinder()
    {
    return this;
    }
    @OverRide public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
    {
    switch (code)
    {
    case INTERFACE_TRANSACTION:
    {
    reply.writeString(DESCRIPTOR);
    return true;
    }
    case TRANSACTION_packageInstalled:
    {
    data.enforceInterface(DESCRIPTOR);
    java.lang.String _arg0;
    _arg0 = data.readString();
    int _arg1;
    _arg1 = data.readInt();
    this.packageInstalled(_arg0, _arg1);
    return true;
    }
    }
    return super.onTransact(code, data, reply, flags);
    }
    private static class Proxy implements android.content.pm.IPackageInstallObserver
    {
    private android.os.IBinder mRemote;
    Proxy(android.os.IBinder remote)
    {
    mRemote = remote;
    }
    public android.os.IBinder asBinder()
    {
    return mRemote;
    }
    public java.lang.String getInterfaceDescriptor()
    {
    return DESCRIPTOR;
    }
    public void packageInstalled(java.lang.String packageName, int returnCode) throws android.os.RemoteException
    {
    android.os.Parcel _data = android.os.Parcel.obtain();
    try {
    _data.writeInterfaceToken(DESCRIPTOR);
    _data.writeString(packageName);
    _data.writeInt(returnCode);
    mRemote.transact(Stub.TRANSACTION_packageInstalled, _data, null, android.os.IBinder.FLAG_ONEWAY);
    }
    finally {
    _data.recycle();
    }
    }
    }
    static final int TRANSACTION_packageInstalled = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
    }
    public void packageInstalled(java.lang.String packageName, int returnCode) throws android.os.RemoteException;
    }`

@shahinyotech
Copy link

I am using rooted device and made my app as system app. Install is properly working but Uninstall is not working at all. I am using android version 7.1.2 and device model is Moto E3 Power.

will help me with this ?

@shahinyotech
Copy link

i am using rooted device but its not working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment