As you may already known, Titanium SDK 3.2 is around the corner and amongst its many new features and enhancements, the CLI (command-line interface) has gotten more useful and powerful, as you can see in this recent slide deck by Chris Barber. When you use the Titanium CLI YOU are in control and you can use any text editor you’d like; in fact Titanium Studio uses the CLI for its own compilation and build operations.
In preparation for this new release, in this blog post we’ll explore how to get started with the CLI and start taking advantage of its flexibility.
Install the CLI
If you already have Titanium installed and working, then you already have Node.js and the CLI installed so you can skip to “Create a Project”. If you’re performing a fresh Titanium installation, you could use your text editor of choice together with the CLI.
Assuming you’re starting from scratch, make sure you have Xcode (Mac-only) and the Android SDK Manager installed. Remember that you’ll have to open up the Android SDK Manager to download the actual SDKs you’d like to use for testing.
Download and install Node.js.
Open a Terminal/Command Window and follow these steps.
$ [sudo] npm install titanium –g
$ titanium login
Install Titanium SDK and set it as default
$ titanium sdk update --branch 3_1_X –default
$ titanium setup
Note: Make sure you provide the correct path to the Android SDK
Configure additional settings
$ titanium setup --advanced
Note: The command ti is a shortcut to titanium and I’ll be using it from here on end.
Create a project
The CLI will allow you to send flags/arguments as a single instruction. All these flags are documented in the official documentation. Knowing them all, or at least the most important ones, is the key to unlocking the flexibility of the CLI because then you can use it for scripting and automation.
In the case of creating a new project, the CLI offers you a friendly way of providing flags using screen prompts.
$ ti create
By running ti create you’re invoking a wizard of sorts that will ask you for details about this new project. When finished, your new project will be created. One very useful flag is -d, which overrides the default project directory you specified during the setup process. If for example your default folder is /tiprojects, you could write something like
$ ti create -d /tiproject/newworkspace
to create this particular project in a different directory.
Since iOS has only 1 simulator, building a project for iOS is pretty straight-forward.
$ ti build -p ios
Building for Android requires additional flags to specify the emulator to use, its skin (screen size) and architecture (arm, x86, etc). To know which emulator to use you’ll need to run a command provided by the android sdk
$ android list targets
This command will provide you a list and details of all the virtual devices you have installed. You don’t have to run this command every time, but only when you need to find the id of the emulator you wish to use. Notice that each of them has a sequential id. Take note of the one you wish to use, for example 11. To build for android using the WVGA854 skin and using an x86 emulator you’ll call:
$ ti build -p android -I 11 -S WVGA854 -B x86
This command will build your app, invoke the specified Android emulator if it is not already running, install and run your app.
If you’d like to build to a Android device, simply run
$ ti build -p android -T device
or for iOS
$ ti build -p iOS -T device
Note: Building for iOS devices requires you to specify the certificate and provisioning profile to use, and currently the app will be deployed to iTunes. The 3.2 CLI will include the feature of bypassing iTunes and build straight to the iOS device.
CleanTo clean your project, go to your project folder and run
$ ti clean
These are the basics of the CLI. Make sure you check the official documentation for details about all the available commands. At any moment you could use the –help flag to get more information about the command you’re running. For example: $ ti build –help will give you a description of all the possible flags for the build command.