Android development on the command line
Although it’s possible to develop for Android using (almost) entirely command-line tools, Google’s documentation isn’t geared for that. So, I thought I’d write up my experiences.
First, install Java. I have Java 1.8:
whiteandnerdy:example ppelleti$ java -version java version "1.8.0_25" Java(TM) SE Runtime Environment (build 1.8.0_25-b17) Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)
Next, download Android Studio. Go ahead and install Android Studio. (On OS X, this involved opening the
.dmg and then dragging Android Studio to Applications.) Then, run Android Studio, because this is necessary to finish the installation. Once the installation is finished, you can quit Android Studio.
Android Studio installed the Android SDK somewhere. On OS X, this is
~/Library/Android/sdk, but on Linux or Windows it would be somewhere different.
You’ll need to set a couple environment variables in your shell. (And you’ll probably want to set them in your
.profile, too.) First, set
ANDROID_HOME to the directory where the SDK is, such as
~/Library/Android/sdk. Then, add
Now, just type “android” to launch the SDK Manager (a GUI program), which will let you download additional API levels. (I’m going to use API level 21 in this example.)
Creating a new project
android create project can be used to create a new project from the command line, like this:
android create project \ --activity ExampleActivity \ --package org.funwithsoftware.example \ --target android-21 \ --path example \ --gradle \ --gradle-version 2.1.3
Anyway, in theory the
example directory should contain a complete, ready-to-build example project. In practice, I ran into a few problems that needed to be fixed.
First, in the file
gradle/wrapper/gradle-wrapper.properties, change the line:
This is where you actually set the Gradle version, and for some reason,
android project create sets a Gradle version that the Android Gradle plugin is not happy with.
build.gradle, and change:
as explained in this Stack Overflow post.
Building and running the project
At this point, cd to the
example directory, and you should be able to type:
which will list all of the possible Gradle tasks available.
To just build your app, type:
However, if you have an Android phone attached, you can do:
which will first do
assemble, and will then install the app on your phone.
At this point, the app should look like:
A more modern look
--target android-21 sets
build.gradle, it doesn’t set
AndroidManifest.xml. To be able to use newer Android APIs, and to get a more updated look, edit
src/main/AndroidManifest.xml and add the line:
right before the
Build and install the app again, and now it has a more modern titlebar:
Instead of running
android project create and then making the changes mentioned above, you can just start with this repository, and run