Last Friday I had a meeting with a posible client, who had made an app with a freelance developer using Appcelerator Titanium. Now he wished to make upgrades to the app, but he realized he didn’t have the source code for it and the original developer was nowhere to be found.
I’ve heard these kind of stories a lot of times before, but I though as I’m creating compilers that generate Titanium code i wonder: how recoverable is the source code of a Titanium app from an existing APK ? I found it a nice challenge, so i started digging.
Almost any Android developer knows that there exist tools to unpack and decompile the assets and class files from a APK. It’s basically a zip file that contains a resources.arsc, classes.dex and a binary manifest.xml. Using apktool you can extract the assets (images, sounds, fonts, raw objects, xml layouts, decrypt the manifest) from almost any APK, and using dex2jar you can transform the classes.dex file into a normal ‘jar’ file, which you can then decompile using any class2java tool (example jd or jadx).
Now for this task, or any like it, the first step is to get the resources, so I made my own one step tool called APK_unpack to do that job easier using NodeJS and a java-bridge. This small step was a challenge but solved quickly (i had never made before a nodejs app that consumed java classes directly).
Surprisingly I was able to get the source code for the entire app !(at least into memory)
After playing a bit with the extracted code, i found that some versions of Titanium minify the JS compiled code in different ways, so i though it would be nicer if I could restore the original structure for the app instead of just having the compiled resources (very much readable by the way). For this, i made another NodeJS program called ti_recover, which uses all the packages made before and creates an output dir trying to restore the structure of a real Ti app. I’m making this for solving the requirement of my client, but thought it may be of use for other Titanium developers that have lost their code (which i have seen several times on quora).
To tell you the truth this frightened me, because i have made and published more than a dozen titanium apps, but helps me understand how can i better protect my code, and the ones of my clients. And don’t misunderstand me, I love Titanium, I find it a very good product, but sometimes happens you need to do get your source back, and now I know you actually can.