Get Started with Android application development using Linux and Android SDK

From Logic Wiki
Jump to: navigation, search


http://linuxconfig.org/get-started-with-android-application-development-using-linux-and-android-sdk

Eclipse IDE Installation

sudo apt-get install eclipse

Installation of Java SE Runtime Environment

sudo add-apt-repository "deb http://archive.canonical.com/ lucid partner"
sudo apt-get update 

After the update simply use apt-get to install Sun Java JRE 6

sudo apt-get install sun-java6-jre galternatives

As a last step we need to change a default system's java environment from OpenJDK to Sun JRE 6. To do that enter a command:

sudo galternatives

Select java from left menu and select java-6-sun radio button. Once done, simply close galternatives window.

Now verify the java installation with:

java -version
java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
Java HotSpot(TM) Client VM (build 17.1-b03, mixed mode, sharing)

Installation of Android Developer Tool (ADT)

Android Developer Tool is a special Eclipse plug-in which contains integrated android environment for building Android application using Eclipse. The following Android Developer Tool installation steps are same for both Fedora and Ubuntu. There might be small differences between dialog boxes, but the procedure is exactly the same. Start eclipse and take a note of the eclipse version number by navigating to:

Help -> About Eclipse SDK

Follow steps below to install Android Developer Tool Eclipse plug-in for a Eclipse version <= 3.5.

Navigate to:

Help -> Install New Software -> Add

Insert a following information into the dialog box:

Click OK and tick "Developer Tools". Developer tools installation option should appear once Eclipse syncs with a new installation URL added earlier.

Watch for an Error:

If you get an error message saying:

Cannot complete the install because one or more required items could not be found.
Software being installed: Android Development Tools 8.0.1.v201012062107-82219
(com.android.ide.eclipse.adt.feature.group 8.0.1.v201012062107-82219)
Missing requirement: Android Development Tools 8.0.1.v201012062107-82219
(com.android.ide.eclipse.adt.feature.group 8.0.1.v201012062107-82219)
requires 'org.eclipse.gef 0.0.0' but it could not be found

Navigate to:

Help -> Install New Software -> Available Software Sites

and make sure that eclipse's release link is present and enabled.

For HELIOS version add and enable: http://download.eclipse.org/releases/helios

For GALILEO version add and enable: http://download.eclipse.org/releases/galileo

What follows now is just a review of all items to be installed. Hit Next ...

Android SDK Installation

The last step of our installation section will belong to installation of Android SDK. First we need to download Android SDK starter pack from

Android SDK download link:

http://developer.android.com/sdk/eclipse-adt.html.

As a root user, navigate to /opt/ and initiate a download of Android SDK starter pack with a wget command:

NOTE: To keep this guide concise for Ubuntu and Fedora I do not use sudo command. Ubuntu users can change to root ( '#' ) with :

sudo bash 

or simply insert sudo in front of every command below.

NOTE: The wget link below may be outdated. Consult http://developer.android.com for any updates.

# cd /opt
# wget http://dl.google.com/android/android-sdk_r08-linux_86.tgz

Extract the content of Android SDK pack with

# tar xvzf android-sdk_r08-linux_86.tgz

At this point we need to install platforms and optional additional plug-ins or sample codes. Start Android SDK manager:

# android-sdk-linux_86/tools/android

And select SDK Platform Android 2.3, API 9 or choose any other platform depending on your desires. You may also wish to install some Samples code and additional Third-Party add-ons.

When you have selected all packages you desire click Install Selected.

You will be prompted to Accept License. Click Accept All and then Install.

This concludes the installation part of this article.

Setting up Eclipse IDE with Android SDK

Navigate to:

Virtual Devices -> Add

Insert a following information into:

  • Name: MyAndroidVirtualDevice
  • Target: choose any desired Android version and API level available to you from a drop down menu.

Feel free to change or add other values.

Once you click on "Create AVD" a new Android Virtual Device will be created in a following directory:

~/.android/avd

Close Android SDK manager.

The next step is to make the Eclipse IDE aware of our Android SDK installation. Start Eclipse and navigate to:

Window -> Preferences -> Android

and enter a location of your Android SDK installation into SDK Location Box and click apply:

Writing a simple Android Application

Finally we are ready to write our first Android Application.

Open Eclipse and navigate to:

File -> New -> Project -> Android -> Android Project

and insert a following information to start a new Android Project:

Press Finish.

On your left hand side you have a "Package Explorer. Use a package Explorer to navigate to:

HelloWorld -> src -> org.linuxconfig.helloandroid

From there double-click on HelloAndroidActivity.java

Replace an existing code:

package org.linuxconfig.helloandroid; 
 
import android.app.Activity; 
import android.os.Bundle; 

 public class HelloAndroidActivity extends Activity {
   /** Called when the activity is first created. */ 
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
   }
}

With a following code:

package org.linuxconfig.helloandroid; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 
 
public class HelloAndroidActivity extends Activity {
   /** Called when the activity is first created. */ 
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       TextView tv = new TextView(this);
       tv.setText("Hello, Android \n linuxconfig.org");
       setContentView(tv);
   }
}

Save your Project with CTRL + S .

Testing new android application

Now that you have saved your new project you can attempt to run it by navigating to:

Run -> Run -> Android Application -> OK

This will now start your android virtual device "MyAndroidDevice" you have created previously.

NOTE: Allow couple minutes for Android Emulator to start as this things does no happen instantly.

If your application does not start after boot you may need to navigate with your mouse to:

HOME ( house icon ) -> All Apps Launcher Icon

and select "HelloAndroid" from the list as illustrated below:

Click on your new HelloAndroid application: