Android: how to edit or port a rom's apk

To edit an Android apk we have to accede at its content and to do this there are several software that simplify the usafe of Apktool:

and if we have to edit the files smail in the odex file, the we can use the tools smali/backsmali.

Here there are some standard commands to do smali/backsmali:

#decompile
java -jar baksmali.jar -o <deodexed_dir> <odex_file>
#compile
java -Xmx512M -jar smali.jar <deodexed_dir> -o <dex_file>

Here we can see some particular usage:

#the commands -b, -p, -m flags clean up the code
java -jar baksmali.jar -b -p -m -o <deodexed_dir> <odex_file>
#the -l changes the .registers to .locals
#the -b removes the smali debugging stuff that is not needed
#most custom rom builders know about this edits but this is for the average user that would like to make edits on their own and want to make edits to system/apps
java -Xmx1024M -jar baksmali.jar -l -b -o <deodexed_dir> <odex_file>
java -jar baksmali.jar -a 15 -x <odex_file> -d <framework_dir> -o <deodexed_dir>
java -Xmx512m -jar smali.jar -o <dex_file> <deodexed_dir>

Now we see how deodex with smali/backsmali from a computer an odex file of an apk or a jar to do a porting,  for example we have the files original_app.apk and original_app.odex extracted from /system/app of a phone of which we also have the directory /system/framework:

java -jar baksmali.jar -a 15 -x original_app.odex -d copy_of_system_framework_dir -o original_app_deodexed_dir
java -Xmx512m -jar smali.jar -o classes.dex original_app_deodexed_dir

then copy the file classes.dex in the file patched_app.apk, that is a copy of the file original_app.apk.

To reodex the file we can use from the phone the tool dexopt-wrapper with the following commands:

dexopt-wrapper /sdcard/tmp/patched_app.apk /sdcard/tmp/patched_app.odex
busybox dd if=/sdcard/tmp/original_app.odex of=/sdcard/patched_app.odex bs=1 count=20 skip=52 seek=52 conv=notrunc