Monday, June 2, 2014

App development with GWT, Cordova and Netbeans Part 4

This is the forth part of my blog series on how to develop mobile apps with GWT, Cordova and Netbeans. The initial description can be found here.
In the previous parts we did create a MGWT project and did put a "Hello world" button on the screen and finaly did build the native apps for android and windows phone.

Web apps are all nice, but often you wish to access some features of your mobile device which is not available to web applications. Some common things are the camera, sensors and so on.

With cordova/phonegap we fortunally can use these devices via native plugins.
Just a warning: Any plugin you use must be available on all target platforms, otherwise you will get in troubles. (There are ways to handle such situations, but they are outside the scope of this tutorial)

The first step to allow such access, is to teel the maven pom.xml that we wish to use phonegap "features".
Just add this dependency in your pom.xml file:

<dependency>
    <groupId>com.googlecode.gwtphonegap</groupId>
    <artifactId>gwtphonegap</artifactId>
    <version>2.4.0.0</version>
</dependency>


The second think to change, is you index.html file.
There you have to specify that cordova.js is started, so the html<->native bridge is activated.

Somewhere in your index.html you have the line which starts the gwt java script.
It looks like this:
<script type="text/javascript" language="javascript" src="mgwt1/mgwt1.nocache.js"></script>

Before that line, add the call to the cordova.js file, so the phonegap environment is initialized.

It should then look like this:

<script type="text/javascript" language="javascript" src="cordova.js"></script>
<script type="text/javascript" language="javascript" src="mgwt1/mgwt1.nocache.js"></script>

It is important that the cordova.js in loaded before the GWT script, because the initialisation order is important.

Finally you must tell the phonegap build service to include some native plugins, otherwise the cordova.js will be missing in the package.
For this add these lines to your src/main/phonegap-build/config.xml:

<feature name="http://api.phonegap.com/1.0/notification"/>
<feature name="http://api.phonegap.com/1.0/device"/>
<gap:plugin name="org.apache.cordova.device" version="0.2.8" />
<gap:plugin name="org.apache.cordova.dialogs" version="0.2.6" />

With these four lines you include the device information features (You can retrieve the platform informations) and show native notifications.

In the start() method of your "java" class, you add the initialisation of the phonegap environment.

PhoneGap phoneGap = GWT.create(PhoneGap.class);
boolean isPhoneGap= phoneGap.isPhoneGapDevice();
           
if (isPhoneGap)
{
    phoneGap.addHandler(new PhoneGapAvailableHandler()
    {
        @Override
        public void onPhoneGapAvailable(PhoneGapAvailableEvent event)
        {
            // Here you have the native "bridge" up and running
        }
    });
   phoneGap.addHandler(new PhoneGapTimeoutHandler()
   {
       @Override
       public void onPhoneGapTimeout(PhoneGapTimeoutEvent event) 
       {
           //can not start phonegap - something is for with your setup
       }
    });
}
The last step required, is tell GWT to use the phonegap API too.
So in your <project.gwt.xml uncomment this line:

<inherits name="com.googlecode.gwtphonegap.PhoneGap"/>

With this you now have access to all the features as described here.
Of course you need to speicfy access rights for you app, otherwise you will not be allowed to interact with the native phone system. (Camera etc.)

You can download the sample project from this place.

I hope you enjoyed this tutorial.
If you struggle somewhere, please drop me a note, so I can enhance the tutorial.


App development with GWT, Cordova and Netbeans Part 3

This is the first part of my blog series on how to develop mobile apps with GWT, Cordova and Netbeans. The initial description can be found here.
In the previous parts we did create a MGWT project and did put a "Hello world" button on the screen.
This is shown just nice on any browser, but is not yet a native mobile app.
In this part we will use the adobe phonegap build service to package our small web app as a native app which can be put in a app store of the big players.
Packaging the app for the different target systems is a tedious work, since you must install the different SDK, in the correct versions. And you can't develop all version on a single platform. For example iOS apps need a OS-X system to build them.

Fortunally there exists a build service from adobe which allows you to upload your web app as zip file (or take the source from a git repository) and then build the app for all the target platforms you wish.

The free version of the service allows you to build one private app and as many opensource apps as you wish.
If you need to build more private (or better closed source ) apps, then you can buy a upgrade for $9.99/Month which allows you to build up to 25 private applications.

We will continue with the free account and use this to build our closed source app, directly from inside netbeans. So you won't have to upload the zip file manually and can fully automate your builds.

The first step to build a native app, is extending the maven build to include the build service. Fortunally Chris has written a maven plugin which handles this very well.

In your pom.xml you just add the dependency to the new plugin:

<plugin>
    <groupId>com.github.chrisprice</groupId>
    <artifactId>phonegap-build-maven-plugin</artifactId>
    <version>0.0.7</version>
    <executions>
        <execution>
            <id>phonegap-build</id>
            <goals>
                <goal>clean</goal>
                <goal>build</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
   
    <platforms>            
            <!-- <platform>ios</platform> -->
       
    <platform>android</platform> 
            <platform>winphone</platform>
        </platforms>
    </configuration>                                   
</plugin>

