"Optimising apps" bug or feature?

When upgraded from Kitkat to Lollipop,  during boot it said,

"Optimising apps, 1 of 132"

took around 20 minutes to complete.  Obviously I thought that will be one-time process but it repeats randomly during boot.
Its quite annoying. When it took like 30 minutes, I decided to search for solutions. Seems like well-known issue and lot
of people such different answers like:

  1. clear cache.
  2. wipe cache partition via bootloader.
  3. don't reboot while charing.
  4. re-flash firemware
  5. Don't reboot the device at all!
  6. Downgrade to kitkat.

1,2,3 they didn't work me. Not sure, whether 4 will help or not. 5th one is seriously funny solution!

Looks like 6th (going back to Kitkat)  is the only option.

But I have rooted device and thought about debugging the issue further.

Here's what I found:

// apk is selected
newfstatat(AT_FDCWD, "/data/app/com.google.android.talk-1/base.apk", {st_mode=S_IFREG|0644, st_size=29530582, ...}, 0) = 0
newfstatat(AT_FDCWD, "/data", {st_mode=S_IFDIR|0771, st_size=4096, ...}, 0) = 0
newfstatat(AT_FDCWD, "/data/dalvik-cache/arm", {st_mode=S_IFDIR|0711, st_size=16384, ...}, 0) = 0
// Android tries to find files now
openat(AT_FDCWD, "/data/dalvik-cache/arm/data@[email protected]@[email protected]", O_RDONLY|O_LARGEFILE) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/data/app/com.google.android.talk-1/arm/base.odex", O_RDONLY|O_LARGEFILE) = -1 ENOENT (No such file or directory)

// Since files are not present, Packagemanager will run 'dexopt' on above package
writev(3, [{"\0", 1}, {"\305c", 2}, {"%\335\273X-}\313(", 8}, {"\0", 1}, {"\4", 1}, {"PackageManager\0", 15}, {"Running dexopt on: /data/app/com"..., 117}], 7) = 145
sendmsg(25, {msg_name(0)=NULL, msg_iov(1)=[{"Z\0", 2}], msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 2
sendmsg(25, {msg_name(0)=NULL, msg_iov(1)=[{"dexopt /data/app/com.google.andr"..., 90}], msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 90
recvmsg(25, {msg_name(0)=NULL, msg_iov(1)=[{"\1\0", 2}], msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 2
recvmsg(25, {msg_name(0)=NULL, msg_iov(1)=[{"0", 1}], msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 1
// Few seconds later, /data/dalvik-cache/arm/data@[email protected]@[email protected] is created.

1- During boot app apk files are converted into somekind of 'optimized' dex file.
2- These files are stored under /data/dalvik-cache/arm/
3- During Reboot somehow those dalvik-cache files vanished or probably /data/dalvik-cache/arm
files will be removed when system needs more space.(directory-name itself says its cache,
so its not a surprise, anyway).
4- Now step 1 gets repeated for all apk files in the mobile.

That's why I'm having this stupid 'optimising apps' message and wastes the time. Honestly, when
its obvious that you need odex files to even start 5.1 version, why its stored in cache location
instead of other locations?

Once we know the problem, there is lot of solutions/workarounds. The one trick is to ensure the file are
locked once they created in /data/dalvik-cache/arm/. This work-around took me couple of hours
to complete.

I wonder why Google wasted so many hours of its Lollipop users by storing file in wrong location.
If these odex files are so critical, why not simply store them under /data/app/<package-name> instead of
using cache location like /data/dalvik-cache/arm/?

During upgrades of specific new odex can replace the old ones. And kept the booting process to avoid
"Optimising app" phase or reduce the time.

Some may call 'Optimising apps' as a feature, but for me its look like a feature with  'blocker' bug,
which wasted user time. This could have been avoided.

[1] Rooted app - which mimics chattr +i/-i to lock/unlock odex files.