Developing Android aplications using NetBeans
Contents |
[edit] Introduction
There is an nbandroid plugin, but at the time of writing this guide (2008), it was still in development. Furthermore, this plugin is not needed in development of Android applications using the NetBeans IDE. This tutorial is meant to show you how it can be done without the plugin. Just as in the tutorial on how to use Android SDK from console, you'll be shown how to set up the LunarLander sample in NetBeans, how to develop it and build it.
There's an introduction on how to install Android into NetBeans at Kenai.com:NetBeans Android. Basically there is info that can supersede the info here on setting up the environment. However, it doesn't mention setting up the emulator for running your applications.
[edit] Creating a new Android project
As it was described in the mentioned tutorial, a new Android project should be created using the console tool activitycreator. In our case, the files from the LunarLander should be also copied to the new project's directory (it was also described in the linked tutorial).
AFAIK, there is nothing like Console plugin for NetBeans, so all of this stuff have to be done from a system console.
[edit] Setting up a NetBeans project
Instead of using the nbandroid plugin, a free-form Java project might be used in NetBeans. A free-form Java project is a project, which uses its own Ant script (a script not generated by the NetBeans IDE). Creating such a project is quite simple. First, the proper project type should be chosen from the new project dialog. The free-form project can be found in the Java category.
The next step is setting the location of the project's directory, the project's Ant script, the project's name, etc.. (Tip: try to set the project's location first. NetBeans should be able to automatically set up all the remaining fields in the dialog.)
The next dialog allows to set up the Ant script's targets, which will be used when the user invoke one of the NetBeans typical commands (like build or clean project).
It must be said that the NetBeans way to work with a Java project is different from the Android's way. The Ant script generated by the activitycreator program doesn't have targets for cleaning or generating Javadocs. Although there are targets for running the project, they don't work as intended (at least I had some problems with them). Of course, user can set any target he/she want to, but in this tutorial only the "Build Project" target will be set (to the "release").
After setting the Ant script targets to use by NetBeans, it's time to set the project's source directories. Since the LunarLander project is quite simple, only one source directory is used (src in the project's directory). It can be added using the "Add Folder" button.
I'm not sure if this is needed.
The next thing to do is setting the project's classpath. Since this is an Android project, the Android API jar should be added (using the "Add JAR/Folder" button). The jar file can be found in the directory where the Android SDK was installed.
Finally, the output directory for the project should be set. In Android projects, the bin directory is used for this purpose. It can be added to the NetBeans project with the "Add JAR/Folder" button.
When all the steps described above are completed, a new Java project is opened in the NetBeans IDE.
It's not the end, though. The project is open, but it doesn't work as intended:
There are many bugs, because the package R doesn't exist. The package can be generated using the Ant script generated by the activitycreator tool (the target name is "resource-src"). It could be achieved by simply running that target (from the right-click menu on the build.xml file in the "Projects" tab), but it might be better to set up the NetBeans project properly and add this functionality to the project's context menu.
First, the project.xml file from the nbproject directory (created by NetBeans) should be opened.
There are three things to do. The proper IDE action should be added, and the proper Ant script target should be assigned to this action. The IDE actions are described in the <ide-actions> node. As can be seen on the following screenshot, adding a new IDE action is very easy and needs no further explaination.
The last thing to do is adding the new IDE action to the project's context menu. The proper entry in the project's xml file should be found (it's called "context-menu"), and a new element should be created.
After the modifications made are saved, a new option will be available in the project's context menu. This action might be used to generate the R.java file.
[edit] The final word
Of course, more actions can be added this way (e.g. an action for the "compile" target). It depends only on developer's habits, which of them are needed/useful, and which ones aren't. Anyway, using the NetBeans IDE to develop Android applications is easier when anyone can tune the functionality to his/her own needs.
There are also some topics that haven't been mentioned here. The most important ones seem to be running the Android emulator and installing/uninstalling packages to/from the emulator. I hope that this tutorial will be improved in the future and these topic will be covered.