With this your build process will push the zip file to the adobe build service and retrieve the final platform packages.
Since the build service is password protected, you need to specify your credentials.

This can simply be done by specifying which server should be used for building in your pom.xml file. (Almost on top of it)

<properties>
    <gwtversion>2.5.0</gwtversion>
    <phonegap-build.server>phonegap-build</phonegap-build.server>
</properties>

The second part is to specify your personal credentials. As it's a bad idea to put them in the pom.xml, you should put them in default maven settings file.
The maven settings file is called settings.xml and is usually located in the .m2 directory of the users home folder.

The content should look like this:

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <servers>
        <server>
            <id>phonegap-build</id>
            <username>yourlogin@adobe.com</username>
            <password>MySecurePassword</password>
        </server>
    </servers>
</settings>



The last step before starting the build is to specify a config.xml file, to instruct the build service what should be done.
This file is located under src/main/phonegap-build and should have this content:

<?xml version="1.0" encoding="UTF-8"?>
    <widget xmlns = "http://www.w3.org/ns/widgets"
        xmlns:gap = "http://phonegap.com/ns/1.0"
        id        = "org.company.project"
        versionCode=""
        version   = "1.0.0">
        <name>My test project</name>
        <description></description>
        <author href="http://www.aarboard.ch" email="support@aarboard.ch">André Schild</author>
        <preference name="phonegap-version" value="3.4.0" />
        <preference name="orientation" value="default" />
        <preference name="fullscreen" value="true" />
        <preference name="target-device" value="universal" />
        <preference name="webviewbounce" value="true" />
        <preference name="prerendered-icon" value="true" />
        <preference name="stay-in-webview" value="true" />
        <preference name="ios-statusbarstyle" value="black-opaque" />
        <preference name="detect-data-types" value="true" />
        <preference name="exit-on-suspend" value="true" />
        <preference name="show-splash-screen-spinner" value="true" />
        <preference name="auto-hide-splash-screen" value="true" />
        <preference name="EnableViewportScale" value="false" />
        <preference name="MediaPlaybackRequiresUserAction" value="false" />
        <preference name="AllowInlineMediaPlayback" value="false" />
        <preference name="BackupWebStorage" value="cloud" />
        <preference name="TopActivityIndicator" value="gray" />
        <preference name="KeyboardDisplayRequiresUserAction" value="true" />
        <preference name="HideKeyboardFormAccessoryBar" value="false" />
        <preference name="SuppressesIncrementalRendering" value="false" />
        <preference name="android-minSdkVersion" value="7" />
        <preference name="android-installLocation" value="auto" />
        <preference name="SplashScreenDelay" value="5000" />
        <preference name="ErrorUrl" value=""/>
        <preference name="BackgroundColor" value=""/>
        <preference name="KeepRunning" value="false"/>
        <preference name="DisallowOverscroll" value="false"/>
        <preference name="LoadUrlTimeoutValue" value="20000" />
        <preference name="disable-cursor" value="true" />
        <gap:platform name="android" />
        <gap:platform name="winphone" />
        <access origin="http://127.0.0.1*" />
    </widget>


You can now rebuild your project and should have the two app packages in the target/phonegap-build folder.
One is a .apk file for android, the other a .xap file for windows phone.

You could now transfer these files to your mobile device and test them, or you can go to the adobe build service page and scan the QR code from there.


If you modify your code, you can then just rebuild the app and reinstall it on your phone.
If subsequent builds fail, then you have to specify which APP number to use. Otherwise the build service thinks you wish to cretae multiple private apps.

To prevent this, you note the app id from the build service (see screenshot above) and then add this to your pom.xml file in the plugin section.

    </platforms>
    <appId>9xxxxxx</appId>                            </configuration>                   

As you may have noted, we did now build the app for Android and Windows phone. When you wish to build your app for iOS, then you need a developer account from apple. This account costs $99.-/Year.
You have to create a account here and then register for iOS development. You will then receive a signing key, with which you have to sign ANY iOS application you develop. There is no other way to install software on your iOS devices (unless you root the device)

In the next and last part of the serie we will look at what is required to access native features for your mobile device. In short you need to integrate the cordova plugins and initialize them correctly.

App development with GWT, Cordova and Netbeans Part 2

This is the second part of my blog series on how to develop mobile apps with GWT, Cordova and Netbeans. The initial description can be found here.

In the first part we did create a basic MGWT project from a maven template and did run it in the web browser of our choise.
It did just display a grey divider line on the screen, similiar to this.

Now we wish to show some real content on the page. For this we need to mobify the main class of the application.
The main application class is defined in your <project>.gwt.xml file.
Look for the  <entry-point> xml tag, this is where your application starts.

You can now open this java file and search for the start() method.
The simplest way to get our hello world is to modify the start() method to only show a button with hello world as caption.

So your start() method should look like this:

private void start() {
       
      //set viewport and other settings for mobile
            MGWT.applySettings(MGWTSettings.getAppSetting());
            LayoutPanel mainPanel= new LayoutPanel();
            mainPanel.setWidth("100%");
            Button button = new Button("Hello world");
            button.setWidth("100%");
            mainPanel.add(button);
            RootPanel.get().add(mainPanel);       
    }


