iPhone Java Create iPhone/iPad Applications using Java

25Jul/110

We now have events !

We have just added basic wrappers for all the Titanium SDK events, enabling type-safe handling of events in your iPhoneJava projects.

Since the code is automatically generated from Titanium's documentation, and their documentation lacks information about the types of the event properties, most of the properties currently use the Object type. We are going to manually add the type information over time.

This change is a significant step towards making the project useful for production, as event handling is a very important aspect of every user-interface application.

Examples for some of the new event wrappers:

  • BatteryEvent - monitors the device's battery level
  • ClickEvent - fired when the user taps on a button/widget
  • ShakeEvent - detects a "shake" gesture
  • VolumeChangeEvent - fired when the user changes the volume of a media player widget

These are just a few examples for the events provided by the Titanium Platform. The new update adds wrapper for all the events supported in Titanium, which totals to 133 different events.

23Jul/110

iPhoneJava goes with Maven

You can now build the gwt-titanium module using maven. Just clone (or download) the latest version from our GitHub repository and run the following command:

mvn install

This will build the project and install it to your local maven 2 repository. You can then start using the gwt-titanium module in your own iOS Java projects. If you don't have the "mvn" command installed on your system, you can download it from the Apache Maven downloads page. The project should compile with Maven version 3 (and hopefully also version 2).

iPhoneJava Maven Project Structure

An iPhoneJava Maven project has the following structure:

  • src/main/java - Holds all your java classes source code
  • src/main/resources - Holds all your projects resources
  • src/main/resources/tiapp.xml - Titanium Application Descriptor (see below)
  • src/main/assembly/titanium.xml - Describes how to assemble the Titanium project (see below)
  • target - This is where the compiled project goes
  • pom.xml - Maven build file

Using maven for your own project

You can also take advantage of maven to automatically build, package and run your iOS java application. Just follow the steps below:

Step 1 - Add the gwt-maven-plugin to your pom.xml

Add the following code inside the plugins section of your pom.xml:

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>gwt-maven-plugin</artifactId>
    <version>2.3.0-1</version>
    <executions>
       <execution>
         <configuration>
           <module>org.urish.iphonejava.examples.helloworld.HelloWorld</module>
           <style>pretty</style>
         </configuration>
         <goals>
           <goal>compile</goal>
         </goals>
      </execution>
    </executions>
  </plugin>

Make sure to replace the 'org.urish.iphonejava.examples.helloworld.HelloWorld' with your own GWT module name. If you wish to use GWT version other than 2.3.0, make sure you change the version number here and also in the dependecies (see step 2).

Step 2 - Add dependencies

Add the following dependencies to your pom.xml:

  <dependencies>
    <dependency>
      <groupId>com.google.gwt</groupId>
      <artifactId>gwt-user</artifactId>
      <version>2.3.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.urish.gwt-titanium</groupId>
      <artifactId>gwt-titanium</artifactId>
      <version>0.1.0</version>
    </dependency>
  </dependencies>

Step 3 - Add assembly plugin

The assembly plugin will take your GWT generated code and automatically create a Titanium project from it. First, add the following into the plugins section of your pom.xml:

  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2.1</version>
    <configuration>
      <descriptors>
        <descriptor>src/assemble/titanium.xml</descriptor>
      </descriptors>
    </configuration>
    <executions>
      <execution>
        <id>make-assembly</id>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

Then, create a file named 'titanium.xml' under the 'src/assemble'. This file describes how to put together the Titanium project from the GWT compiled javascript code. The file contents is as follows:

  <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>titanium</id>
    <formats>
      <format>dir</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
      <fileSet>
        <directory>${basedir}/src/main/resources</directory>
        <outputDirectory>/Resources</outputDirectory>
        <excludes>
          <exclude>tiapp.xml</exclude>
        </excludes>
      </fileSet>
    </fileSets>
    <files>
      <file>
        <source>${project.build.directory}/${project.artifactId}-  ${project.version}/${project.artifactId}/${project.artifactId}.js</source>
        <outputDirectory>/Resources</outputDirectory>
        <destName>app.js</destName>
      </file>
      <file>
        <source>${basedir}/src/main/resources/tiapp.xml</source>
        <outputDirectory>/</outputDirectory>
      </file>
    </files>
  </assembly>

