0

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.

tyczj
  • 71,600
  • 54
  • 194
  • 296
  • What you describe sounds more like a system app than a root app as you the title indicates. – Robert Jun 26 '23 at 20:08
  • As I understand it you are able to copy an app to the system/priv-app folder and it becomes a system app where those permissions are granted hence needing root to be able to do that – tyczj Jun 26 '23 at 20:12
  • Yes, but that is how you install the management app you develop. This is unrelated how the app itself manages the other apps, thus I find it very confusing to first talk about root but then describe a system app that has the permission to (un)install other apps. – Robert Jun 26 '23 at 20:38
  • @Robert Again this is just a POC app, I dont have system signing keys so therefore you need root to be able to move the app to the system folder so it is relevant. If I were not to put that I have a rooted device in the question I would just get people telling me you would need root to do this without being a system app – tyczj Jun 26 '23 at 23:05

1 Answers1

0

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

tyczj
  • 71,600
  • 54
  • 194
  • 296