Android – setup, installation, and sample code

Getting a Google Android development platform setup and ready to use was a pleasant surprise.  The first steps were quick, and intuitive.

The right place to start is by downloading a Java runtime. This may not even be necessary if you’ve already been using tools which depend upon Java. A quick test of the JRE is simple:

Java-logo04 <verify Java>

Getting the Java run-time installed, if it’s not already on your platform, is a matter of following the link referenced in the previous URL, <or, click here>.  The above link will also help you to remove older versions of the Java Run-time.  Make sure that you verify the versions which may be needed for other Java dependent software as well.

When you browse thru Sun’s Java site you may feel like there are lots of various options (and there are!) but, you only need a “Java SDK”.  So, you don’t need to head for the Java EE, nor Java Beans, or an Application server – just get the JDK for your platform (Windows, for me). Same goes for the Eclipse IDE links below – you want a “Classic Eclipse” (near the bottom).  I ended up with a quirky behavior (i.e. resulting in a dead-end getting no where) came from the browser (Microsoft’s IE8) which reports a 32bit executable; but, I was pretty sure that I’ have installed a 64 bit version of everything I could (at least, when they existed, I have meant to use 64bit windows binaries). What transpired, was that it’s a problem to install Eclipse into the C:\ProgramFiles folder in Windows – use ANY OTHER directory, but DON’T USE C:\ProgramFiles.   I suggest you put the environment tools into a folder under C:\Android <— this directory will be referenced in my Blog entries, for the Android SDK.

The ADT (Android Developer’s Toolkit) is a Plug-in, to the Eclipse IDE (interactive development environment). 

For downloading Eclipse, the Android SDK, and ADT plug-in to Eclipse, use these links:

Eclipse-logo03 Eclipse-Android-SDK-logo03

<Eclipse Download> <Android SDK Download> <ADK Eclipse Plug-in>

<http://www.eclipse.org/downloads>       <http://developer.android.com/sdk/index.html>

Let’s make this as plain as possible.  Here are the steps for getting started with installing the free Android development tools:

  1. Get a platform (Windows XP, or Windows Vista;  although Android’s SDK works on Linux and Mac OS X as well)
  2. Download Java SE6 (already done, if you have previously used other Java based tools or applications)
  3. Download Eclipse – the tool platform, or Interactive Development Environment (IDE)
  4. Download Android Software Development Kit (SDK) for your platform (again, I’ll use Windows to illustrate, but you can use Mac or Linux too)
  5. Download the Eclipse Android plug-in

These steps listed above will use “open source” software – so, they are free. Each download is available in a matter of minutes from the respective organization; be cautious about downloading these from third party sources unless you know the server’s organization.  The potential for viral software, or spam or other problems is a reality;  just, be careful – be sure you are using web URLs which you can trust.

