Running Android app on device using Gradle

Here I will show you, how I managed to build and run an Android app via command line (Terminal, since I use Mac) using Gradle and how I start a resulting app on the device (Google Nexus 5 with Android 4.4.2) automatically. I had to solve two major problems to achieve this.

The first problem is with Gradle version: last version of Gradle is not compatible with the last version of Android Plugin.
The second problem is with custom task to run app on device: you have to put the correct package and activity names.

The final result looks like this:

Where Multiverse is the root folder of my project and the name of the project, appStart – custom Gradle task that builds and runs an app on device.
Gradle loads all dependencies, builds app, makes a lint check and starts and app on your device automatically. Yahoo! This is how it looks on Terminal:

However, I should notice, that this works slower than just running via Eclipse: as you see it takes at least 13 seconds for a little project on Macbook Pro Retina Display Late 2012.

First of all you need to install a correct version of Gradle. If you just download the latest version, your Android plugin will not work. You have to get a version, for instance, 1.10, not 2.0. Also you need to setup PATH variable, so that you will have bin folder of Gradle and android sdks tools there:

Then I created a gradle build file:

Here com.alwawee.multiverse is the package name of the application.
And com.alwawee.main.MainActivity is the full Activity name, that the application starts from. This is how my project tree looks like:
Снимок экрана 2014-07-28 в 1.51.29

This is a part of the Manifest file:

At the end there is a custom task appStart. It is important to correctly write the package name before class name. I thought that the package name is the package name for MainActivity class. I was wrong. It’s a name for a whole app’s package name. You can find it at the root node of your Manifest file.

Notes on commented code: use abortOnError false if you have any serious problems with lint and don’t want to solve them. Uncomment the line about Windows, if you use Windows.

This is a StackOverflow question, I asked to reach this goal.

One thought on “Running Android app on device using Gradle”

Leave a Reply

Your email address will not be published. Required fields are marked *