Saturday, January 2, 2010

Creating Android Sample Application

Recently, Android 2.0 had some new sample projects added. They are available as a .zip file from the Android developers site. However, if you unzip and import a project, something wrong happens. The R.java is not generated. The instructions for importing these sample project are as follows from the Android developer site:

You can easily create new Android projects with these samples, modify them if you'd like, then run them on an emulator or device. For example, to create a project for the API Demos app from Eclipse, start a new Android Project, select "Create project from existing source", then select ApiDemos in the samples/ directory. To create the API Demos project using the android tool, execute:

android update project -s -n API Demos -t -p /samples/ApiDemos/

The given instructions give 2 possible actions. The first, as indicated previously, does not work. The second action uses the 'android' executable which is part of the SDK. The options given do the following:


update project : updates an existing android project
-s : only errors produce output (silent mode)
-n : name of new android device (in this case it is 'API Demos'
-t : target id of new android device
-p : Location path of the directory where the new AVD will be created

If you run the above 'android' command-line, you will get the following result:

Updated default.properties
Updated local.properties
File build.xml is too old and needs to be updated.
Added file /home/me/Projects/A20/samples/ApiDemos/build.xml
Updated default.properties
Updated local.properties
File build.xml is too old and needs to be updated.
Added file /home/me/Projects/A20/samples/ApiDemos/tests/build.xml

So, neither of the above procedures really lets you create one of the sample projects for use. The entry will show you the right way to do this for the new ApiDemos sample.

1. The ApiDemos project is an Android 1.6 project. So, in general, make sure before using any of the sample projects that you have the right SDK version installed.

2. Start Eclipse.

3. File->New->Project...->Android->Android Project

4. click on the 'next' button

5. In the next window that appears, select the radio button 'Create Project from existing source'

6. Click on the 'Browse...' button next to the 'Location' text box.

7. Browse to the directory that contains the ApiDemos directory from the latest_samples.zip file.
The 'Location' text box should have a path like /path/to/latest/ApiDemos

8. Select OK

9. In the 'Build Target' area, select the 'Android 1.6' build target.

10. Select the radio button 'Create Project from Existing Sample'

11. In the 'Samples' drop-down, select 'ApiDemos'

12. Click on the 'Finsh' button

13. If this is succesful, the ApiDemos project will build with no errors and the R.java files will be generated automagically.

The above method should be used for the other samples, too. If you do not, the R.java file will not be generated.

No comments: