Starting with Android development

TL;DR

If you cannot find the gradlew file in your Android project directory, you should be able to build your project with the ant debug command. If you want to use Gradle anyway, you probably need to add --gradle --gradle-version 0.10.0 to your android create project command-line.

If you get a build failure with a message such as Could not create plugin of type 'AppPlugin' or Could not find com.android.tools.build:gradle:9, your Android Gradle plugin version is wrong or your Gradle version is wrong, or both. You might try (Android Gradle plugin version, Gradle version) = (0.10.0, 1.12) or (1.3.1, 2.6).

Update 2015-20-08: I found a compatibility matrix for Android and Gradle.

LoveTrouble at first sight

I finally took the time starting looking at Android development. My main motivation is to write two applications to interact with my Pebble: one to forward notifications from my Debian laptop to my Pebble, and the other one to support me while I am commuting. More on those later.

As a novice Android developer, I started to follow Google's Getting Started for Android.

Just some quick background info about me: I am a software engineer working on embedded systems for 9 years now. As such, doing cross-development on a mainstream ecosystem like Android on ARM did not look scary, and I was expecting to run my 'Hello world' app in 20 minutes or so.

It turns out it is not scary, but that the documentation is wrong (dogfood anyone?). I ended up spending a couple of hours downloading half the internet and messing around with the official build system. I wrote this blog post hoping it could helpful to someone else.

The default build tool is Ant, not Gradle...

When creating the project using this command:

android create project --target <target-id> --name MyFirstApp \
--path <path-to-workspace>/MyFirstApp --activity MyActivity \
--package com.example.myfirstapp

You cannot build the example app using ./gradlew assembleDebug. And this is expected, because by default you should be using Ant with ant debug instead! But what if you want to use Gradle (after all it is claimed everywhere in the Android docs that it is the new cool build system!)? In that case you need to specify that you want to use Gradle on creation:

android create project --target <target-id> --name MyFirstApp \
--path <path-to-workspace>/MyFirstApp --activity MyActivity \
--package com.example.myfirstapp \
--gradle --gradle-version 0.10.0

And yes, you need to specify a --gradle-version...

(Fun fact: despite the confusing name, this is not the version of Gradle, but the version of the Android Gradle plugin...)

The tricky part is to have both the Gradle version (the real one, the version of Gradle itself, not the --gradle-version one) and the Android Gradle plugin version compatible with each others. If it is not the case, you will end up with an error like this one:

$ ./gradlew assembleDebug

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'MyFirstApp'.
> Could not resolve all dependencies for configuration ':classpath'.
   > Could not find com.android.tools.build:gradle:9.
     Required by:
         :MyFirstApp:unspecified

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 15.169 secs

I also saw errors like Could not create plugin of type 'AppPlugin'.

You already know the Android Gradle plugin version you requested. The Gradle version you are using can be found in the file gradle/wrapper/gradle-wrapper.properties in your project directory.

As of this writing, the default Gradle version used by the Android toolchain seems to be 1.12. The Android Gradle plugin version 0.10.0 seems to be compatible with Gradle 1.12 so far. (I finally found a working version by looking into the plugin repository and trying various options.) I was also successful using Android Gradle plugin 1.3.1 and Gradle 2.6, as long as I do not use Proguard.

As a side-note, I could not refrain to note that building a simple 'Hello world' from scratch took more than 10 secs, whereas it took something like 3 secs with Ant. But Hooray! \o/ I can now resume my reading of the Getting Started.