2

I have been trying to make an Android system application to automatically install an apk. It was working fine in Marshmallow.

My application is platform signed and android.uid.system.

Here is the code:

private static boolean installPackage(Context context, InputStream in, String packageName)
        throws IOException {


    PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
    PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
            PackageInstaller.SessionParams.MODE_FULL_INSTALL);
    params.setAppPackageName(packageName);
    logg("extra: " + Intent.EXTRA_REFERRER);
    params.setReferrerUri(Uri.parse(Intent.EXTRA_REFERRER));
    params.setOriginatingUri(Uri.parse(Intent.EXTRA_ORIGINATING_URI));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        params.setOriginatingUid(1000);
    }
    // set params
    int sessionId = packageInstaller.createSession(params);
    PackageInstaller.Session session = packageInstaller.openSession(sessionId);
    OutputStream out = session.openWrite("COSU", 0, -1);
    byte[] buffer = new byte[65536];
    int c;
    while ((c = in.read(buffer)) != -1) {
        out.write(buffer, 0, c);
    }
    session.fsync(out);
    in.close();
    out.close();

    session.commit(createIntentSender(context, sessionId));
    return true;
}

private static IntentSender createIntentSender(Context context, int sessionId) {
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
            context,
            sessionId,
            new Intent(ACTION_INSTALL_COMPLETE),
            0);
    return pendingIntent.getIntentSender();
}
Maarten
  • 6,894
  • 7
  • 55
  • 90
Prabeer
  • 21
  • 1
  • I am kind of surprised this code worked in Marshmallow. For security reasons, I would expect only OS-level applications to have permissions to automatically install apps and all other apps to require the user to explicitly agree. When you say "Android system application", do you mean something other than an ordinary app? – Maarten May 23 '17 at 08:38
  • 1
    Yes it is not ordinary app, it is system application which has root privileges. I am trying to build an application manager – Prabeer May 23 '17 at 09:13
  • as it has root privileges, just put the apk into `/system/app` should work. `pm` will detect that directory and automatically do installing – Jiang YD May 23 '17 at 09:17
  • @JiangYD my application needs to install other applications, my application already in /system/priv-app – Prabeer May 23 '17 at 09:37

0 Answers0