Sunday, December 9, 2012

Android SDK on Ubuntu 12.04 (Precise Pangolin)

In my previous blogs, I showed how to install various 'flavours' of Java on Ubuntu 12.04 - both 32 and 64 bits versions. Now I will show how to install and configure the Google Android SDK to be used together with Java for Android software development.

  1. First head over to:

      http://developer.android.com/sdk/index.html

    and download the latest (as of December 2012) file:

      android-sdk_r21-linux.tgz

    IMPORTANT NOTE:
    If you are using the 64-bit version of Ubuntu 12.04, you MUST download and install the 'ia32-libs' package since the Android SDK is a 32-bit application and requires the 32 bit library to operate properly. To download and install, open a terminal and execute:
      sudo apt-get install ia32-libs

  2. Open a terminal and unzip the downloaded file:

      tar xvf ~/Downloads/android-sdk_r21-linux.tgz

    This will create a new directory 'android-sdk-linux' in your home directory containing all the unzipped files. Why the home directory? The simple answer is 'to avoid file permission problems'. Note that by installing the SDK in the home directory, only the current user can access it - so security is guaranteed.

    If, however, you want the SDK to be accessed by other users, then you may have to install it somewhere else - perhaps the /opt directory. This article will not describe how to do that - it just assumes that the current user is the only one that can access the SDK.

  3. Change directory and execute the android SDK:

      cd android-sdk-linux/tools
      ./android


    NOTE:
    You can set the PATH environment to point to your Android SDK tools directory so that you can start the 'android' program anywhere. Edit the .bashrc file in your home directory and add the following lines to the end of the file:

    # set PATH so it includes user's Android SDK if it exists
    if [ -d "$HOME/android-sdk-linux" ] ; then
      PATH="$HOME/android-sdk-linux:$HOME/android-sdk-linux/tools:$PATH"
    fi

    This will only take effect on the next logout/login or restart.

    The SDK Manager will start as shown in the screen-shot below.

    Initial SDK Manager screen

    Notice the progress bar at the bottom of the screen - this indicates that the SDK Manager is querying for updates from Google. Wait until this is finished and the 'Done loading packages' message appears. Notice that the 'Android SDK Tools' have already been installed. According to Google, the 'Android SDK Platform-tools' is also required when you install the SDK for the first time - so the first thing you need to do to place a check mark on that item. Then click on the 'Install 1 package' button (see figure below).

    Android SDK Platform-tools selected

    A window will pop-up asking you to confirm - click on the 'Accept' radio button and then the 'Install' button.

    Confirmation screen

    Accept and install

    The platform-tools package will then be downloaded and installed. A log screen (as shown below) may appear if there are problems during install - ignore the 'Stopping ADB server failed (code -1)' message if it appears on the log - this seems to be harmless. Close the log window by clicking on the 'Close' button.

    Log window

    The screen will now look like the one shown in the screen-shot below.

    Final SDK Manager screen

  4. You will now have to select at least one set of packages for Android application development. For this article, the Android 4.2 (API 17) package is selected by placing a check mark on this package. Then click on the right-pointing arrow to the left of this item to expand it. You can now inspect the list of packages that will be downloaded and installed. The final screen will look like the one shown below.

    Select packages

    Click on the 'Install 6 packages' button - a window will pop up listing all the packages to be installed. Since multiple items have been selected, click on the 'Accept all' radio button - then press the 'Install' button to start the process of downloading and installing the packages. The progress bar at the bottom indicates the status of this process. Also a log screen may appear if there are problems during install - fix the problem and try again if so. The final screen should show that all the packages have been successfully installed as shown.

    Selected packages installed

    That's it - you have installed the latest Android packages necessary for software development. As of December 2012 this is Android 4.2 (API 17) - codenamed Jelly Bean. Is it necessary to install all the other packages (API 3 to 16)? That is entirely up to you - if you really need to develop software only for a specific Android version, then, by all means download and install it using the SDK Manager.

  5. Now we need to set up at least one AVD (Android Virtual Device) on which you can test your developed software. The AVD is an emulator which emulates the ARM processor. To set up the AVD we need to invoke the AVD Manager which is part of the SDK Manager. Click on 'Tools' (in the menu bar) and then click on the 'Manage AVDs...' item. You will see the screen as shown below.

    Android Virtual Device Manager screen

  6. Click on the 'New' button - a new window will pop-up (see below).


    AVD Manager selections

    Give the new AVD a name - for example 'MyJellyBean'.

    Under 'Device' select a suitable device - in the example, I have selected '7.0" WSVGA (Tablet) (1024 x 600: mdpi)'.

    Under Target, select 'Android 4.2 - API Level 17'.

    Leave the 'Keyboard:' and 'Skin:' selected.

    Under 'Front Camera:', you can select 'None' (default) or 'Emulated'. If you have a webcam on your PC, this can also be used as the camera.

    Change the 'Memory Options:' RAM to at least 768 if you have a minimum of 4GB of system RAM. This will make the AVD load a bit faster.

    Leave the 'Memory Options: VM Heap:' as is.

    Leave the 'Internal Storage:' as is.

    Under 'SD card:' I entered 128 MiB which should be sufficient for testing.

    To make the emulator perform even faster, enable GPU emulation by selecting the 'Use Host GPU' under 'Emulation Options'. You can also select 'Snapshot' if you wish.

    The final screen will look like the one shown below. Then click on the 'OK' button.

    AVD Manager data

    If there are no errors, you will see a screen like the one below. Press 'OK' - the AVD list will then show the new AVD - MyJellyBean in this case (see figure).

    AVD Manager results

    New AVD created

  7. To start the AVD, first select it on the list, then click the 'Start' button (see below).

    New AVD selected for starting

    A 'Launch Options' window will pop-up (see below). You can set the display size and wipe the user data from here if you wish - then click on the 'Launch' button. You will then see a 'Starting Android Emulator' screen.

    AVD Launch Options

    Emulator start screen

    While the emulator is starting (may take time depending on the RAM size set and the speed of your system CPU and GPU), a 'thank-you' window from Google may pop-up (see below) - if you wish you can agree to send usage statistic to Google - then click on 'Proceed' to dismiss the window.

    Google thank-you

    The emulator loading screen with a flashing 'Android' sign will be displayed (shown below).

    Android loading screen

    Once the emulator has finished loading, an initial screen will be displayed as shown below.

    Initial screen

    Click on the 'OK' button and the main 'Home' screen will appear (see below).

    Home screen

  8. To test the emulator On/Off button, click the 'power' button on the right side (first row of buttons) to switch off and then click on it again to power on. The login screen will appear (see below) - slide (using the mouse) to unlock the device. The 'Home' screen will appear (see below).

    MyJellyBean powered off

    MyJellyBean powered on - login screen

    Slide to unlock


    Home screen

  9. You can play with the emulator at this stage - close the main window when done. Then close the 'Starting Android Emulator' screen. Close the Android SDK Manager window if you wish.

    Note that at the moment, you have start a terminal. change to the android-sdk-linux/tools/ directory and execute the ./android command every time you wish to start the Android SDK Manager. If you had implemented my tip about setting the PATH environment to point to the android executable, then all you have to do is open a terminal and execute the android command. It does seem to be a bit kludgey but it works. Is there an easier way? There are various suggestions on the Internet, found by using Google. If you are using the Unity interface and wish to place the Android SDK Manager launcher in the dock, I suggest the method outlined in the followthegeeks.com website (http://followthegeeks.com/how-to-manually-create-application-launcher-in-ubuntu-12-04-unity/). To reiterate the instructions:

    a) Open a terminal and execute:

      sudo gedit /usr/share/applications/AndroidSDK.desktop

    In the gedit editor window, copy and paste the following text:

    [Desktop Entry]
    Version=r21
    Type=Application
    Terminal=false
    StartupNotify=true
    Icon=/home/ridz1/Pictures/android-sdk-96.png
    Name=Android SDK
    Comment=Google Android SDK Manager
    Exec=/home/ridz1/android-sdk-linux/tools/android
    Categories=Application;Development;


    Change the items necessary for your system - for example, the location of the android executable (mine is '/home/ridz1/android-sdk-linux/tools/android' - yours may be different. Also make sure that the location of the icon for the Android SDK is valid. Save and close gedit.

    b) Now click on the Unity dash and enter 'android' in the search box. The Android SDK will be displayed - click on the icon to start the program. The program will run and the Android icon will appear as an item in the dock. Right-click on that item and select 'Lock to Launcher'. Viola! You now have a Unity dock with the Android SDK permanently present. The screen-shot below shows my Unity desktop in all it's glory.

    My Unity desktop

    That's it - you now have a working Android SDK and you are now almost ready to start developing Android software on your Ubuntu 12.04. The next blog will describe installing Eclipse - a programming environment for developing Java programs - and with the help of several Google plugins for Eclipse - a programming environment for developing Android programs as well.

