How to Load and Run a Sample Program from the Android SDK
The Android Software Development Kit (SDK) gets updated with new version platforms for each new release of the Operating System (OS). Each version platform can be loaded onto your development machine with SDK Manager.exe. There are available sample Applications (Apps) to showcase Android features and provide example code that allows developers to see how to use the Android Application Programming Interface (API).
Getting Hold of the Android Samples
Each SDK platform version provides Android sample source code project, however, they must be selected for installation on your development machine using the SDK Manager program. Run SDK Manager and wait for it to finish doing it’s checks. Expand the required Android Version folder to see and select the Samples for SDK option.
Once the samples have been installed they will be found in the android-sdk/samples folder in the Android SDK’s install directory. (On Windows this may be under the Local Settings/Application Data folder, e.g. C:\Documents and Settings\John Doe\Local Settings\Application Data\Android, or wherever you chose to install it.) There will be a folder for each Android API version for which you chose to install the samples, called android-x where x is the API version.
See our article Use Android SDK Samples for Guidance to see a list, and brief description, of the example Apps supplied with the various Android versions.
The samples can be run on an Android Virtual Device (AVD) or physical device by creating a new Android project in Eclipse and selecting the sample to load. Use the Eclipse File then New menu option to select Project.
In the New Project dialog under the Android option select Android Sample Project and press the Next button.
Choose the Android SDK platform version to see the samples available (you will need to choose Android 2.1 or later). Press the Next button again.
Finally select the required sample to use to create the project to run, and then press the Finish button.
Eclipse will create a new project in the Workspace using the sample’s name as the project name. Then the sample’s source files are copied into the Workspace and added to the project. The project is then compiled and will be ready to run on an AVD or physical device. Whilst the project is being prepared for execution a message will be displayed in the Eclipse status bar.
Run the new project to see the sample in action (ensure you have a physical device plugged into the development machine or have an AVD configured, see Set Up an Android Virtual Device for App Testing). The Eclipse Run As dialog is likely to display when the sample is first executed. Select Android Application and then the OK button (subsequent executions will not bring up this dialog).
Have a play with the example Android programs to get an understanding on what can be coded using Eclipse and Java for Android App development. The good thing about the Android OS in general is that there are plenty of Android sample projects with source code available, allowing you to drill down into the Java and XML to see to achieve the desired functionality.
- It is possible to add your own projects to the existing samples, simply export the required code to a new directory in the samples location.
- If the samples have been moved from their original install directory the Android Sample Project option on the New Project dialog cannot be used. Instead use Android Project from Existing Code.
See Move Android Code Between PCs Running Eclipse for details on the above points.