Installing app with android.permission.INSTALL_PACKAGES permission and root

324 views Asked by At

This is just a POC app that I am creating to try something but I have an older rooted device and trying to create an installer app that can manage other apps on the device by itself. I added the permissions

<uses-permission android:name="android.permission.INSTALL_PACKAGES"/>
<uses-permission android:name="android.permission.DELETE_PACKAGES"/>

to my manifest

Using this PackageInstaller example I am getting a dummy apk file I have on the device and trying to install it

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    try{
        val apk = File(Environment.getExternalStorageDirectory(), "sample_app.apk")
        Log.d("MainActivity", "Path: ${apk.path}")

        val params = PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL)
        params.setAppPackageName(packageName)

        val sessionId = packageManager.packageInstaller.createSession(params)
        val session = packageManager.packageInstaller.openSession(sessionId)

        apk.inputStream().use { apkStream->
            session.openWrite("apk-install", 0, apk.length()).use {sessionStream ->
                Log.d("MainActivity", "Copying")
                apkStream.copyTo(sessionStream)

                Log.d("MainActivity", "Sync")
                session.fsync(sessionStream)
            }
        }

        val intent = Intent(this, InstallReceiver::class.java)
        val pi = PendingIntent.getBroadcast(
            this,
            RootInstallService::class.hashCode(),
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )

        Log.d("MainActivity", "Commit")
        session.commit(pi.intentSender)
        session.close()
    }catch (e: Exception){
        e.printStackTrace()
    }

}

I ran the app to install it on the device, copied the base.apk and lib folder from data/app/ to a folder in system/priv-app. Deleted the app from data/app and rebooted the device.

Opening the app once rebooted it runs without error but the app does not appear in the launcher.

Yes I am aware I can install with using adb commands in the app but I don't want to do this, I am looking for a native sdk solution.

I am not sure what I am missing.

1

There are 1 answers

0
tyczj On BEST ANSWER

The issue was that I was setting the app package name to my app

params.setAppPackageName(packageName)

which according to the docs says that is that if the app package does not match the app being installed it will fail so I just removed that line and it started working