2

The Story Now I am working on a project where an Android application is running on a custom device and this app is not installed through the Play Store, but the APK file is manually uploaded, installed and launched on the device.

The problem here is when new version is released we have to update the application manually on all devices.

We want to automate this process and build process like this:

  • Install the app
  • Launch the app
  • Start a background process which to check for a new version in out Server
  • Download the new APK file
  • Uninstall the current app / Update the app
  • Install the new app / Update the app

The Questions:

Can we uninstall and install APK file programmatically?

Shell we use the existing app for this job or create a new android app which to handle the new version checking and update the app in the background?

Cœur
  • 37,241
  • 25
  • 195
  • 267
MrVasilev
  • 1,503
  • 2
  • 17
  • 34

3 Answers3

1

Instead of un-installing APK, put the updated APK file on your server, download that in your application.

Use the below function to install the application

fun install(context: Context, packageName: String, apkPath: String) {

// PackageManager provides an instance of PackageInstaller
val packageInstaller = context.packageManager.packageInstaller

// Prepare params for installing one APK file with MODE_FULL_INSTALL
// We could use MODE_INHERIT_EXISTING to install multiple split APKs
val params = PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL)
params.setAppPackageName(packageName)

// Get a PackageInstaller.Session for performing the actual update
val sessionId = packageInstaller.createSession(params)
val session = packageInstaller.openSession(sessionId)

// Copy APK file bytes into OutputStream provided by install Session
val out = session.openWrite(packageName, 0, -1)
val fis = File(apkPath).inputStream()
fis.copyTo(out)
session.fsync(out)
out.close()

// The app gets killed after installation session commit
session.commit(PendingIntent.getBroadcast(context, sessionId,
        Intent("android.intent.action.MAIN"), 0).intentSender)
}

Restarting App After Update

class UpdateReceiver : BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {

    // Restart your app here
    val i = Intent(context, MainActivity::class.java)
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    context.startActivity(i)        
}
}

the full process to silent update application in background is described in below link

https://www.sisik.eu/blog/android/dev-admin/update-app

Kailash Chouhan
  • 2,328
  • 16
  • 16
0

An automated ADB Script could work, maybe paired with a Python script? You would need to trigger a side-load of the APK and then once complete, uninstall

-1

You cannot programmatically install an APK automatically from the device itself.

  • You can either automate the process if the device is rooted using a shell script and pm command
  • You can enable USB Debugging on all the devices and write an adb script to install the apk using a computer
  • You can have another app which does is only check for new APKs and download and start an Intent to install the APK (Oreo and above need explicit permission) and then have the user to tap Install button

However, you are going the wrong way because all the methods mentioned above are very cumbersome. Because the PlayStore exactly does your specified requirements. I strongly recommend you to publish on Playstore.

Sharukh Mohammed
  • 365
  • 2
  • 16