Finally, create your tiapp.xml file under the src/main/resources directory. For example:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
  <ti:app xmlns:ti="http://ti.appcelerator.org">
    <deployment-targets>
      <target device="mobileweb">false</target>
      <target device="iphone">true</target>
      <target device="ipad">true</target>
      <target device="android">true</target>
      <target device="blackberry">false</target>
    </deployment-targets>
    <guid>0abb784b-a5dd-40dd-adff-c829d36d373c</guid>
    <id>org.urish.iphonejava.examples.helloworld</id>
    <name>HelloWorld</name>
    <version>1.0</version>
    <publisher>Uri Shaked</publisher>
    <url>http://</url>
    <description>not specified</description>
    <copyright>2011 by Uri Shaked</copyright>
    <icon>appicon.png</icon>
    <persistent-wifi>false</persistent-wifi>
    <prerendered-icon>false</prerendered-icon>
    <statusbar-style>default</statusbar-style>
    <statusbar-hidden>false</statusbar-hidden>
    <fullscreen>false</fullscreen>
    <navbar-hidden>false</navbar-hidden>
    <analytics>true</analytics>
    <iphone>
       <orientations device="iphone">
         <orientation>Ti.UI.PORTRAIT</orientation>
       </orientations>
       <orientations device="ipad">
         <orientation>Ti.UI.PORTRAIT</orientation>
         <orientation>Ti.UI.UPSIDE_PORTRAIT</orientation>
         <orientation>Ti.UI.LANDSCAPE_LEFT</orientation>
         <orientation>Ti.UI.LANDSCAPE_RIGHT</orientation>
       </orientations>
    </iphone>
    <android xmlns:android="http://schemas.android.com/apk/res/android">
    </android>
    <modules>
    </modules>
  </ti:app>

Make sure to customize the following values: guid, id, name, version, publisher, url, description, copyright. The guid parameter should be simply a random GUID.

Step 4 (optional) - Add exec plugin

If you follow this step, you will be able to build and lunch your project in the iPhone Simulator directly from maven. Add the following code under the plugins section of your pom.xml:

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.1</version>
    <executions>
      <execution>
        <goals>
          <goal>exec</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <executable>/Library/Application Support/Titanium/mobilesdk/osx/1.7.2/titanium.py</executable>
      <workingDirectory>${project.build.directory}/${project.artifactId}-${project.version}-titanium</workingDirectory>
      <arguments>
        <argument>run</argument>
        <argument>--platform=iphone</argument>
      </arguments>
    </configuration>
  </plugin>

Note that you will have to change the path to the titanium.py script to match the installed version of Titanium SDK on your system.

Give it a go!

Once you have configured your iPhoneJava project to run with maven, run the following command to build it:

mvn package

If you also followed step 4, you will be able to run it directly from maven using the following command:

mvn package exec:exec

Example Project

We are working on creating a complete, working example project with the Apache Maven build system. Once the example project is complete, we will put a download link here.

12Jul/110

“Hello World” iPhone Application, written in Java

Event handling example in the Hello World Java application for iPhone

No development platform is complete without an "Hello World" example. In this post, I am going to give you a simple "Hello World" iPhone application written entirely in Java using the gwt-titanium SDK. The example shows how to create UI controls, add them to screen, add an event listener and display an alert message when a button is clicked.

You can pull the fully working example code from our github repository.

Instuctions how to build the example can be found in our blog post iPhone Java project kicks off.

The Code

