Monday, September 1, 2014

Starting From Scratch: Android - Creating Your Project

This week I'd like to start a new series called Starting From Scratch. The idea behind the Starting From Scratch series is to give you real world insight into a specific topic. According to PC World, Android currently makes up 84% of the overall smartphone market. So I thought a good place to start the Starting From Scratch series would be with Android.

If you've been following this blog for a while, you know that I'm not a fan of the traditional IDE. Given that, I won't be using Eclipse for this series. I'm going to walk you through creating a project from the command line.

Installing the Android SDK Tools

The first thing you need in getting started with Android development is to download the SDK. Head over to the Android developer site and download the SDK tools (you don't need the ADT plugin for this series). The second thing you need to do extract the SDK tools and, for convenience, add them to your path.

On *nix, for the latest version to date, this can be accomplished by running:
$ cd ~/
$ curl http://dl.google.com/android/android-sdk_r23.0.2-linux.tgz | tar -xzvf -
$ echo 'export PATH="$PATH:~/android-sdk-linux/tools:android-sdk-linux/platform-tools"' >> ~/.bash_profile
$ . ~/.bash_profile
Installing the Android SDK

Installing the SDK tools doesn't actually install any of the SDKs. The next thing we need to do is install one or more of the Android SDK versions. Which SDK versions you install depends on your applications target audience. You should take a look at the Android Platform Versions website to see what versions you should install. For this tutorial we're going to install the platform tools and Android 4.4.2.
$ android update sdk --no-ui -t tool,platform-tool,19
Creating a project

The android tool that comes with the Android SDK allows you to create projects from the command line. There are several things that you need to provide the tool when creating the project. These are:

  • Android version
  • Package name
  • Path of the project
  • Name of the Application
  • Name of the main Activity

The Android version is the name of the Android version you want to target. You can get a list of all installed Android versions by running $ android list target.

The package name is the namespace to use within your project. This is typically your domain in reverse. For instance if your domain was www.foo.com your package name would be com.foo.

The path is the directory where the project files should be created under.

The name of the application is, well, the name of the application.

The name of the main Activity is the name of the Activity that will be launched when a user opens the application. I'll get into what an Activity is later in this series.
$ cd ~/
$ mkdir MyFirstAndroidApp
$ cd MyFirstAndroidApp
$ android create project -t android-19 -k com.example.myfirstandroidapp -p . -n MyFirstAndroidApp -a MyFirstAndroidActivity

Building The App

At this point you actually have an app that can be built and installed on a phone. You should follow these instructions and put your device in developer mode. Once you have your device ready you can plug it in and run:
$ ant debug && ant installd
The default Android command line build system is built on top of Apache Ant. Ant uses a build file which tells it how to to build a project. When you installed the Android SDK tools you got several Android specific Ant targets which have been created to allow compilation, signing, and other interactions with your application build artifacts. When you created the project using the android binary a build.xml file was created for you in your project directory. This is the Ant build file.

Running the command ant debug tells the Android build system to compile the application for debug. The ant installd command tells Ant to install the debug Android package (APK) on the connected device.

No comments:

Post a Comment