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:
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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With