In recent articles we discussed the difference between web apps and native apps, and went through the steps required to set up your computer for Andcroid app development. In this article we will show you how to get the Eclipse IDE to run a sample app using the Android Emulator.
We have already discussed the process of installing the Android SDK on your computer, as well as the Eclipse IDE and the ADT Plugin for Eclipse, which allows you to work with the Android SDK. We’ve also already walked you through the process of using the AVD Manager to download and install the various components used by the Android SDK, including previous version releases, sample apps and documentation. We will now move on to actually trying out Eclipse with an Android app, and will check out the Android emulator.
Potential Android SDK Roadblock: Pathname
If you’ve gotten this far and tried to run some of the sample applications, you may have noticed a few error messages, along with the fact that the Android emulator most definitely did NOT run. It may look very similar to this:
invalid command-line parameter: Files\Android\android-sdk\tools/emulator-arm.exe.
The error messages refer to the fact that Eclipse couldn’t find the Android SDK based on the pathname you entered when you installed Eclipse. If you’re on a Windows computer, that’s because the path to Program Files includes a space, and Eclipse (and the Java VM that runs it) can not use a path with a space. Fortunately Windows creates a shortened pathname for cases like that, and you just need to find that pathname.
An easy way to find it is to open up a command prompt, change to your root directory, and enter the following command:
dir /x
That will produce a directory listing that will include the Program Files directory, and the part that you are looking for will appear similar to this:
07/28/2011 04:34 PM <DIR> PROGRA~1 Program Files
The problem is, you can’t easily cut and paste from the command prompt window, so I just channel the output into a text file, which I can then cut and paste from. I do that using this command:
dir /x > shortcuts.txt
The part of the output that you will actually need is “PROGRA~1”. If you look at the pathname shown in Eclipse for the Android SDK, it might look like this:
C:\Program Files\Android\android-sdk
For it to work, it needs to look like this:
C:\PROGRA~1\Android\android-sdk
Running a Sample Application in the Android Emulator
Being able to run one of the included sample applications in the Android Emulator that comes with the Android SDK is a good test of your installation. If it works, you’re all set to begin creating apps. If it doesn’t work, well–back to the drawing board. I consider myself to be a computer savvy person, and I have set up development environments before, but it literally took me two days to get it to work properly. The initial problem was the pathname issue mentioned above. The second issue was not adding the proper packages (.jar files) to the sample app to get it to run (or adding them, but not refreshing the app before I tried to run the emulator).
To begin, you create a New Android Project from the Eclipse File menu. In the Contents area, you will need to select Create project from existing sample, and then select a sample from the dropdown list. Any sample app will do, and there are quite a few to choose from. One you have selected a sample app, you will need to choose a build target, that is, select which version or versions of the Android OS that you will be creating the app for.
Assuming you have worked out all the previous issues we have discussed that might stop the emulator from running, it should open a new window that will show you that the code is being compiled, and when the process has been completed, you will see a black window open. After a few anxious moments, you will see the familiar Android logo animation, and eventually (after waiting 3 – 5 minutes) you will see the Android interface appear, along with the sample application you are working with (I selected an example from PhoneGap, which is a discussion for another time).
You are able to close the app, open the Android browser, view websites within the browser, and run other core applications within the emulator, including the Settings app, which even shows the version info:
Conclusion
We’ve now shown you how to set up your computer for Android development, walked you through the Android SDK installation procedure, shown you how to use the SDK with the Eclipse IDE and AVD Manager, and hopefully you now know how to solve some potential problems you may run into while trying to get Eclipse and the Android emulator working. Lastly we showed you how to open and run one of the sample applications using the emulator.