public class HelloWorld extends GwtTitaniumBootstrap {
  @Override
  public void main() {
    Window win = UI.createWindow();
    win.setBackgroundColor("white");
    win.setTitle("Hello, World!");
 
    API.info("Hello World example started");
 
    Label label = UI.createLabel();
    label.setColor("blue");
    label.setTextAlign("center");
    label.setText("Hello World !");
 
    label.addClickHandler(new EventCallback() {
      @Override
      public void onEvent(JavaScriptObject event) {
        AlertDialog alertDialog = UI.createAlertDialog();
        alertDialog.setTitle("Example");
        alertDialog.setMessage("Hello, World");
        alertDialog.show();
      }
    });
 
    win.add(label);
    win.open(null);
  }
}

Screen Shots

"Hello World" main screen:

Clicking the blue "Hello World" label yields the following popup alert message:

10Jul/111

iPhone Java project kicks off!

iPhone Java is a new initiative enabling developers to create iPhone applications using the Java programming language. The project takes advantage of existing technologies (namely Google Web Toolkit and Appcelerator's Titanium) in order to enable the creation of native-looking iPhone applications written entirely in Java.

Underlaying Technologies

The Google Web Toolkit (GWT) technology enables seamless and effective compilation of java code into javascript code, and is developed by google. The framework is originially targeted at web developers, but can also be extended to support additional scenarios.

The Appcelerator Titanium SDK is a framework which enables creating native-looking mobile applications for various mobile platforms, including the iOS and Android platforms. The application code is written in javascript, then packaged with a native javascript runtime engine and a set of modules providing access to most of the native device functionality.

iPhone Java (code-named 'gwt-titanium') is a set of Java bindings for the Titanium APIs, along with a custom GWT linker, enabling the compilation of java code into a javascript equivalent that can run directly under the Titanium platform.

Project Status

There is no official release yet, but a significant amount of code has already been comitted to our github repository. There is currently very little documentation, one working 'hello-world' example, initial (non-working) code base for a Kitchen Sink example, and comprehensive javadocs for the API bindings based on Titanium SDK documentation. The code for the project is released under the Apache License, Version 2.0.

'Hello World' Example

The hello world example provides a quick proof of concept and serves as a starting point for new projects. An upcoming blog post will cover the 'Hello World' example in detail, including some screenshots.

Obtaining & Building

Get a fresh copy of the latest source code from github. If you are not familiar with github, you can download a zipped copy by clicking the "Downloads" button on the top-right side of the github page, then you will get a dialog window offering you to download the source code directly.

Next, place the files "gwt-user.jar" and "gwt-dev.jar" from GWT 2.2 distribution under the 'lib' directory. Newer version of GWT will also work. The latest GWT sdk (at the time of writing) can be downloaded here: http://google-web-toolkit.googlecode.com/files/gwt-2.3.0.zip.

Finally, run the "build.xml" file using the ANT build system. This will create a file named "gwt-titanium.jar" under the 'build' directory, which is the GWT module you use for creating iOS applications in Java.

Building 'Hello World'

Simlpy run the "build.xml" ant file in the 'examples/hello-world' directory. This will result in a file called "HelloWorld.js" under 'examples/hello-world/build', which is the javascript compiled version of the example.

Running 'Hello World'

You will need a Mac device for running the 'Hello World' example on your iOS device. If you only have a windows machine, you can still use the framework and utilize the Titanium support for android in order to run the example inside the Android emulator or on a real Android-based device. You will also need the Appcelerator Titanium SDK installed (free, under the Apache 2 License).

Create a new mobile project inside the Titanium Studio IDE. After you have created the project, simply copy and paste all the content from the HelloWorld.js you built in the previous step to 'Resources/app.js' in the newly created project. Run the new application from within the Titanium Studio and you are ready to go!

If you need assistance in installing the Titanium SDK and creating a project, check out Appcelerator's Help Center.

Credits

The iPhone Java project is lead by Uri Shaked.

I would also like to credit the gwt-node project, which provided a great start for creating the custom GWT linker.

Final Words

The gwt-titanium project is just in the beginning. I would love to hear your thoughts and ideas and see how far we can go with this project.