Titanium Mobile hack: execute your projects from the command line using Make

Appcelerator’s Titanium Mobile is a nice platform to develop native iOS (and Android) applications using JavaScript. Unlike other platforms that provide only a web container to run web applications that pretend to be native, Titanium translates JavaScript code into native applications that look and behave like they were written in Objective-C.

To run Titanium applications you need to use Titanium Developer. This program executes a bunch of scripts to compile your projects using Xcode, starts the application in iOS simulator and so on. Fortunately Titanium is open sourced on Github, and while reading its code I had an idea that would make me feel much more comfortable about running my Titanium applications. If you are a “command-liner” like me, you probably will prefer to execute your applications from your shell using Makefiles, and that’s what my hack is all about.

The first thing I did was to understand how Titanium starts its projects and write a command-line script that replicates this behavior. Titanium uses a script called builder.py to do all the magic, and I wrote a simple wrapper script called titanium.sh that uses builder.py to compile and start the application:

You will notice in the script source that I read some data (the application name and ID) from tiapp.xml. This is the file used by Titanium to store projects’ metadata. if you want to use this script in your projects, it’s important to change the tiapp.xml path according to your project structure. My projects usually have a structure like this:

/bin (directory)
    titanium.sh
/SampleApp (directory)
Makefile

… where:

  • bin is the directory where I put titanium.sh and other utility scripts.
  • SampleApp is the directory where my application lives (this directory has the same name of my application).
  • Makefile lives in the root directory.

If you use this same project structure, your tiapp.xml file will be located at /SampleApp/tiapp.xml. In this case, just copy my script and it will work for you too.

You can also configure Titanium and iOS SDK versions that will be used to compile. Just change the values of *_SDK_VERSION variables to the versions you have installed in your computer. Some variables (like PROJECT_NAME) are parameters that will be passed when the script is called (in our case, this will be informed by the Makefile).

One last thing about titanium.sh is that I use some Perl “black magic” at the end of the script to produce beautiful colored output depending on the log type (info, debug, error, etc.) – just like Titanium Developer does:

After that, you will just need a simple Makefile that will allow you to start the application by simply typing “make run” at the command line:

The Makefile is responsible for calling titanium.sh passing all the necessary variables. Just copy my Makefile to your project root, change the PROJECT_NAME variable and you are good to go!

If you want to take a look on how I use these scripts in my projects, take a look at titanium-jasmine on GitHub. You will notice in this project that you can do a lot of other fancy stuff using Make.

Tags: , , , , , , , , , , , , ,

17 Responses to “Titanium Mobile hack: execute your projects from the command line using Make”

  1. Adam Paxton says:

    Hey, this is very handy. Thanks for sharing!

  2. Rodrigo Pinto says:

    Congrats man! It rockz, faster than to use the conventional way!

  3. Dan Tamas says:

    It’s great, what about a textmate bundle? :)

  4. Alex says:

    This is excellent. I need to automate builds for titanium and this is very helpful. do you know how to run the same project in android emulator?

    • I didn’t do that myself but I suppose that this very same thing would work (maybe with minor changes/improvements). All I’m doing here is accessing the same scripts that are used by Titanium to start the simulator, and those scripts also start the Android simulator.

  5. Wilker says:

    Olá Guilherme,

    Estou tentando fazer esse seu procedimento aqui, mas acho que deve ter mudado algumas coisas, pois agora é o Titanium Studio aqui, e não consigo achar o script em python que faz a compilação…

    Você teria algo pra o SDK mais recente? (Atualmente estou usando o 1.7.2)

  6. Matt says:

    Great info, do you have any more info from your presentation at codestong regarding automated tests / builds etc in Titanium?

  7. Chris says:

    This is excellent, I was about to write something similar but luckily you already did :) .

    Do you know a way to set the log level (to get rid of the [DEBUG] logs, which I don’t need) and/or to strip out certain logs (like all the “[WARN] [object TiUILabel] has an auto width value of 0, meaning this view may not be visible.”) logs?

    I tried adding a “| grep -vE “(DEBUG|visible)”" to the build command, but that leaves you with a ton of empty new lines.

    Thanks for this!
    Cheers

  8. Brandon says:

    Thanks for this. The only part I’m having a problem with is that the simulator loads anyway. Did you run into that when you were developing this? Any idea why that might happen?

Leave a Reply