Powered by Java – Getting Started With Android Development – Part 1

One of the most exciting things about writing Android applications (apps) is having multiple options when choosing your programming language. Android apps can be written in:

  • Java, using the official Android SDK,
  • C#, using Xamarin Studio,
  • Javascript, using Ionic Framework (Hybrid), or
  • Javascript, using React Native (Native), etc.

Of these options, the most commonly recommended and used language is Java. Because Java is an object-oriented programming (OOP), if you already have some background experience using any of the other aforementioned OOP languages or experience with Java, itself, you will find that getting started in building apps in Android with Java will be easier. However, building applications for Android differs greatly from building Desktop or Web Applications.

So, let’s get started.  To start working with Java, we must first get it set up for use.

Setting up Java

To start programming with any language, there are few options you can use.  For example, you can use an online integrated development environment (IDE) to execute programs and see the output. This option is handy as you need not download or setup any software on your machine.

Here are some online IDEs which are easy to use for Java programming:

The drawback to using an IDE is that you will still need to install and run Java programs locally in order to start developing apps for Android.

Installing Java

How you install and run Java depends on the operating system (OS) you’re running on your machine. In the following, we will cover the Mac OSX, Linux, and Windows machine installation processes separately.

Windows

Step 1: Download JDK
  1. Visit the Oracle site here to download Java SE and click on the “JDK Download” button.
  2. Look for the latest “Java SE Development Kit 8u{xx}” ⇒ Check “Accept License Agreement”.
  3. Choose the Java Development Kit (JDK) for your operating system, e.g., “Windows x64” (for 64-bit Windows OS) or “Windows x86” (for 32-bit Windows OS). You can check whether your Windows OS is 32-bit or 64-bit via “Control Panel” ⇒ (Optional) System and Security ⇒ System ⇒ Under “System Type”.
Step 2: Install JDK and JRE

Run the downloaded installer (e.g., “jdk-8u{xx}-windows-x64.exe”), which installs both the JDK and Java Runtime Environment (JRE). By default, the JDK will be installed in directory “C:\Program Files\Java\jdk1.8.0_xx”, where xx denotes the upgrade number; and JRE in “C:\Program Files\Java\jre1.8.0_xx”. Accept the defaults and follow the screen instructions to install the JDK and JRE.

In the above step, be sure to note the directory in which the JDK was installed along with its version number, as we will need it in the next step.

We will refer to the JDK installed directory as , hereafter, in this procedure.

Step 3: Include the JDK’s “bin” Directory in the PATH

Windows Shell searches the current directory and the directories listed in the PATH environment variable (system variable) for executable programs. The JDK’s programs (such as Java compiler (javac.exe) and Java runtime (java.exe)) reside in the directory “\bin”. We need to include “\bin” in the PATH to run the JDK programs.

To edit the PATH environment variable in Windows 7/8/10:

  1. Open the “Control Panel” ⇒ (Optional) System and Security ⇒ System ⇒ Click “Advanced system settings” on the left pane.
  2. Switch to the “Advanced” tab ⇒ Click the “Environment Variables” button.
  3. Under “System Variables” (the bottom pane), scroll down to select “Path” ⇒ Click “Edit…”.
Step 4: Verify the JDK Installation

Launch a CMD shell via one of the following means:

  1. Click the “Search” button ⇒ Enter “cmd” ⇒ Choose “Command Prompt”, or
  2. Right-click the “Start” button ⇒ run… ⇒ enter “cmd”, or
  3. (Prior to Windows 10) Click the “Start” button ⇒ All Programs ⇒ Accessories (or Windows System) ⇒ Command Prompt, or
  4. (Windows 10) Click the “Start” button ⇒ Windows System ⇒ Command Prompt

Issue the following commands to verify your JDK installation:

  1. Issue “path” command to list the contents of the PATH environment variable. Check to make sure that your \bin is listed in the PATH.
  2. Type in the following:
=// Display the PATH entries
prompt> path
PATH=c:\Program Files\Java\jdk1.8.0_xx\bin;[other entries...

Caution! Don’t type prompt>, which denotes the command prompt! Key in the command (highlighted text) only.

  1. Issue the following commands to verify that JDK/JRE is properly installed:
// Display the JRE version
prompt> java -version
java version "1.8.0_xx"
Java(TM) SE Runtime Environment (build 1.8.0_xx-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)

// Display the JDK version
prompt> javac -version
javac 1.8.0_xx

Step 5: Write a HelloWorld Java Program
  1. Create a separate directory to store all the programs you create, e.g., “D:\myProject”, or “C:\myProject”, or any directory of your choice. The directory name should not contain blank or special characters. Use meaningful but short names, as it is easier to type and remember.
  2. Launch a programming text editor (such as Sublime Text, or Atom). Make a new file and enter the following source code:
/*
* First Java program to say Hello
*/
public class Hello {  // Save as "Hello.java"
  public static void main(String[] args) {
     System.out.println("Hello, world!");
  }
}
  1. Save the file as “Hello.java”, under your work directory:
Step 6: Compile and Run the HelloWorld Java Program

To compile the source code “Hello.java”:

  1. Start a command prompt terminal.
  2. Set the Current Drive to the drive where you saved your source file “Hello.java”. For example, suppose that your source file is saved in drive “D”, enter “D:” as follows:
prompt> D:
D:\xxx>
  1. Set the Current Working Directory to the directory that you saved your source file via the cd (Change Directory) command. For example, suppose that your source file is saved in directory “D:\myProject”.
D:\xxx> cd \myProject
D:\myProject>
  1. Issue a dir (List Directory) command to confirm that your source file is present in the current directory:
D:\myProject> dir
......
xx-xxx-xx  06:25 PM               277 Hello.java
......
  1. Invoke the JDK compiler “javac” to compile the source code “Hello.java”:
D:\myProject> javac Hello.java
  1. The compilation is successful if the command prompt returns. Otherwise, error messages will be shown. If this occurs, correct the errors in your source file and recompile. Check Common JDK Installation Errors” if you encounter problems compiling your program.
  2. The output of the compilation is a Java class called “Hello.class”. Issue a dir (List Directory) command again to check for the output.
D:\myProject> dir
......
xx-xxx-xx  01:53 PM               416 Hello.class
xx-xxx-xx  06:25 PM               277 Hello.java
......
  1. Finally, to run the program, invoke the Java Runtime “java”:
:\myProject> java Hello
Hello, world!

Installing Java on Mac OSX

In Step 1, instead of downloading the Windows executable, download the Mac OS version with ‘.dmg’ extension.

Double-click the downloaded Disk Image (DMG) file. Follow the screen instructions to install JDK/JRE. Now, eject the DMG file.

Installing Java on Ubuntu

In Step 1, instead of downloading the Windows executable, download the Ubuntu version with ‘.tar.xz’ extension.

Double-click the TAR file. Follow the screen instructions to install JDK/JRE, and you should be good to go.

I hope this helps.  I know how tedious and uneventful this whole process really is, but the good thing is that once you’ve set it all up, you will be set for as long as you or your machine lives 🙂