Developing Android apps using Netbeans

by Stii

I prefer Netbeans to Eclipse. I know very little Java, but I’ve kind of grown used to Netbeans while exploring Groovy and Grails. Eclipse is the de facto standard for developing Android apps, although there are a Netbeans plugin. Here is how I got my Netbeans 6.5 instance to work with Android.

First of all, you need to install the Netbeans Android plugin called nbandroid. In Netbeans, go to Tools > Plugins > Settings and click on the Add button and add the plugin update URL: http://kenai.com/projects/nbandroid/pages/Install. Install the Android plugin.

You need to download and extract the Android SDK. The latest version can be found on the Android Development site. Extract the zip file.

The file structure for the new SDK has changed and Netbeans won’t work with the new SDK as is. You have to do the following:

~$ cd path/to/sdk
~/path/to/sdk$ cp -R platform/android-1.5/* ./

Start a new Android project in Netbeans and in the new project window, click the Manage Platforms button.

netbeans manage platforms

Select the “Google Android Open Handheld Platform” option and click the Next button.

netbeans select platform type

Navigate to the path where your SDK is (the path to the prepared one as per the above mentioned steps). Click the finish button and it should be good to go.

netbeans select android sdk

Before you start hammering away at your newly created Android project, you need to set up an emulator to test your application, else you would not be able to test anything.

You need the API target id in order to create an emulator. Do it as follows:

~/path/to/sdk$ android list targets
Available Android targets:
id: 1
     Name: Android 1.1
     Type: Platform
     API level: 2
     Skins: HVGA (default), HVGA-L, HVGA-P, QVGA-L, QVGA-P
id: 2
     Name: Android 1.5
     Type: Platform
     API level: 3
     Skins: HVGA (default), HVGA-L, HVGA-P, QVGA-L, QVGA-P
id: 3
     Name: Google APIs
     Type: Add-On
     Vendor: Google Inc.
     Description: Android + Google APIs
     Based on Android 1.5 (API level 3)
     Libraries:
      * com.google.android.maps (maps.jar)
          API for Google Maps
     Skins: HVGA (default), HVGA-L, QVGA-P, HVGA-P, QVGA-L

Say you want to test using the Android 1.5 API, you need to specify the target as 2 (see? id: 2) With this information in your arsenal, type the following:

~/path/to/sdk$ android create avd -n avd_1.5_1 -t 2

The -t 2 section of the command is the target id as per the previous command.

The last step is to add the following to your project’s build.xml file.

<target name="-pre-init">
        <property name="emulator.options" value="-avd avd_1.5_1"/>
</target>

I.e. your build.xml file should look something like this (without the … truncated … part):

<?xml version="1.0" encoding="UTF-8"?>
<project name="AndroidApplication2" default="default" basedir=".">
    ... truncated ...
    <target name="-pre-init">
        <property name="emulator.options" value="-avd avd_1.5_1"/>
    </target>
</project>

That’s it! Now make it do something simple and run your application. It should open an Emulator window and run your application. Nifty!

android emulator

Here are the sources that solved my problems:
http://wiki.netbeans.org/IntroAndroidDevNetBeans
http://kenai.com/…