Setting Up Your System to Write Android Applications

For those who came late, here is a little bit of history on Android.

Android is a mobile operating system initially developed by Google. The initial release of Android happened on September 23, 2008. Alongside the release, Google also took the initiative to form the Open Handset Alliance (OHA), which is a group of 84 companies including Sony, Dell, Samsung Electronics, HTC, and Motorola to name a few. Note that Android was and is a platform, and the primary purpose of the OHA was to develop open standards for mobile devices and promote the Android platform.

For more information about the Android operating system, visit https://www.android.com/.

Looking at the historical timeline of Android releases, you will realize that Android has really come a long way since 2008:

Figure 5: Android version history timeline; source: www.wikipedia.com
Fun fact—All of the code names for Android flavors are in reference to sweets and the first letter of each flavor name follows an alphabetical order with respect to the next one.

Accompanying Android and the OHA, the Google Play Store was also launched where developers can launch their apps written for the Android platform. Google Play Store also allows developers to collect insightful statistics about their apps and devices active on the play store. In fact, Google itself uses the play store to gather insightful data regarding Android market share and statistics, as shown below:

Figure 6: Insights collected from Google play store; source: developer.android.com

From time to time, Google publishes updated Play Store statistics.

For the latest Play Store statistics visit https://developer.android.com/about/dashboards/index.html.

An important observation from the data presented above is the fact that Marshmallow, Lollipop, and Kitkat are the dominant releases on the Play Store. This can already help you in deciding the minimum sdk version for your next popular app. We will understand the significance of the minimum sdk version as soon as we download Android Studio, which is the default and officially supported IDE for Android development by Google. Navigate to the following link, you should see a DOWNLOAD ANDROID STUDIO button depending on your operating system (Windows, Mac, or Linux), and start the download.

To download Android Studio, visit https://developer.android.com/studio/index.html.

The following are the supported OS versions for Android development:

  • Microsoft® Windows® 7/8/10 (32- or 64-bit)
  • Mac® OS X® 10.10 (Yosemite) or higher, up to 10.12 (Sierra)
  • Linux: GNOME or KDE desktop

A detailed list of requirements including RAM and hard disk requirements is available here under system requirements.

To get information about system requirements for Android development machines, visit https://developer.android.com/studio/index.html.

We'll be covering the development setup for Mac OS here as that corresponds to my personal development environment.

Having a Mac-based development environment also helps, since you can then develop for both Android and iOS on a single system.

But don't let that scare you, there are plenty of tutorials available out there to guide you with the setup for Windows and Linux too. One such comprehensive tutorial is mentioned next.

For additional help in setting up the Android development environment for Mac, Windows, and Linux visit http://www.androidcentral.com/installing-android-sdk-windows-mac-and-linux-tutorial.

Android Studio requires a working version of Java to operate. On a Mac, you need not do anything special as Java is preinstalled (unless you uninstalled it and in that case you can head over to the Oracle website mentioned as follows to download and install the appropriate Java (32/64 bit) for your system).

You can check whether Java is installed on your system or not by running the java -version command on terminal on a Mac machine.

Your copy of Android Studio should have finished downloading by now. Perform the following steps to set up the Android development environment and create a Hello World program. Successful execution of this will eventually prove that our setup is correct:

  1. Double-click on the downloaded .dmg file and in the shown dialog, copy the Android Studio executable to the Applications directory.
  2. After copying, click on the icon and start the set up, you should be immediately prompted with a dialog to import your previous settings. I am assuming that you did not have a previous version of Android Studio installed and hence would not like to import previous settings, therefore choose the option that states so.
  3. After that, it is pretty much straightforward, by clicking on Next and choosing a Standard installation.

If everything goes well, you should be presented with the Android Studio welcome screen as follows:

Figure 7: Android Studio welcome screen

And, as they say, well begun is half done.

Now we shall go ahead and write our first Hello World program on Android:

Fun fact—in the programming jargon, Hello World means success.
  1. Click on the Start a new Android Studio project on the Android Studio welcome screen.

 

  1. This will bring up a dialog where you can choose your project name, package name, and the location of the project on your hard drive, which should be pre-populated.
  2. After naming your application, click on Next, which will bring you to the selection of minimum sdk.

If you remember we ran into the concept of minimum sdk version earlier, and now we will elaborate on it. Minimum SDK refers to the minimum version of Android SDK that an application supports and is all about backward compatibility. A lower value for the minimum sdk version ensures more backwards compatibility with older Android releases, but also requires more effort towards development/coding because some API(s) will be deprecated and not used in newer versions of Android, while others will be new and unavailable in older versions. You will need to make a balanced choice here and to help you with this there is a Help Me Choose button on this screen, which at the time of writing this book in May 2017, showed this visual:

Figure 8: Android Platform Version Distribution

Using the data collected through Google Play Store statistics, Android Studio is helping you make a choice by suggesting Android 4.0, API level 15, which stands at a whopping 97.4% distribution and also looks like a lucrative choice:

  1. We will go ahead with this and choose it as our minimum supported SDK (for now).
  2. Clicking on Next brings you to the choice of activities for your application.
For simplicity of this and this app only, an Activity is a screen. For more information about an Android Activity, visit https://developer.android.com/reference/android/app/Activity.html.
  1. Choose an Empty Activity and click on Next, which should bring you toward naming your activity.
  2. Keep the default values filled in and finish the process.

As soon as you finish, Android Studio should generate the necessary project essentials and boilerplate code for you. If the project setup was completed successfully, you should have something like the following view on your screen:

Figure 9: Initial view of the starter project in Android Studio

Let's take a minute to understand the project structure that was generated. In the left pane is the project structure and in the right pane are the details of the file that is currently selected. The following are the most important files/directories:

  • AndroidManifest.xml: This provides the necessary information about the app to the Android system
  • Source files: The Android apps are primarily programmed in Java. The highlighted source file, MainActivity, in the preceding screenshot is one such source file, which contains the source code of an Activity.
  • layout: This is a directory and contains the layout files. Layout files in android consist of UI elements/views defined in xml format. In the preceding screenshot, activity_main.xml consists of UI corresponding to MainActivity.

Android programming is a pretty vast topic in itself which deserves a book (or a few books) of its own. The Android developer website is one of the best knowledge resources out there for novices and experts alike.

To gather more knowledge on specific topics related to Android, visit https://developer.android.com/training/index.html.

Coming back to our Android project, Android Studio has already generated all the boilerplate code for us and to see this in action all you need to do is enable the developer mode on an Android phone, connect the Android phone (we used an LG Nexus 5 running Android 6.0) via USB to the system and press the green play button. 

For troubleshooting and learning more about running Android apps on physical devices in developer mode, please visit - https://developer.android.com/training/basics/firstapp/running-app.html

We highly recommend using physical Android and iOS devices throughout the course of this book since we will be dealing with Bluetooth functionality, which is not supported on emulators and simulators. If all goes well, then you should be presented with a screen like the following on your Android device:

Figure 10: Hello World, Android

The preceding screenshot says Hello World, which tells us that our Android development environment has been set up successfully.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.184.90