Friday, September 24, 2010

Setting-up Netbeans IDE for Android application development on GNU/Linux

Note: The instructions in this post have been tested on Netbeans 6.8 running on Ubuntu 10.

I have always used Eclipse in the past for coding. Recently I decided to give Netbeans a go mainly for two reasons:

  • It provided Rails support as well as other languages without the need to download extra plugins.
  • I had always found Eclipse I little bit over bloated with functions which made it slow and sometimes difficult to use.

A month ago I also bought a HTC Desire and I wanted to have some fun writting applications for Android.

As I had Netbeans installed and I was happy with it I decided I would use it for Android development.

I searched Android sdk on Google and found out that Google only provided a plugin for Android development under Eclipse. Another Google search revealed the existence of a Netbeans plugin and I decided to give it a go. Below are the steps I followed to get everything working.

1. Download the Linux Android SDK

Go to http://developer.android.com/sdk/index.html and download the Linux SDK. android-sdk_r07-linux_x86.tgz at the time of this writing. I downloaded it to the /tmp folder.

2. Install the SDK

sudo mkdir –p /opt/google
cd /opt/google
sudo tar xvzf /tmp/android-sdk_r06-linux_86.tgz

3. Configure the Android SDK with your selected platforms

Run the Android SDK manager:
cd android-sdk-linux_86/tools
sudo ./android

In available packages select all those for the Android platform you will be developing for. In my case Android 2.1 (you can select more platforms if you want).

Android-select packages

As you can see there several packages for each platform that you can install (actually four per version). I suggest you install the four of them.

Click on Install Selected.

Once the installation finishes, if you go to Installed Packages you should see something like

Android Installed

Exit the manager.

4. Create an  Android Virtual Device.

Run the Android SDK Manager again but not as superuser, but rather with the user account you will be using to do the development, normally the one you use to log in in your desktop machine:
./android

Under Virtual Device, click on New and select the options that fit your needs. You have more information about all the parameters in http://developer.android.com/guide/developing/tools/avd.html.

Android Create Virtual Device

You will need to create a different virtual device for each Android Platform you will be developing for.

5. Launch Netbeans

6. Install the Android plugin for Netbeans

Add the source server for the nbandroid plugin by going to Tools/plugins,  Settings tab, click on Add 3rd Party Plugin and entering the following url: http://kenai.com/downloads/nbandroid/updates.xml

Android - Install Plugin

This should make the Android plugin available for download.

Go to the Available tab and select Android from the list.

7. Restart Netbeans

8. Add the Java Android Platform to Netbeans platform list

Go to Tools / Java Platforms. The Java Platform Manager will open.

Android - Add Platform 1

Click on Add Platform and  select the Google Android Open Handheld Platform. Click on Next.

Android - Add Platform 2

Enter the path to your Android SDK folder under File Name (in my case /opt/google/android-sdk-linux_86).

Android - Add Platform 3

Finally select the Android Android Platorm you want to add and give it a name. Click on Finish.

Android - Add Platform 4 Android - Platform Added

9. Create a new Android project

Let’s write our first Android application.

Go to File / New Project, select Android, then Android Application.

Android - Create Application

Click Next. Now enter Hello World as Project Name and Hello World under ActivityName . Click on Finish.

Android - New project 

The HelloWorld.java file should open. If not open it. It is located within the Hello World project under Source Packages / org.me.helloworld.

Edit the file to look like the following:

package org.me.helloworld;

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

public class HelloWorld extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle icicle) {

        super.onCreate(icicle);

        TextView tv = new TextView(this);

        tv.setText("Hello, Android");

        setContentView(tv);

    }

}


Save the changes and now let’s run it!

Click on on the run button (the green play button on the top button bar). Netbeans will automatically open the Android emulator and run our application (you may need to first unlock the phone by dragging the lock to the right). The Android emulator may take some time to load so be patient.

lock

HelloWorld

 

Congratulations! You are ready to start developing your own Android Apps using NetBeans!

To learn more about Android apps development I suggest you check the guide available at the official Android developer’s website: http://developer.android.com/guide/developing/tools/avd.html

No comments:

Post a Comment