I'm trying to add an ability to my android-app to update itself via external link to apk with new version. I'm downloading an apk from external link (I uploaded new build of my app), than I'm using packageInstaller to update an app, and application updates successfully. But after update it crashes with error:
VerityUtils pid-594 E Failed to measure fs-verity, errno 1: /data/app/~~QTV2DULtWFYdJpO7yGn7sg==/com.test.app--JKzM4i9A3BAuvFAm9x9YQ==/base.apk
What can be a reason of this? Seems like installation apk-file is corrupted? But how it is possible if update was successfull? (I know this because I see some new text in UI that I added in new version of app)
P.S. app has device owner rights
Here is my code:
class UpdateAppViewModel(application: Application): AndroidViewModel(application) {
fun download() {
viewModelScope.launch {
withContext(Dispatchers.IO) {
val fileUrl = "https://my-path-to-ne-apk-s3.net/test-app"
val intent = Intent(Intent.ACTION_INSTALL_PACKAGE).apply {
data = Uri.parse(fileUrl)
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_ACTIVITY_NEW_TASK
}
Log.d("UPD", "START DOWNLOAD")
val packageInstaller = getApplication<Application>().packageManager.packageInstaller
val sessionParams = PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL)
val sessionId = packageInstaller.createSession(sessionParams)
val session = packageInstaller.openSession(sessionId)
val inputStream = URL(fileUrl).openStream()
val outputStream = session.openWrite("test-app.apk", 0, -1)
inputStream.copyTo(outputStream)
session.fsync(outputStream)
inputStream.close()
outputStream.close()
Log.d("UPD", "FINISH DOWNLOAD")
val pendingIntent = PendingIntent.getBroadcast(getApplication<Application>().applicationContext, 0, intent, 0)
Log.d("UPD", "BEFORE COMMIT!")
session.commit(pendingIntent.intentSender)
Log.d("UPD", "AFTER COMMIT!")
session.close()
}
}
}
}
Basicly, it seems like a normal behavior. All I did to improve user experience - added broadcast reciever with intent to launch main activity after app has been updated. So, for user i looks like app has been updated and restarted. Here is the code:
class PackageInstallerStatusReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Log.d("UPD!", "APP HAS BEEN UPDATED!")
if (context == null) {
Log.d("ERR!", "Context should not be null")
return
}
val stIntent = Intent(context, MainActivity::class.java)
startActivity(context, stIntent, null)
return
}
}
Also, I noticed that in erlier Android versions the behavior is not the same - app crashes, but there is no error form VerityUtils. Looks like some security mechanism in Android 13.
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