What is the proper method to manually odex an apk? I am looking for a proper and simple method to odex an apk file. I have an upgraded apk file from a ROM with which I want to replace the older one and it is located in the /system-priv directory. The older apk has the .odex file, but I figured out I can't directly overwrite the it, since I can't use the old .odex (hence I need to odex first), lest I'ill be stuck with app crashes or bootloops.
I have tried searching for methods online but are not satisfactory in one of the ways:

This one which involves the use of a dexopt-wrapper.bin seems to be way outdated as binary is not supported, I have this error poping up:
 error: dexopt-wrapper error: only position independent executables (PIE) are supported.


The second one which is lengthy and links to other required resources are no longer there, and is somewhat not compatible with my system.

The only promising one which uses Auto Odexer Script, but currently I'm running issues adb and also seems it uses the same dexopt-wrapper binary which was giving me errors in terminal.


There are tools/methods for doing the entire system at once, but I  do not need the entire system re-odexed. I want to personally create an odexed &quot;upgraded&quot; .apk from the extracted ROM.
Update
I upgraded the wrapper binary (now PIE) and should be compatible however still encountering this error:
--- BEGIN '/sdcard/app/A.apk' (bootstrap=0) ---
--- waiting for verify+opt, pid=17200
--- would reduce privs here
execl(/bin/dexopt) failed: No such file or directory
--- END '/sdcard/app/A.apk' --- status=0x4300, process failed
1|mido:/ #

and the result is an empty .odex file in the dir. Someone to point out where I am going wrong here?
credits to com.modaco.odexme
xda link

Theoretically you should only need your Android device to manually odex, I ended up using both smartphone (running KitKat 4.4) and PC as explained later.

I successfully odexed apks using the following steps:

1.Create a /dir/odex on your Android device

2.Save next script as odex.sh in /dir/odex, give it execute permission 777

#!/system/bin/sh

cd /dir/odex

for filename in `find . -name '*.apk'`
do

# step 1 - odex the apk
./dexopt-wrapper $filename `echo $filename | sed 's/\(.*\.\)apk/\1odex/'`

# step 2 - did we succesfully odex?
if [ -f `echo $filename | sed 's/\(.*\.\)apk/\1odex/'` ]
then
    # step 3 - remove the classes.dex from the apk
    ./zip -d $filename classes.dex

    # step 4 - zipalign, just in case
    ./zipalign -f -v 4 $filename $filename.new
    mv $filename.new $filename
fi

done;


3.Obtain following binaries from com.modaco.odexme.apk\assets

dexopt-wrapper zip zipalign

put those in /dir/odex and give execute permission 777

4.Copy .apk(s) you want to odex from /system/app or /system/priv-app to /dir/odex

5.Open Terminal, cd to /dir/odex and execute ./odex.sh

At this point I had a problem with zipalign which could not LINK EXECUTABLE - cannot locate symbol... but nonetheless script partially executed and obtained odex(es) sitting next to apk(s) in our dir.

6.So I had to resort to the PC, copy original apk to PC, open it with WinRar or 7zip, manually remove classes.dex and zipalign, than put "deodexed" apk back to Android device.

7.Now with a file explorer put newly created .odex and .apk in /system/app or /system/priv-app (have a backup of original apk first). If it fails to write mount system as rw. Give both permission 644 as all the other apks and odex in dir.

8.You can now delete corresponding dex in /data/dalvik-cache

9.Reboot (or soft-reboot)

10.Enjoy your newly odexed app

I successfully odexed LGEIME.apk, a modified keyboard for my G2, and GoogleDrive.apk, I moved it to /system/app to save space on /data partition because newer version are 5.0+ only. I was skeptical about GoogleDrive because it was multi-dex, infact apk contained classes.dex and classes2.dex: turned out I had to strip only classes.dex from the apk and leave classes2.dex inside the apk.

credits to com.modaco.odexme
xda link