If you have an Android phone, or even if you haven’t, getting set up for Android development is a rewarding experience and can teach you lots about Java and is an excellent choice for getting your teeth into learning how to program. One of the biggest frustrations for those new to programming is that whilst you get to learn lots of new concepts and learn to apply logic to problems, the display of your hard work is restricted by your ability to generate graphics using your code. Android provides a lot of framework to allow you to more easily express yourself with many of the more complicated graphics taken care of on your behalf.
By following the instructions here, you can get yourself up and running quickly and start making software you can take with you on your phone and show to others. These instructions are aimed primarily at Windows and Macintosh, but you can equally develop for Android on Linux as well. Personally, I own a Macintosh, so the screenshots will be based on that, but the screens are almost identical on other platforms.
Installing the Java Software Development Kit (SDK)
The first step in developing for Android is installing a Java SDK. Java is the language you will be using to develop for Android. It is an impressive language as it is full-featured, can work on many different platforms and it is easy to use once you get the hang of the basics. Most computers come with Java pre-installed, but unfortunately that version is almost certainly only for running programs that other people have written. If you want to develop your own Java programs, you must have the Software Development Kit installed. It’s easy to download and install: just use Google to search for “Java SDK” and you will find that the first result leads you to a website similar to the screenshot below.
Choose the Java Platform (JDK) button (highlighted above) which is currently version 7u10 as shown in the screenshot. A list of downloads will be displayed, as shown below.
You must first click “Accept License Agreement” which activates the links below. You can then proceed to select the download which is correct for your computer’s operating system. If you run Windows 7, you can check whether you Windows version is 32-bit or 64-bit by right clicking My Computer and choosing Properties. The version marked x86 in the download list is 32-bit and the version marked x64 is 64-bit. Macintosh is only available as 64-bit. For Windows, you simply run the .exe file you have downloaded, and for Macintosh, you open the .dmg file you have downloaded and run the installer package contained within.
Installing the Android Development Toolkit (ADT)
So, you’ve got the Java SDK installed. Now it’s time to get hold of the Android Development Toolkit. This used to be a laborious chore of downloading an editor, the Android SDK, a virtual device ROM and all sorts of other technical things, but Google, the company behind Android, have obviously realised that this was too much so now they give you everything pre-set up in a single package. It is a big package, but you can get it by going to http://developer.android.com and clicking Develop, then Tools, then Download. You will be faced with a screen similar to that shown below.
In my case, I am viewing this from a Mac, so that is the package I am offered, but you will be offered the one appropriate to your operating system. The download for me was nearly 400 MB so be prepared for a long wait, but once downloaded you should have a .zip file with everything else you’ll need to develop for Android. Unzip the file and you will have a folder with the software inside. My folder was called “adt-bundle-mac-x86-64” but yours may be different. This doesn’t need installing in the traditional sense, only placing somewhere smart. I would recommend you put it in your home folder. On a Windows computer, you can get to this folder by going to My Computer, double clicking the C: drive (or the drive with Windows installed on it), double clicking on Users and then double clicking on your login for the computer. On a Macintosh, open any Finder window and push ⇧⌘H (Shift + Command + H). This is a location that is exclusive to your user on the computer and that you can both read and write to.
The Eclipse software is where you do all your development wizardry and you can access this by placing a shortcut on your desktop. Navigate to the new folder you just extracted from the ADT bundle above and open the folder named eclipse. In Windows, drag the file named eclipse.exe to the desktop using the right mouse button and choose to Create a Shortcut. On Macintosh, drag the Eclipse.app icon to the dock and it will remain there for quick access. When you first run Eclipse, it will ask you to set a workspace directory. It is safe to accept the default and this will be where Eclipse keeps all of its settings relating to the projects you create.
As this is your first time using Eclipse, you will be presented with a welcome screen. If you close the tab for the welcome screen, the real interface of ADT Eclipse will be shown and should look similar to the screenshot below.
Setting some Preferences in Eclipse
When you work on source code, especially with others, it is wise to ensure that your settings are the same as each other for things like indenting and line endings. I won’t go into the technical side of this, but you can indent lines of code with either tabs or spaces, but you should never mix the two as this can leave the code looking hideous in other editors. I suggest you always use spaces for indenting and we can tell Eclipse to do this for us, amongst some other things, so follow these instructions to have Eclipse ready to work best for you.
Open the preferences for Eclipse. On Windows, this means clicking the Window menu and then Preferences which is near the bottom of the menu. On a Macintosh, the preferences are under the ADT menu or can be accessed by pressing ⌘, (Command + Comma). Edit each of the preferences as listed below.
- General > Editors > Text Editors: Tick “Insert spaces for tabs” and “Show line numbers”
- General > Workspace: Tick “Refresh using native hooks or polling” and “Refresh on access”. Also choose “Other:” for the Text file encoding and select “UTF-8” from the drop down options.
- Java > Code Style > Formatter: Click “New…” and give your own name as the “Profile Name:”. Click OK after entering your name and select “Spaces only” as the Tab policy. Also tick “Statements within ‘switch’ body” before clicking Apply and then OK. Click Apply and OK again to finish setting up your preferences.
Making a virtual phone and installing your first Application
So, you’re almost there and ready to start building your first apps. First, though, you either need to set up your own phone for development or create a virtual phone on your computer using the tools you’ve downloaded.
To configure your own Android phone, go to the Settings application on the phone, choose the Developer settings section and enable the option for USB debugging, then connect your phone to your computer. However, be aware that if you are not running at least Android 4.0 on your phone, you might be better off with a virtual device for these tutorials which do not focus on older Android versions.
To use a virtual device instead, click the Window menu in Eclipse and choose Android Virtual Device Manager. You will be presented with a screen on which you can choose the “New…” button to build a new device. You should then get a screen similar to that shown below, which I have pre-filled with the options you should select.
If you have one set up and you wish to use it, you could set the back camera to use your webcam instead. Once you have completed the details as shown, click OK to create the new virtual device. Select the new device from the list of devices and press “Start…” followed by the “Launch” button. After a short wait, you will see an Android phone booting up in its own window on your computer. For now, you can just leave this running.
To check that everything is set up correctly, we can put together a very simple application. All this is going to do is display a screen that says “Hello World” and nothing more for now.
To start this project for yourself, in Eclipse click File > New > Android Application Project. You will see a window similar to that shown below.
For the Application Name, put “Hello World”. The Project Name will be completed for you (notice how the space is removed). Under Package Name, you should put something unique that no-one else would use and that describes this project. Traditionally, people use their domain name in reverse, with the name of the application at the end and so I have used com.alychidesigns.helloworld. Keep clicking Next until you reach the last screen where the Next button is greyed out. Here you can name your activity. An activity is effectively a screen of your application so we will call this first activity “Home” as this is the only screen. The Layout Name will update automatically to say activity_home and this is fine so you can click the Finish button.
The Activity designer will appear with your first activity. I’ve cheated you a little here because the default activity already says “Hello world!” on it, but I promise we will do more fun things in the future. For now, we are just establishing that everything is installed and working. To check that everything is as it should be, you can choose to run the application on your phone or in the virtual device. Choose the Run menu in Eclipse and select the first entry “Run”. After some fiddling about by Eclipse, you should find that your very first Android application will load on your phone or in the virtual device and you will see the screen saying “Hello world!” as shown below.
So, in this tutorial you’ve installed all the needed components to begin Android development, you’ve set up your working environment including a virtual device and you’ve created and installed your first application called “Hello World”. Next tutorial will take a look at some of the source code and xml which produce the application and will put something a little more interactive together.