Saturday, December 15, 2012

Begin Android Software Development on Linux

So far my blogs on starting software development of applications with Android has covered installation of Java (the building block of Android applications), the Android SDK (Google's software development kit for Android) and Eclipse (the integrated development environment for Java and Android). Now it's time to use those items to actually develop Android software applications. It is traditional to begin programming in a new language by writing a simple "Hello World" program - a very simple application that displays the said message to announce to the world that you are now an Android programmer.

This blog assumes that you have some basic knowledge in using the Eclipse IDE and the Java language. However, if you are new to the Eclipse and/or Java, there is a great video tutorial that can be found here (http://eclipsetutorial.sourceforge.net/totalbeginner.html). The same people there also makes a great video tutorial on the Eclipse workbench - the link to that is on the same page at the given URL. If you'd rather read than watch a video, the Eclipse IDE tutorial found here (http://www.vogella.com/articles/Eclipse/article.html) is a good stating point for learning about Eclipse. As for Java, there many great tutorials found on the internet - just use Google to search for them. Also, for a total beginner to Java, the "Java For Dummies", 5th Edition book is very highly recommended by the author.

Now let's start our journey, but before you begin your journey into Android software development, let me point you to the most useful resource that you can use for reference during your journey. This is, of course, Google's Android Developer web site (http://developer.android.com/develop/index.html).

When you arrive at this page, you will find 5 links at the top (2nd line) for Training, API guides, Reference, Tools, and Google Services. You already have visited the Tools web-page during the download of the Android SDK package. The other web-page that are of immediate interest to you at the moment is the Training page - so click on it - you will be greeted with the 'Getting Started' page. Of course, if you are a total beginner at developing Android software, you will spend some considerable time at this site, so I suggest that you bookmark it.

Where to start Android programming? At the beginning, of course, i.e., 'Building Your First App'. You can follow the steps outlined on the resulting web-page when you click on the 'Building Your First App' item or you can follow the same steps given below. The steps below shows more screen shots of the process - so if you are a total beginner, this may be of help. The choice of which to follow is entirely up to you.

  1. Start Eclipse and then click on the New button found in the toolbar (see figure below)

    New project start
     
  2. In the New project window that appears next (see figure below), expand the 'Android' item by clicking on the arrow to the left of the name and selecting 'Android Application Project'. Then click Next.

    Android application project selected
     
  3. Fill in the form that appears (see below) with the data shown. Note that after entering the Application Name, the Project Name and Package Name is automatically filled in - do not change this. The 'Minimum Required SDK', 'Compile With' and 'Theme' has also been filled in with the correct data - in my case, I have only to change the Target SDK to 'API 17: Android 4.2'). Then click Next.

    New Android app data
     
  4. Click Next when the 'Configure Project' screen (see below) appears.

    Configure project
     
  5. Click Next when the 'Configure Launcher Icon' screen (see below) appears.

    Configure launcher icon
     
  6. Click Next when the 'Create Activity' screen (see below) appears.

    Create activity
     
  7. Click Next when the 'New Blank Activity' screen (see below) appears.

    New blank activity
     
  8. In my case, an 'Install Dependencies' window appeard next (see below). Note that you may not see this screen if all the dependencies have been met. If this screen appears, you need to install or upgrade the dependencies before you can proceed with the project - so click on the 'Install/Upgrade' button. The package selection window (shown below) will appear with all the dependent packages pre-selected - click 'Accept All' and then 'Install'. The package(s) will be downloaded and installed. The 'Install Dependencies' screen will reappear - click the Finish button.

    Install dependencies

    Dependencies selection

    Dependencies download and install

    Dependencies installed
     
  9. The main Eclipse workbench will reappear after a while (see figure below) - your first Android project has now been set up with some default files.

    Workbench with first project

    Note that the project that was just created includes a default "Hello World" source file that allows you to immediately run the application without having to enter any lines into your project.
     
  10. Now we will test out the application on the AVD you have previously created. First, make sure that you do not have any Android device plugged into your PC (we will test it on an actual Android device later). Then click on the Run button in the toolbar (see figure below).

    Run the app
     
  11. A Run As window will appear (see below) - select Android Application and click OK.

    Run As window
     
  12. The program will run and the AVD will appear - if this the first time you are running Eclipse, you may get an Auto Monitor Logcat window popping up as shown below. Click OK if it does.

    Auto Monitor Logcat window
     
  13. The AVD login window will appear (see below) - slide to unlock and the output of your first Android application will appear in the AVD (see below).

    Slide to unlock

    MyFirstApp output
     
  14. If you press the 'Home' key and then the 'Applications' icon (as shown below) - you can see that your My First App will also be listed in the available applications.

    Applications icon

    My First App listed
     
Congratulations! You have just successfully tested MyFirstApp on the AVD. Close the AVD once you have finished admiring your work. DO NOT CLOSE ECLIPSE YET! We will now test out the same app on a real Android device - which I assume you already have one.

Before you do any else, I suggest that you visit Google's Android Developer web site (URL given at the beginning of this blog), and then click on 'Tools' and 'Workflow' and finally 'Using Hardware Devices'. Acquaint yourself with the contents that appear- it gives clear instructions on how to set up and connect a device for testing during software development. The required steps are replicated here for your convenience.

  1. Since you are using Ubuntu/Mint Linux, all you need to do is open a terminal and execute:

      sudo nano /etc/udev/rules.d/51-android.rules
     

    This will open a blank file in which you need to enter the following:

      SUBSYSTEM=="usb", ATTR{idVendor}=="4-digit-number", MODE="0666", GROUP="plugdev"

    Replace the idVendor with a 4-digit hex number for your device - the web-page mentioned above has a list of the vendor's IDs listed there.  Find the id for the vendor of your Android device on the and enter it into the file. Save and close the file. Then execute the command:

      sudo chmod a+r /etc/udev/rules.d/51-android.rules

    Then close the terminal.

    As an example, for my Samsung Galaxy Tab 7.7 Tablet, I looked up the idVendor and found it to be 04e8 hex. So the line I entered looks like:

      SUBSYSTEM=="usb", ATTR{idVendor}=="04e8", MODE="0666", GROUP="plugdev"
     
  2. Make sure that USB debugging is enabled on your device - the settings can normally be found at Settings >> Developer options but may vary from device to device, but it is always located in Settings.
     
  3. Connect your Android device to the PC via a USB cable. You may get one or more windows pop-up asking you what to do - select 'Do nothing' in all cases and then press OK. You may also get an error message saying that Ubuntu is unable to mount the Android device - click OK to dismiss the message, i.e., we will ignore it.

    Now open a terminal and execute the following commands:

      cd adt-bundle-linux-x86/sdk/platform-tools
      ./adb devices


    If your Android device has been connected correctly, you should see something like this:

      List of devices attached
      464119060136df1f    device

     
  4. Click on the Run button on Eclipse and a Device Chooser window (shown below) will pop up. Select the device and click OK.

    Device Chooser window

    After a brief period, the application will run and the same output as before will appear on the device. MyFirstApp will also appear in the Applications list of the device. I did not include any screen-shot displayed on my brand new tablet devices as I do not know how to do screen capture on the tablet - yet.
Double Congratulations! You have just successfully tested MyFirstApp on the AVD as well on an actual Android device. I shall now take a short break - the next blog will explore the samples supplied as part of the SDK - hopefully soon.


3 comments:

  1. Wow Great information about toolbar. I like your blog post. If you want more information about Toolbar Developer then visit our website.

    ReplyDelete
  2. Wow Great information about toolbar. Toolbar is best way to increase your internet work speed and also increase online profit. Custom toolbar increase your online reputation and branding also. Learn more about Toolbar development then visit our website.

    ReplyDelete
  3. Wow great information about toolbar development. Hire toolbar developer exhaustive analysis is capable of reshaping toolbar solutions with accuracy and precision. Here, cutting-edge technology in collaboration with cost-efficient methods is the stand-out corner for toolbar developer.

    ReplyDelete