Wednesday, December 25, 2013

Starting Android Programming on Windows 7 (UPDATED)

This is an updated blog based upon an earlier blog on starting Android programming on a Windows 7 PC. It was done to reflect changes to the Java JDK, Android SDK and ADT, and the Eclipse IDE. Oracle Java has been updated to 1.70_45 while Google has created an Android ADT bundle which includes Eclipse, the ADT plugin and all other bits and pieces which are required for Android software development. This makes things easier for the developer since only 2 package types need be downloaded and installed - Oracle Java and the ADT Bundle. I also took this opportunity to include lots of screen-shots as well as revise the text of the blog.

Recently, I invested in an Android smartphone (albeit a cheap one) and wanted to write my own software for the smartphone. So I did some research via the Internet about Android software development. As usual, there were a lot of information available from the Internet, some good and some bad (as far as 'easy-to-read' and 'understandable' goes) - so I decided to write my own guide on how a beginner to Android software development can start on a Windows 7 PC. This blog covers Android software development on a 32-bit Windows 7 PC while the procedures for a 64-bit Windows 7 PC is very similar. I will also assume that the reader is familiar with software programming in general and Java in particular. No effort will be made to teach the reader how to program - so be forewarned.

Writing software for Android requires Java. So, the first thing to do is download and install the Oracle Java JDK.

  1. Open a web browser and go to here - Click on 'Download' under the JDK column - NOT the JRE column (see figure below).


         Select Java 7 JDK

    You will then brought to the download page where you first have to accept the license agreement (click on the 'Accept License Agreement) and then you have to select the correct file for your OS - in my case, it is 'jdk-7u45-windows-i586.exe' for the 32-bit Windows 7 32-bit OS. Note that that you can download and install the same 32-bit Java JDK on a 64-bit Windows 7 PC - which is highly recommended anyway.


         Download Java 7 JDK

    Click on 'Save File' when the dialog box pops up. This will take a while to finish downloading. When completed, and on the same download page, click on the demos and samples file (in my case it is jdk-7u45-windows-i586-demos.zip) to download it.


         Download Java 7 Samples & Demos

    When completed, go back to the last web page (use the browser's back key), scroll down the web page until you see Java SE 7 Documentation (see figure below) - then click on it.


         Select Java 7 Documentation

    Again you first have to accept the license agreement (click on the 'Accept License Agreement) and then click on the 'jdk-7u45-apidocs.zip' file.


         Download Java 7 Documentation

    Save this file to the same folder as the JDK you have downloaded. Again, this will take a while to finish downloading.

    Go to the folder where the downloaded Java JDK file 'jdk-7u45-windows-i586.exe' resides and double-click on it. Click 'Yes' when UAC pops up - then 'Next', 'Next', and again 'Next'. When you see the 'Successfully Installed Java SE Development Kit 7 Update 45' window, click 'Close'.

    To install the Java 7 API documents, first unzip the file (jdk-7u45-apidocs.zip in my case) to a temporary folder and then move the 'docs' folder from the temporary folder to the Java JDK folder (C:\Program Files\Java\jdk1.7.0_45 in my case). If UAC pops up, click on the 'Continue' button.

    To install the Java demo and sample files, first unzip the file (jdk-7u45-windows-i586-demos.zip in my case) to a temporary folder. This will create a folder named 'jdk1.7.0_45' in which 2 sub-folders, 'demo' and 'sample' are found. Move these 2 sub-folders to the Java JDK folder (C:\Program Files\Java\jdk1.7.0_45 in my case). If UAC pops up, click on the 'Continue' button. You now have Java JDK 7 installed.
     
  2. Windows 7 does not set the environment variable JAVA_HOME. If you plan to use a text editor and/or the command line (or with Eclipse) to develop Java programs, you have to do this manually if you want to develop Java software. Failure to do this may cause error(s) when developing and testing Java software. Also, the path variable need to include the 'bin' subdirectory of the Java JDK if the 'javac' compiler is used to compile Java software from the command line. To set these variables, follow these steps:

    1. Right-click on the 'Computer' icon on your desktop (if present) or click on the 'Start' button then right-click and select 'Properties'.
    2. Click on 'Advanced system settings' located on the left-hand column.
    3. Click on the 'Environment Variables' button.
    4. Under the System variables part, click 'New'.
    5. Enter the Variable name as JAVA_HOME.
    6. Enter the Variable value as the installation path for the Java JDK (C:\Program Files\Java\jdk1.7.0_45 in my case).
    7. Click 'OK' to set the new JAVA_HOME variable.
    8. Under the System variables part, click on the 'Path' variable already there (you may have to scroll down a bit) and the click on the 'Edit...' button.
    9. Press the 'End' key to place the cursor at the end of the line and enter the 'bin' path (preceeded by a semi-colon ';') - in my case is ';C:\Program Files\Java\jdk1.7.0_45\bin'. Press 'OK', 'OK' followed by another 'OK'.
    10. Reboot Windows 7 in order for the PC to recognize the new system variables.

    To test whether the environment variable JAVA_HOME has been set correctly, open a command window and type:

      echo %JAVA_HOME%

    If the output shows the correct path (C:\Program Files\Java\jdk1.7.0_45 in my case) then JAVA_HOME has been set correctly. If not, check that you have followed the steps (1) to (10) above correctly.

    To check whether the path has been set correctly, type:

      javac -version

    If you see 'javac 1.7.0_45' as the output line, the the path has been set correctly.  If not, check that you have followed the steps (a) to (j) above correctly. Once you have corrected the problem, and as another test that Java is working correctly, type:

      java -version

    If you see "java version "1.7.0_45"" in one of the output lines, then you have installed Java correctly.
     
  3. Writing software for Android also requires the official Android SDK, and the Eclipse IDE. Google has created a ADT Bundle to make things easier for the developer as this is the only package (it includes the essential Android SDK components and a version of the Eclipse IDE with built-in Android Developer Tools) that has to be downloaded. It contains everything a developer needs to begin developing Android applications. Open a web browser and go to here. Click on the 'Download the SDK' button on this page (see below).


         Download ADT Bundle

    On the next page, agree to the Terms and Conditions, select the correct version (32 or 64 bit), and then click on the 'Download the SDK ADT Bundle for Windows' (see below) button to start downloading.


         Select Download ADT Bundle

    Wait for the download to finish then unzip the file to the C:\Users\*username* folder (replace *username* with your own login name). Once completed, you will find a new folder labelled 'adt-bundle-windows-x86-20131030' under C:\Users\*username*.

    That's it! The IDE is already loaded with the Android Developer Tools plugin and the SDK is ready to go.
     
  4. The Eclipse executable is found in the C:\Users\*username*\adt-bundle-windows-x86-20131030\eclipse (replace *username* with your own login name) directory. It is suggested that a shortcut to the executable be made on the desktop so that Eclipse can easily started.

    Double click on the Eclipse icon to start it - if this is the first time that Eclipse is started, a default workspace needs to be specified in the windows that pops up (see below).


         Eclipse Workspace

    Put it anywhere you like - in my case I placed it under C:\Users\*username*\Projects\Android (where *username* is my own login name) and I use this as my default by selecting 'Use this as the default and do not ask again'. Click 'OK' - if this is the first time that Eclipse is started, a 'Contribute Usage Statistics' window will be displayed as shown below. Select either 'Yes' or 'No' and then click on the 'Finish' button.


         Eclipse Contribute Usage Statistics

    The Eclipse 'Welcome Screen' will be displayed as shown below. Read this and follow the links if you wish. Remember that this window can be re-displayed at any time later by selecting 'Help' and then 'Android IDE' from the menu.


         Eclipse Welcome Screen 
     
    Close the welcome screen by closing the Android IDE tab and the main Eclipse workspace will be displayed (see below).


         Eclipse Workspace
     
  5. Click on the 'Window' item on the Eclipse menu, and then select 'Android SDK Manager'. After a while, the Android SDK Manager window (shown below) will appear.


         SDK Manager Window

    Notice that both the Android SDK Tools and Android SDK Platform-tools have already been installed along with some of the files for Android SDK (API 19). If there are updates to any of the items already installed, these will be indicated as shown in the figure above. Notice the button at the lower right hand corner that says 'Install 5 packages...' - click on this button and a window (as shown in the figure below) will pop-up asking to to confirm the packages to be installed along with the acceptance of the license. Click on the 'Accept License' and then the 'Install' button. Then wait for the updated items to be downloaded and installed.


         Package Install

    When completed, you should see a screen similiar to the one below. At this point, you can select other extra packages to install. Note that you can start developing Android software for Android version 4.4 (KitKat) without installing anything else. However, if your actual Android device is not version 4.4 but a previous version, for example version 4.1.1 (JellyBean), you can download files for that version but be aware that version 4.4 (KitKat) is supposed to be backward compatible to lower versions (what this mean is that software developed using version 4.4 SDK will work on previous versions of Android).


         Updated Packages Installed

    I do encourage that the samples and documentation for Android 4.4 be downloaded and installed. To do this, place check-marks on the 'Documentation for Android SDK' and the 'Samples for SDK' files. Then click on the 'Install 2 packages...' button. The Choose Packages to Install window will pop up (see below) - select 'Accept License' followed by the Install button. The items will then start downloading - progress of the download and install process will be shown on the SDK Manager window.


         Choose Packages Window

    Once the process is completed, the SDK Manager will show something like the one shown in the figure below. Then close the SDK Manager.


         Packages Installed
     
  6. You can now set up one or more Android virtual devices (AVD) which correspond to your actual Android device(s). By doing this, you can test the developed software to see whether they work or not on the device. To do this, first click on the Window item in the menu, and then select Android Virtual Device Manager. After a while, the AVD Manager window (shown below) will appear.


         AVD Manager Window

    If this is the first time you are using the Android AVD Manager, there will be no virtual devices and the list of available AVD's will be empty. To create a new virtual device, click the 'New...' button. A window will appear in which you need specify what type of device you are going to create (see figure).


         New AVD Window

    First enter the AVD Name of the device - for example 'MyKitKat' - any name will do, as long as you remember what type of device it represent (an Android 4.4.2 KitKat device in this case).

    Next you need to specify a Device to emulate - in my case, I decided to use a 5.1" WVGA smartphone as this closely resemble the actual phone that I have in my possession. You, on the other hand, may have different Android hardware - choose one closest to the specification of your hardware.

    Next you need to specify the Target - i.e., the version of Android targeted - click on the right-hand downward arrow and click on a suitable one from the list. Note that this list will only contain all the versions that you have previously installed using the Android SDK Manager. In my example, there is only one - the Android 4.4.2 - API Level 19. The CPU/ABI will be automatically entered - in my case 'ARM (armeabi-v7a)'.

    Leave the Keyboard and Skin at the default. Then select a Back Camera to use by clicking on the right-hand downward arrow and click on a suitable one from the list. Since I have a webcam on my PC. I selected 'Webcam0'. Note that this item will not be listed if you do not have a webcam - if that is so, select 'Emulated' instead.

    Leave the RAM and VM Heap and Internal Storage values at the default values. You can, however, change to suit your preference.

    You can now specify the size of the SD card to emulate (in KiB, MiB, or GiB) - in my case I entered 200 MiB. Under Emulation Options, enable 'Use Host GPU'. Leave the rest of the fields at the defaults. The final screen will look something like the one shown below.


         New AVD Settings

    Click on the 'OK' button - the newly created virtual device will now appear in the AVD list (see below).


         New AVD Created
     
  7. To test whether the newly created virtual device works, select the device and click on the 'Start' button - the AVD Launch Options window will appear (see below).


         AVD Launch Options Window

    You are free to set any of the launch options here. For this article, we will leave the options at their defaults and click 'Launch' on the launch option window - the AVD will then start (see below).


         AVD Launching

    After a while, the KitKat Android virtual device will appear on the desktop - initially this will be blank (as shown below). This denotes that it is still loading - be patient.


         AVD Initial Screen

    The KitKat Android virtual device will change to one with a flashing 'Android' figure (see below). Wait for the flashing to stop - which may take some time to finish.


         AVD Loading

    Once the AVD has finished loading, you will be greeted with a screen similiar to the one below (a help screen for those curious) - just admire it and then click on the OK button.


         Android Initial Screen

    The home screen for Android will then appear as shown below.


         Android Home Screen

    Test the AVD on/off button by clicking on the power button (rightmost button on the top row). If this works, then the AVD's screen will go blank, signifying a power off condition (see below).


         AVD Powered Off

    Click on the power button again. The login screen (see below) will appear if everything works.


         AVD Login Screen

    Just like a real Android device, you have to slide the padlock to the right to unlock the device - do so and the home screen will reappear.


         AVD Slide To Unlock 
     
    Now click on the Apps button on the home screen (middle button on the bottom row of the home screen). Another help screen similar to the one shown below will the appear. Click on OK to dismiss this screen.


         AVD Apps Help

    The screen will now display all the apps (application software) on the AVD. You can play around with the apps later - for now let's test the Menu, Back and Search buttons.


         AVD Apps Screen

    First click on the Menu button - the screen will look like the one shown below with 2 menu items appearing from below the screen. As usual the items and number of items will change depending on the screen being viewed. Click Menu again to dismiss the menu.


         AVD Menus

    Now click on the Search key (the rightmost button which looks like a magnifying glass) and after a short delay (depending on your internet connection) the screen will look like the one below.


         AVD Search Screen

    Now click on the Back button (the one to the left of the Search button) and the home screen will re-appear. Click on the Apps button again and the apps screen will re-appear. Then click on Home button (the leftmost button which looks like a house) and the home screen should re-appear.

    Once you have tested all the buttons, you can now play with the AVD. Just remember that this is a virtual machine emulating an ARM CPU based device on an x86 PC. If you expect the virtual device to be fast, then you need to lower your expectation - try it out and see what I mean. It does, however, serves as a test bed for the applications that will be developed later. Close the AVD when done and then close the AVD Manager.

    Now that you have downloaded, installed and configured all the components for developing software on the Android platform, you can start off programming with the obligatory 'Hello World' program.
     
  8. Start Eclipse and then click on the New button found in the toolbar. 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

    Fill in the form that appears (see below) with the data shown. Note that after entering the Application Name 'My First App', 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 19: Android 4.4.2'). Then click Next.


         New Android App Data

    Click Next and when the 'Configure Project' screen (see below) appears, leave everything here at their defaults.


         Configure Project

    Click Next and the 'Configure Launcher Icon' screen (see below) appears. Do not change anything here.


         Configure Launcher Icon

    Click Next and the 'Create Activity' screen (see below) appears. Again, do not change anything here.


         Create Activity

    Click Finish when the 'New Blank Activity' screen (see below) appears.


         New Blank Activity

    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

    Although it is not evident from the figure above, 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. The expected output from this project are the words "Hello world!" displayed on the device's screen. You can view the expected output by clicking on the 'activity_main' tab on the Eclipse IDE.
     
  9. 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 As... button (the green one with the white right-pointing arrow) in the toolbar. A Run As window will appear (see below) - select Android Application and click OK.


         Run As Window

    The program will run and the AVD login window will appear (see below) - slide to unlock and, after a while, the output of your first Android application will appear in the AVD (see below). If this the first time you are running Eclipse, you may also get an Auto Monitor Logcat window popping up as shown below. Click OK if it does.


         AVD Login Window


         MyFirstApp Output


         Auto Monitor Logcat Window

    If you press the Home button and then the Apps button, you can see that your My First App will also be listed in the available applications (see below).


         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 here (http://developer.android.com/sdk/index.html), and then click on 'Workflow' and then 'Using Hardware Devices'. Acquaint yourself with the contents that appear - it gives clear instructions on how to setup and connect a device for testing during software development.

    Note that most Android devices manufacturers have instructions on how to connect the devices to a Windows 7 computer via a USB port. I suggest that you peruse their instructions first before trying to connect the devices to the computer. In some extreme cases, the USB drivers are part of a software package (e.g., the Samsung Kies) and is not separate - the whole package has to be installed before the USB connection works correctly.
     
  10. Install the necessary Windows 7 USB drivers before trying to connect the Android device to the computer. Either follow the device manufacturer's instructions (first choice) or follow the instructions here http://developer.android.com/tools/extras/oem-usb.html if you have any Google devices.

    Note 1: If you have a Nexus device that is not listed, try the Nexus (generic) driver first.

    Note 2: If you cannot find the C:\Users\*username*\adt-bundle-windows-x86-20131030\sdk\extras\google\usb_driver\ directory, then the Google USB Driver package has not been installed into the SDK. Use the SDK Manager (Eclipse >> Window >> Android SDK Manager) to mark and install the Google USB Driver package.
     
  11. 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.

    Connect your Android device to the computer via a USB cable. You may get one or more messages about the drivers being installed and the installation has been successful. If the instllation failed, do not go any further until you have resolved this - the USB driver MUST be installed in order for you to test developed applications on your device.

    Open the Windows Explorer and navigate to C:\Users\*username*\adt-bundle-windows-x86-20131030\sdk\platform-tools\ directory and Shift-right-click on it and select 'Open command window here'. Then execute the following command in the window:

      adb devices

    If your Android device has been connected correctly, you should see something like this (the numbers to the left may be different from that shown):

      List of devices attached
      0123456789ABCDEFG     device


    Close the command window.
     
  12. 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 (see below). If you select Apps on your device, MyFirstApp will also appear in the Applications list of the device.


         Hello World on Device

    Double Congratulations! You have just successfully tested MyFirstApp on the AVD as well on an actual Android device.
In this blog, a lot of features on Java, Android SDK and Eclipse were not explained. If this is the first time you are trying all of the said tools, then you need spend some time to familiarize yourself with them. Documentation on all the tools are available from the Internet, so look for them and read, read, read ... and then try out the tools. Don't be afraid of making mistakes ... it's the only way to learn. Good luck!

5 comments:

  1. Great tutorial, thank you. Because under Window menu does not exist Android SDK Manager, I followed this path:
    Window > Customize perspective > in Command Groups Availability tab > select Android SDK and AVD Manager > click OK.
    Can you tell me, please, how to add the Debug button in toolbar?

    ReplyDelete
  2. Hi Ridzman, another error appears, when I try to install plug-ins, Android DDMS and Android Development Tools (in Developer Tools), I get this error message:
    Your original request has been modified.
    "Android DDMS" is already installed, so an update will be performed instead.
    "Android Development Tools" is already installed, so an update will be performed instead.
    The Finish button is not active. Can you advise me what to do, please.

    ReplyDelete
  3. Sorry for my new question. Because it did not work correctly, I deleted the folders .android and adt-bundle-windows from c:\Users\MyName and the problems remained:
    1. After I created a new Android Application Project string.xml from /res/values has only app_name(String) and from this point the application does not appear in the Android emulator. Please guide me how to correct the errors. Thank you.
    Thanks a lot for your help.

    ReplyDelete
  4. Acetech is one of the largest independent solution providers in custom software development services and Offshore software development. For details or business enquiries visit Acetech software development company in delhi.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete