Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Android app rolls back to a previous version after device shutdown?

Tags:

android

Observed this very strange behavior with Android application. Approximate scenario:

  1. Version A installed on the device
  2. Application works alright
  3. Version B installed on the device (B > A)
  4. Application works alright
  5. Device shuts down due to battery drain
  6. Device turned on
  7. Version A of the application runs on the device again

Additional info:

  • The application isn't distributed through Google Play, but installed on-premise via USB connection (NOTE: the application runs in production; it's not installed through AndroidStudio).
  • Kiosk
  • Android 5.1 (API 22)

I guess I've got two questions:

  • Why the device cached the older version of the APK (and where did it cache it)?
  • Under which circumstances can applications roll back to previous versions like that?

Edit (more information):

  • It looks like after the APK is rolled back, the application loses some permissions (maybe even all). Functionality that worked before the roll-back stops working due to SecurityException being thrown from Android's APIs. This happens even though this version of Android doesn't have runtime permissions yet!
  • After browsing tablet's filesystem, I indeed see several app's APKs residing under similar paths: /data/app/com.myapp-2/base.apk, /data/app/com.myapp-3/base.apk, etc.

My current hypothesis is that battery drain causes the tablet to "reset" its state (for example, the clock is also reset), and when it's powered again it confuses between app's APKs and loads the wrong one.

However, I have no clue why would it do that, or how to prevent this behavior.

like image 933
Vasiliy Avatar asked Oct 24 '19 13:10

Vasiliy


People also ask

How do I stop apps from restarting on Android?

Option 1: Freeze Apps Open “Settings” > “Applications” > “Application Manager“. Choose the app you wish to freeze. Select “Turn off” or “Disable“.

When closing an app on Android what is happening in the backend?

In general, there's no such thing as closing applications in Android: the user just stops using the app. It's up to the programmer to make sure that the user does not mention process creation and termination.

How do I roll back an app update?

In order to enable a rollback, all you need to do is add the –enable-rollback option to the 'pm install' command when updating an app.


2 Answers

If you are using Android Studio 3.5+, then instead of instant run, you are likely using Apply Changes.

This has a different way of shipping changes to the device, without rewriting the apk, so make a lot of sense that after rebooting, the apk that you will run if you execute your app directly on the device, has nothing to do with the one that was running before

Apply Changes

Instant Run and re-architectured and implemented from the ground-up a more practical approach in Android Studio 3.5 called Apply Changes. Apply Changes uses platform-specific APIs from Android Oreo and higher to ensure reliable and consistent behavior; unlike Instant Run, Apply Changes does not modify your APK.

https://android-developers.googleblog.com/2019/08/android-studio-35-project-marble-goes.html

like image 134
Carlos Robles Avatar answered Sep 28 '22 04:09

Carlos Robles


This lists user installed packages:

adb shell cmd package help

pm list packages -f -U -3 --show-versioncode

And then fully uninstall before reinstalling:

adb uninstall com.myapp

With instant run and not applying the patch APK (see the pm help output), this might run the base APK. This does not roll-back anything, but it's likely the one APK without the other one APK overloaded (Android Studio might automate the application of the hot-patch, but at boot time this might not be the case). Not using instant run removes these patch update APK; and when there is only one APK, there is nothing else to run.

like image 25
Martin Zeitler Avatar answered Sep 28 '22 04:09

Martin Zeitler