1

I am trying to use the hidden Android package manager's method installPackage and its callback class IPackageInstallObserver. I managed to retrieve the method in question via Java's reflection but am struggling to implement the hidden observer interface via reflection proxies.

I tried the following approach but it still didn't work. I tried it like this:

// I declared the same interface signature locally
interface IPackageInstallObserver {
    fun packageInstalled(packageName: String?, returnCode: Int)
}

// get the classes and method via reflection
val cPackageManager = Class.forName("android.content.pm.PackageManager")
val cPackageInstallObserver = Class.forName("android.content.pm.IPackageInstallObserver")
installPackage = cPackageManager.getMethod("installPackage", Uri::class.java, cPackageInstallObserver, Integer.TYPE, String::class.java)
INSTALL_REPLACE_EXISTING = cPackageManager.getField("INSTALL_REPLACE_EXISTING").getInt(null)

// create the observer
val installObserver = Proxy.newProxyInstance(
    CustomPackageManager::class.java.classLoader,
    arrayOf<Class<*>>(IPackageInstallObserver::class.java),  // local definition
    InstallObserverInvocationHandler(listener)
) as IPackageInstallObserver // local definition

// the invocation handler
class InstallObserverInvocationHandler(private val listener: PackageListener): InvocationHandler {

    override fun invoke(proxy: Any?, method: Method?, args: Array<out Any>?): Any? {
        if (method?.name == "packageInstalled" && method.parameterTypes.size == 2 &&
                         method.parameterTypes[0] == String::class.java &&
                method.parameterTypes[1] == Integer.TYPE) {

            /* custom implementation */
        }
        return null
    }
}

// how I use the installPackage method
installPackage.invoke(context.packageManager, apkUri, installObserver, INSTALL_REPLACE_EXISTING, INSTALLER_NAME)

However, my installObserver instance is unfortunately null - can somebody tell me what I'm doing wrong? I do have system privileges and required permissions declared.

6
  • But why you did declare that interface signature locally? Commented Aug 2, 2018 at 18:22
  • how am I otherwise able to cast it? without a cast, the installObserver would only be an object and does not satisfy the method signature Commented Aug 2, 2018 at 18:25
  • what method signature? installPackage.invoke have signature of Object, Object... so it does fit perfectly here. Commented Aug 2, 2018 at 18:28
  • did you fixed your issue? Commented Aug 3, 2018 at 9:36
  • yes I did, thanks for your help Commented Aug 7, 2018 at 5:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.