Sunday 22 May 2011

Setting up Netbeans for Android Development

I am currently running 10.04 on my main system, and the version of Netbeans in the repository is version 6.9 - so in order to get going with version 7, you need to download the shell script from the Netbeans website. I opted for version 7 as there was a problem in that the nbandroid plugin (discussed below) - it had some other dependencies that 6.9 didn't seem to meet, and figured that it would just be easier to install version 7 then to resolve all those dependencies.

chmod +x netbeans-7.0-ml-linux.sh
./netbeans-7.0-ml-linux.sh

This will launch a graphical installer - there is nothing too complicated about this, just follow the steps.

You also need to grab a copy of the Android SDK. This can be downloaded from the SDK download page: http://developer.android.com/sdk/index.html and is just an archive file (tgz) - I simply unpacked it directly into my home folder. This doesn't have any of the platforms installed ready to go, so you have to launch the SDK and AVD Manager and download at least one Android paltform - suitable to your device or what you want to develop for. This is launched by the android executable in the tools folder.

cd android-sdk-linux_x86/tools
./android

I think it's pretty straight forward, but for the Android documentation, see: http://developer.android.com/sdk/installing.html.

Once you have downloaded a platform, you need to create and Android Virtual Device (AVD) for the IDE to use when developing. This is done by going to Virtual Devices tab in the Android SDK and AVD Manager. Again, its pretty straight forward, so I won't go into the nitty gritty, but in the Hello World example on the android developer site, it discusses creating an AVD - see: http://developer.android.com/resources/tutorials/hello-world.html#avd

Now that we have Netbeans installed and ready to go, and one (or more) android platforms installed and optionally any third party libraries, the next step is to set up Netbeans the be able to develop Android apps. Thankfully, there is a plugin you can install into Netbeans which aids in development - nbandroid. The process to set it up is outlined on the netbeans wiki: http://wiki.netbeans.org/IntroAndroidDevNetBeans#Installing_the_Android_plugins_for_NetBeans (actually, the wiki links to a guide on the project website). I opted to follow the steps using the netbeans update center. So:

Go to tools -> plugins.
Click on the Settings tab.
Click the Add button located on the right hand side.












Give it a meaningful name (I just called mine Android). Specify URL as: http://kenai.com/projects/nbandroid/downloads/download/updatecenter/updates.xml










Go to the Available Plugins tab. Select Android and Android Test Runner for Netbeans 7.0+ (or what your version is) and click Install









 


Follow the steps to complete the installation.
Go to the Installed tab.
Activate the User Installed Plugins












Now that that is all done, it is time to create an Android Project.

Go to File -> New Project...
In the categories, specify Android, and in the Projects, specify Android Project.
The first time after installing the plugins and creating a new project, you will need to specify the location of the SDK. You can do this on step two of the new project wizard by clicking the Manage Anroid SDK... button - and then specifying the location of where the sdk was extracted to from an earlier step.



















 








Then, simply specify the target platform for the project. Specify a package name, and you should be all good to go. I just specified org.me for the package name to get things going.













To follow the example of the Hello World application as demonstration on the android developer site, specify the code as:

package org.me;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        TextView tv = new TextView(this);
        tv.setText("Hello, World!");
        setContentView(tv);
    }
}

Now, it took me a while to figure out what was going on. When you run the project, a virtual device will appear on the screen. At first instant I thought it wasn't working as it seemed to be stuck on a virtual phone that just said android across the screen. I then realised I was too impatient! It takes about 1 to 2 minutes before the device actually boots up.

I find the best practice is to go into the SDK and AVD Manager, and start the AVD from there - this creates a AVD server that Netbeans can hook into when running the application you are developing. This saves having to wait for the device to boot each time you want to test your code. And the application actually appears on the AVD's screen when you run the project.

Click Start in the SDK and AVD Manager (whilst the device you want to run is selected)
Accept the defaults and click Launch.



















 







Then when you run your project, by default the running device is set to run Select running device. If it is not, select this and click OK.









 







After clicking OK, if you switch to the AVD that is running, you should see the project you are working on, in the emulator. In this case, "Hello, World!".  See more tutorials on the android developer site: http://developer.android.com/resources/browser.html?tag=tutorial