4 comments:

  1. Hello Mr. Ridzwan...
    I followed all your steps upto launch..

    after launching AVD I found these errors..

    Starting emulator for AVD 'VirtualPhone'
    Failed to load libGL.so
    Warning: No DNS servers found
    error libGL.so: cannot open shared object file: No such file or directory
    Failed to load libGL.so
    error libGL.so: cannot open shared object file: No such file or directory
    emulator: emulator window was out of view and was recentered

    Plz help me out....
    i am using android4.1.2(API 16)

    ReplyDelete
    Replies
    1. Could you give more info on your system? What sort of graphic card is installed? Is it 32 bit or 64 bit Ubuntu? If 64 bit, is the ia32-libs installed?

      The message 'Failed to load libGL.so' looks suspiciously like your system could not find libGL.so. I would recommend doing a file search for libGL.so and adjusting the link command below based on the location. For example if you found libGL.so in usr/lib/ then use:

      ln -s /usr/lib/libGL.so.1 ~/android-sdk-linux_x86/tools/lib/libGL.so

      I am a bit surprised that this happens - on my PC, I did not have to set the link at all. FYI libGL.so is part of the mesa package, which was installed by default on my system.

      Delete
  2. respected sir,

    while creating avd, filling all the information as per your guidance.. OK button seems disable.. can you please provide me your mail id so i can brief it you with snapshot of that thing.

    ReplyDelete