Don't delete the onModuleLoad method, this one is required.
After a rebuild und run of the project, you should now see the "Hello world" button on your browser, similar to this printscreen.


Initial description
Part 1
In the next part we will now go to the magic, and put this in a android and windows phone app.

App development with GWT, Cordova and Netbeans Part 1

This is the first part of my blog series on how to develop mobile apps with GWT, Cordova and Netbeans. The initial description can be found here.

To develop with Netbeans, of course you need the Netbeans IDE.
I suggest downloading the latest version 8.0 from the official website and install it. Just make sure you take the "All" edition, which includes support for Java and HTML5.

When the IDE is installed, you should download and install the GWT plugin for netbeans. It can be downloaded directly from the netbenas plugins web page.

The plugin then requires the GWT environment. If not yet installed, download it from here (The GWT SDK is enough) and unpack it somewhere on your local disk.

After this your development environment is ready to start.

When netbeas is started you first create a new maven project target, select "Maven" and then "Project from Archtype"

Then enter the specified values below in the 4 text fields to generate a MGWT target:
 Group-ID: com.googlecode.mgwt.archetypes
Artifact ID: mgwt-standard-archetype
Version: 1.0.8
Repository: http://www.m-gwt.com/m2/repo



 

Now you specify where to store your new project and your own "namespace", usually something like com.company and the project name.


After finishing you have the skeleton for the MGWT project ready. In the project name you see "Archtype generated mgwt app", with a right click on the project you can open the project dialog and put in a meaningfull project name.




So you are now ready to run the project.
Just right click on the project and select "Run"




It now asks you on which application server you with to run it, don't be worried, you won't need it for the final deployment. But for development purposes your code needs to be accessible via a http connection. So just select one of the installed application servers. (When you select "Remember Permanently" it won't aks you over and over again)



Netbeans now invokes the GWT compiler and trys to build your web application.


Unfortunaly the build will fail, complaining about a missing clientcode.xml

------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
------------------------------------------------------------------------
Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.2-beta-5:single (assemble) on project mgwt1: Error reading assemblies: Error locating assembly descriptor: src/main/assembly/clientcode.xml

[1] [INFO] Searching for file location: C:\Develop\Sourceforge\mgwt1\src\main\assembly\clientcode.xml

[2] [INFO] File: C:\Develop\Sourceforge\mgwt1\src\main\assembly\clientcode.xml does not exist.


The clientcode.xml contains some informations on what should be packaged for the final app.
Just create the clientcode.xml in the folder src/main/assembly with this content:

<assembly>
  <id>clientcode</id>
  <includeBaseDirectory>false</includeBaseDirectory>
  <formats>
    <format>zip</format>
  </formats>
  <fileSets>
   <fileSet>
   <directory>target/${project.artifactId}-${project.version}</directory>
   <includes>
  <include>**/*.js</include>
   <include>**/*.png</include>
  <include>**/*.rpc</include>
  <include>**/*.html</include>
  <include>**/*.gif</include>
  </includes>
   <outputDirectory></outputDirectory>
   </fileSet>
  </fileSets>

</assembly>


Now you can run the project once more, and if your default browser is a webkit based browser you will see the screen similar to this:



If you instead receive a error message like :
ERROR: Possible problem with your *.gwt.xml module file.
The compile time user.agent value (safari) does not match the runtime user.agent value (gecko1_8). Expect more errors


then you have started your web app in another browser which is not based on webkit.
If you wish to continue using your belowed browser, then just change the compile option in the <project>.gwt.xml file. Just remove the line <set-property name="user.agent" value="safari" /> and re run your project.



This is the first step in building a GWT based mobile app with netbeans.
In the second step we will change the content of the start screen, you we see our belowed "Hello World" text.

Quickstart mobile app development with GWT, Cordova and Netbeans

Mobile app development is a fast moving target. You have many options on how to develop mobile apps.

The most sophisticated way is to write native apps for each platform...
Of course you will need to master the different languages and development environments. And since you will have to develop the same functionality for several platforms, you will code it multiple times in different languages.
Happy you if you find a customer willing to pay for it.

If you wish to use the "write once run anywhere" system, then you will be landing on web apps. Fortunally today mobile devices are in most cases fast enough to run you web app without feeling slugish. So you basically can write a html 5 website and make it available as a app.
For this you can use the phonegap / cordova framework.

If you need access to some native parts of the mobile device (Sensors, camera etc.) then you can use many of the available plugins which then allows you to access these hardware specific features.

Even better, you can use the adobe build service to build the whole application from your html/javascript sources.

If you come from the java world, then writing javascript isn't that sexy, you could then use GWT to write your web app in java and let the compiler generate java script stuff from it.
The performance of the generated code is very good, you will have a hard time to make such optimized java script code.

In this blog post serie I will guide you through the process of creating a project in netbeans, which you can then use to build your mobile application from your familiar ide.

Such apps are able to run under iOS, Android and Windows Phone. Of course platform specific features will only be available on the specific platform.

So let's start with Part 1 of the tutorial
Step 2 - Modify content
Step 3 - Build native apps