I have a system-privileged app with android:persistent=true in < application>. When I update it (via ADB or any other way), it fails to update properly and crashes.
What I'm seeing is that the system installs the update while the current (system-installed) version is still running. During the update the system does not stop the process (either attempts to stop and fails or doesn't try at all). After the update completes, the app seems to undergo a "restart" - I'm seeing components being initialized such as Application::onCreate() being called. But this is happening on same process as before the update!
Consequently (upon launching some activity of the app), the app crashes with "weird" exceptions such as failing to cast class to itself:
Caused by: java.lang.ClassCastException: com.XX.YY.ZZ.ClassName cannot be cast to com.XX.YY.ZZ.ClassName
While investigating, I saw that the ClassLoader used after the update does not refer to the the path of the updated APK, but remains pointing to the path of the original version:
Expected classloader:
dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.app.package-1/base.apk"],nativeLibraryDirectories=[/data/app/com.app.package-1/lib/x86_64, /data/app/com.app.package-1/base.apk!/lib/x86_64, /system/lib64, /vendor/lib64]]]
Actual classloader:
dalvik.system.PathClassLoader[DexPathList[[zip file "/system/priv-app/Appname.apk"],nativeLibraryDirectories=[/system/lib64/Start, /system/priv-app/Appname.apk!/lib/x86_64, /system/lib64, /vendor/lib64, /system/lib64, /vendor/lib64]]]
I'm assuming this is a result of not restarting the process during update.
Is there a way to update an app with persistent=true? Or is it an expected behavior, such app cannot be updated by the common update procedure (e.g. posting newer version on Google Play)?