Then a few more install/configure steps will help verify that things are installed properly and working:

  1. Create a “hello world” Java application, to see the output in the IDE console.
  2. Configure an Android Virtual Target (AVT) to allow simulating a smart phone application running on your desktop.
  3. Create a “hello android” application which runs in the simulator.
  4. Add projects, from the sample code for a platform. (run the Lunar Lander example, on your simulated target “phone”.

 

Begin:

If you have the Java run-time installed (see above link for the URL) then you’ll download the files for the Eclipse tools.

There is no “install script” or a self extracting .EXE file for Eclipse 3.5 – you just copy the file hierarchy from the .ZIP file into an appropriate directory (I use: "C:\Android”). The Android SDK (I’m using R0.4 Dec 2009) was similarly placed into my “C:\Android” folder, but you use “SDK Setup.EXE” in the top level directory after unpacking the .ZIP file.   I am using Windows x586 (on a 32bit Win/7 system). NOTE: Read Systems Requirements  It was after installation that I noticed that Win/7 wasn’t on the officially supported platform list.

 ScreenShot04b AndroidSDK Quick Start

A “quick start” in Adroid’s developer webpages shows an environment variable addition to the PATH for your system – don’t skip this step!

 

 

As mentioned, when you have unzipped the Android SDK, you’ll see a “SDK Setup.exe” in the subfolder (C:\Android\android-sdk-windows, for me).

ScreenShot07a AndroidSDK setup 1

Run the setup executable. It provides both the installation and the automatic updates to the latest version (at this writing, it’s Android 2.1; which is new enough so that most books you might get will still be referencing 1.5 and 1.6 versions of Android’s SDK).  The version reference of 2.1 is for the Android SDK not the Eclipse IDE and Eclipse plug-in for Android.

 

Subsequent download of the Android plug-in for Eclipse was invoked via the “Tools” menu after you start Eclipse (NOTE:  I am using Eclipse v3.5), but the book I purchased has examples given with v3.4 screen shots. Enhancements between version 3.4 and 3.5 are some convenient ease-of-use features, especially in this initial area of downloading a plug-in and adding Android sample applications and creating the first project.  Look around a little, to get accustomed to the version you are using!  (The “ADT Plug-in” version I reference was 0.9.5, Dec 2009 for Android 2.1 SDK).

OPEN QUESTION:   I guess it was ok to create the shortcut manually:   C:\Android\Eclipse\Eclipse.EXE

Of course, you will soon want to write the classic “Hello World”, or even better a Android specific “Hello Android” instance of a program to verify that all this installation stuff has worked properly;  check out: http://developer.android.com/guide/tutorials/hello-world.html to get started.

We should point out that a few “video clips” at ScreenCast.com’s web site can help just by ~seeing~ the way it all works. Starting with tool setup:

The URL given (i.e. https://dl-ssl.google.com/android/eclipse ) for the Android Dev Toolkit (ADT) plug-in for Eclipse wasn’t working for me.  The error was noted in the book I had purchased, but their solution didn’t help (there were components missing at the server, according to my error message)… the real problem was that I had installed the products under C:\ProgramFiles rather than in C:\Android or C:\Java or something similar.  Watch the rest of the videos for more startup assistance.

  • Demo Android Apps

After the Installation of the ADT plug-in… the update for ADT will bring your Android development to the 2.1 version (or, what-ever is latest).

NOTE: if you want to build images to support Android 1.5, 1.6, and 2.0 and 2.1 (or today’s latest versions!)… you need to install each.

 

The equivalent of the “hello world” program that every “C” programmer first learned from Kernigan and Ritchie is to just use the Eclipse IDE to type in a few source lines of Java code, and output a line of text in the console window.

Use the Eclipse build/debug environment:
SELECT “File –> New –> Java Project

Fill in the form which appears with a project name:   helloworld

Select “create new project in workspace” (meaning, we’ll create new source files).

The JRE (Java Runtime Environment) should be selected, as the “latest” which for us was JavaSE-1.6 in the drop down list.

I’ve changed project layout to: “use project folder as root”, but either will work.

The [Finish] button, will create the project.    (optionally, hit [Next] and step thru the next form as well.

 

In the next steps, you’ll place a source file into a folder…  it’s not important that the folder is named “src” but it is the conventional use; it is also not important that the file is named helloworld.java but it is important that the file’s name and the class we name in the main package are the same name.  I’m using “helloworld” for both. If you call the file hello2.java and the class is named helloworld the IDE will nicely remind you that they must be named the same thing. It may be just convention that the source directories are called “src”, but the file names themselves are important to end with the file suffix “.java” so that the IDE will help you find syntax errors while you are typing.

Type in the code:                  (file, named:    src\helloworld.java )

/**
*     Hello World,
*     @author Jim Donovan
*     Feb 22, 2010
*
*/
public class helloworld {
  public static void main(String[] args) {
                 System.out.println(“Hello java world”);
  }
}

When you “run” the code, it’s just from the “Run” menu, select “Run” (you can also use: CTRL F11 ) – select the “Java Application” in the dialog box which appears.

Note, this output will only be displayed on the “Console" Tab in Eclipse (the third tab, near the center on the bottom of your window).

While you are noting the names of these TAB areas in Eclipse, select the “@Javadoc” tab, which shows that the @author tag was used to relate the documentation details about the source file, this will come in handy later with a number of automatically generated documentation directly from the source files.

 

 

To output a full Android version of “Hello World" we’ll create another project.

<NOTE>     from JimD                      When I first installed the “Adroid plug-in” the “File –> New –> Android Project” was not listed.   I can try to describe the issue with getting the plug-in to work – or, just presume that everyone else will figure that out.    </NOTE>

 

SELECT “File –> New –> Android Project –> ” ( appears directly below the “Java Project” menu, from the Eclipse main window.

Fill in the following information, in the form

  • Project name:            helloworld
  • (*) Create new project in workspace
  • Build Target:              Android 2.1        (select from the list)

Properties:

  • Application name:      HelloWorld
  • Package name:          org.example.helloworld
  • Create Activity:           Hello
  • Min SDK Version:        7                      (automatically filled in for you)

[Finish] button, to create the project.

 

Expand the project browser, to navigate down thru “src”, and “org” and “example” (note the relation to this hierarchy based on the package name you entered above.

The source file has already written for you, it provide your application’s confirmation and greeting “Hello world, Hello!”.

Again, the “Run –> Run” menu, or select [CTRL] F11 to run the application.    Although, this time you need to choose “Android Application” to get the output to run on the simulator.   

BECAUSE IT’S THE FIRST TIME, your program doesn’t have a default virtual device – this is remedied easily because the plug-in recognized that there’s no compatible target found.   Select [YES] to the dialog box which asks if you want to create one.  I gave it the name em21 (for Emulator 2.1) and you may wish to come back and create another for version 1.5 or 1.6 when you use some demo code for the earlier versions.  Fill in the other information as follows:

  • Name:     em21
  • Target:    Android 2.1 – API Level 7
  • SDCard:  128M
  • Skin:       default

 

Exploring further:

To modify this first Android application, navigate down to the folders \res\values and double click the “strings.xml” to change either the string “hello” or the “app_name” one.    The files can be edited in the “Resources” mode, or in “strings.xml” source tags mode.

 

 

The target simulator in Windows is worthy of a screen shot – this illustrates how easily an installation, and initial demo can be accomplished.

screenShot18a_targetEmulator screenShot19a_targetEmulator

The small circle icon with a triangle inside it on the Android screen expands to display more apps on the desktop. Those are shown on the right (an Alarm Clock, the Browser, Calculator, Camera, etc). If you select the triangle within the circle icon – it’ll shrink back again, to the original desktop. Running one of these apps is as easy as selecting (with your computer’s mouse, or using the simulator’s arrow keys to navigate the cursor), it will run and takes over the android’s screen.  Other applications are continuing to run, but become background tasks to the one running in the screen.

In my next blog post…   I’ll step thru using some of the sample code (which was installed on your platform when you loaded the Android SDK), as well as just showing the target system simulation.  The target simulator may look like a quite generic handset (even though there are a number of vendors building Android based cell phone already) this simulator is live on the web from the very start; so, try selecting “Browser” and see how quickly you can search for a local movie to go see the latest block buster movie in a theater near your home!   (hint, hint:   just enter “movie 01890” (use your own zipcode!)    You may find that you need to use the “MENU” button, and select “Settings” to change the “Language & Keyboard” to English USA (or, your favorite language) and disable some of the keyboard “text settings” for character sets which you may not recognize.

<summary> of Jim’s Android Development entries, or various technical subjects, and
<other topics>of Jim’s Blog entries (a variety of miscellaneous topics)
Jim2004_05b Jim Donovan lives in the Boston suburbs, and writes blog entries on technical topics in software development tools, software quality, initiatives in green energy and conservation as well as yoga, meditation, and even chronicles of his family history. Profile LINK to his “Microsoft LIVE”, Facebook, or Linked-In profile.

Explore posts in the same categories: Computers and Internet

Leave a comment