Saturday, 13 July 2013

Building Android projects with Gradle

I notice there is a lot of interest at the moment in the new Android Studio development environment, launched at the recent Google IO event. I have often found Eclipse to be often slow and lacking in robustness, so an alternative IDE being supported for Android development is welcome news. Upon reading about the changes in more detail, another significant piece of news is the new build system for Android projects, based around Gradle. So, I decided to take a look at Gradle in more detail.

Here are some intended benefits for the use of Gradle for Android projects:
- the use of the same build system whether in the IDE or the command line
- the benefits of improved dependency management
- easier integration into automated build systems

There is an additional advantage thanks to the new packaging format for Android libraries, the "aar" format. Previously, android libraries have usually been imported into the IDE so that their resources could be compiled into the app. But now Android libraries can be included much more easily, in the same way that "jar" files have been included in Java projects for years. This particular feature is long overdue but very welcome indeed.

Here is a gradle build script from one of my projects.
buildscript { //define the path to some pre-existing JAR libraries //required by the project LIBS_DIR = "../../../libs" //the android plugin for gradle should be obtained from the maven central repository repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.4.2' } } //declare the project is an android build apply plugin: 'android' dependencies { //also use the local maven repository to //look for dependencies repositories { mavenLocal() } //these are some jar files that are required by the app compile files("${LIBS_DIR}/hiscore/hiscore.jar") compile files("${LIBS_DIR}/GoogleAnalytics/libGoogleAnalytics.jar") //this is an android library (using the "aar") format which I have put in my local maven repository compile ('com.mopub.mobileads:mopub-android-sdk:unknown') } //the project definitions for the android build android { compileSdkVersion 15 buildToolsVersion "17.0.0" //The source paths below are not the new recommended project structure //I am still using the default Eclipse-like structure sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = ['src'] resources.srcDirs = ['src'] aidl.srcDirs = ['src'] renderscript.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] } instrumentTest.setRoot('tests') } //declare the details for creating a signed release version signingConfigs { release { storeFile file("../keys/android.keystore") storePassword "######" keyAlias "######" keyPassword "######" } } //declare the release build should run proguard and perform signing buildTypes { release { runProguard true proguardFile getDefaultProguardFile('proguard-android.txt') proguardFile 'proguard.cfg' signingConfig signingConfigs.release } } }
Build the app from the command line can be done by running one of these commands:
gradle assembleDebug #for debug build gradle assembleRelease #for release build
I have previously used Maven for some projects, and found it very useful for managing project configuration, especially dependencies. But I also found it lacked flexibility for those cases where you need to customize your build for a special case. While in theory you could write your own Maven plugin, in practise most users won't do this and instead rely only on existing well-supported plugins. I often found myself using Ant rather than Maven, because it provides increased flexibility where you need to do some specific operation in a project, such as copy or modify a source file in some way.

Gradle appears to offer the best of both worlds: comprehensive support for a wide variety of common use cases via plugins, the same dependency support as Maven, but also the flexibility to allow custom modifications to your build script to suit whatever is the need.

I am convinced the Gradle is the way to go for Android builds. There are plenty of other cool features too, such as build "flavors", that I haven't even mentioned. I think this adoption of Gradle by Google is a hugely important step forward for Android. Now, I just need to convert over all my other projects to use Gradle too...

Friday, 7 June 2013

Dart numeric types

As previously mentioned, I started to take a look at the Dart programming language.

When I first started to play around with Dart, I fairly quickly came across a strange but seemingly trivial number type conversion problem. It can be expressed easily by this code:

  int a = 60;   int b = 5;   int c = a/b;
This produces an error at runtime:
  type 'double' is not a subtype of type 'int' of 'c'.
After some investigation it appears to be the "fault" of Javascript, in which there are only floating point numbers and not integers. This presents a problem for Dart as all code is supposed to be exportable to Javascript.

But still, might you not expect any language to cope with simple integer arithmetic like this? It's as though division simply cannot be done on integers! The solution is to either use the (a/b).toInt() function to force conversion to integer, or to change the variable types from 'int' to 'num'.

Tuesday, 21 May 2013

HTML5 programming with Dart

Another programming language


Dart is web programming language from Google designed to address some of Javascript's perceived flaws. While Javascript has its supporters, there are many who find developing with it irritating and difficult, especially for large-scale or complex projects.

Dart attempts to address these shortcomings by providing many features that are associated with the more fully-featured object-oriented languages such as Java, C#, etc. These features include classes, interfaces, generics and optional typing.

With this in mind, I decided to take a look at Dart with a view to creating a simple HTML5 canvas game, in order to try out some of the features. This means having a html canvas element as shown, with the game mechanics and UI written in Dart and then compiled into Javascript.
    <canvas id="gameCanvas" width="400" height="464">     </canvas>
Setting up your system for Dart development is straightforward, and is made easier still by the existence of the Dart Editor, which is essentially a stripped-down version of Eclipse.

Dart First Impressions


Well, the language initially looks very similar to Javascript. Much of the syntax, including for declaring variables and functions, is the same. But, if you have a knowledge of other languages such as Java, the additional features such as classes suddenly make it much easier to plan and develop your project. The notation is familiar:
class MovingObject {   ImageElement img;   int x;   int y;   int dx;   int dy;   MovingObject(this.img, this.x, this.y); }
Another nice feature is that object types can be declared, and this information can be used the editor to provide "content assist" or compile-time warnings, etc. The types are optional so if you need to do rapid initial development using Javascript-like "var"s  in your code, you still can.

The Dart Editor provides Content Assist features

The intention is that Dart can be exported to Javascript so that compatibility with the current crop of web browsers will be maintained. However, this is where I found the most problems. Even when my app would run perfectly as a native Dart app, on several occasions the exported Dart app would not function correctly. As might be expected, I got the best results when using Google Chrome. But if the intention is to compile to Javascript that works on any browser, then my experience suggests there is a way to go yet.

My first project was to create a simple canvas-based two-player strategy game called Gomoku, which you can try out. Note due to the Javascript compilation problems mentioned above, it is currently only working in Google Chrome.

In summary, after a first look, I think Dart has a lot of potential. It's unlikely that the native Dart VM will make it to all the browsers, so it's not going to be a replacement for Javascript. But if the compilation problems can be sorted out, I can imagine it being a popular choice of source language for developing both HTML5 canvas and web-form based applications.

More Dart-related stuff to come shortly...