When someone looks for Android in Internet, the first thing that comes up is the Google’s Operative System. Android controlled 88% of the world smartphone market in 2016 and over 1.4 billion people are now using Android. Based on that we can see the magnitude and the potential of this technology, that’s why I wanted to show you how to be part of this growing market by teaching you how to build an Android app and of course, not crash in the attempt.
First of all, let’s understand what I’m going to show you here. This post is aimed for those who have at least experience in Java and Object Oriented Programming. Why? Because Android uses Java as a programming language (we can also use another programming language like Kotlin, C++ or even C#, but that could be in another tutorial) and the core values of OOP. If you don’t meet the requirements you can find several good tutorials for Java and OOP on the web and then come back and join us on this trip.
Also, I won’t overwhelm you with a lot of theory and diagrams (just a few very important ones), instead, we’ll learn by doing stuff. These are the things we will do in this tutorial:
- Learn how to create an interface and handle events.
- Consume a RESTful API and parse it.
- Android best practices.
We will achieve this by following this tutorial:
As you can notice, with everything you will learn in this tutorial you’ll be capable of making more robust apps, that’s why I didn’t want to teach you how to make a “hello world” or a calculator app.
So, let’s get started!
The Integration Development Environment and the core language
As we mentioned before we will be using Java for this tutorial, so in order to run our code we need the Java Development Kit, you can download it from here.
Once you have installed the JDK we can now install our Integration Development Environment or IDE. Google provides an IDE called Android Studio to develop Android applications and we will be using it in this tutorial. Android applications require several Android specific configuration files and this IDE will ease all the process for us.
System requirements
In order to develop for Android and to do it with a good experience, the official website recommends the requirements listed below:Download Android Studio from from here. Once you have it installed we can continue with the setup.
Creating a project with Android Studio
Now that we already set up our environment we can create the Android project (finally!). For that, you just have to select “Start a new Android Studio project”:
We will see now a view as shown below, please use the following data of input for your project. Project location and package name are derived from your input. If you want another package name, press the small Edit hyperlink.
- Application name: MyFirstApp
- Company Domain: example.com
When you are done filling the fields click on Next. Then, you have to choose what is the minimum SDK you will use for your project, this means the minimum supported version in which your app can run.
I recommend to use the API 19 (Kit Kat), with that one, you will target to 73.9% of the active devices on the Play Store. Keep in mind that below the API means less support for newest features.
Afterward select the Empty Activity template.
On the last page, ensure that “Backwards Compatibility” is selected. This means we will be able to have the newest key features of Android even in backward versions to lollipop (API 21), like material design ones.
Checking the generated project
Android will generate the project and afterward you will see something like this:
As you can notice, we have the project structure on the left side and the open files in the right one. Keep in mind this project structure is not the actual one; in order to see the real project structure and files you need to change to the Project view:
Now you will see something like this:
So let’s take a look to some of these folders and files and check its purpose on this project. We will focus in the app folder for now on, the other files and folders are generated by the project and are important for it to work, but that folder mentioned before is where we will be editing/creating files for our app.
The source folder (src)
You can find this folder inside the app one. This folder is where our magic will be inside of.
androidTest
This folder contains all the instrumented tests classes for the app.
test
Here, we put all the Unit Tests classes.
main
This folder contains basically our app code. Here we have from java classes to images. Let’s check the folders that are inside:
java: Contains the packages of our code. Inside the packages we have the java classes.
res: This folder has static resources like images and configuration files (XML). The specific sub-folder depends on type of resource which is stored. You can also append resource qualifiers to the folder name, they indicate that the related resources should be used for special device configurations.
The following table provides an overview of the supported resources and their standard folder prefixes.
Resource |
Description |
drawable | XML files or images accordingly with the density of the Android device. |
layout | XML files with layout descriptions to define the user interface. |
mipmap | Pre-calculated, optimized sequences of images, each of which is a progressively lower resolution representation of the same image. It’s best practice to place your app icons in mipmap- folders (not the drawable- folders) because they are used at resolutions different from the device’s current density. |
values | Used to define strings, colors, dimensions or styles. |
NOTE: We won’t review files in folders androidTest and test throughout this tutorial since we are not developing using the Test Driven Development process, but keep in mind that testing is very important and for this tutorial I don’t want to overwhelm you with that too (we can make another tutorial for testing though).
The manifest file
The components, settings, and metadata of an Android application are described in the AndroidManifest.xml file. This file can be found inside the main folder. Activities, services and content provider components of the application must be statically declared in this file. Some of these components can be described as:
- The <application> tag section allows to define metadata for your application and optionally define an explicit application class. It’s also a container for declaring other Android components.
- The <activity> tag defines an activity. The name attribute points to class, which is relative to the package defined in the ‘package’ attribute.
- You can declare a required permission with the <uses-permission> tag. Certain permissions must be confirmed by the users to become active above API 22 (marshmallow).
The build.gradle file
You can notice there are two similar files, one outside the app folder and the other one inside. That last one allows us to:
- Add or modify the manifest file at build time (like the application version, which is typically supplied by this file).
- Set the defaultConfig. This section in the gradle allows you to specify the minSdkVersion (the minimum version of Android your application works on) and targetSdkVersion (the version on which you tested and developed) of your application. Also, you can set the versionNameand versionCode, these specify the version of the application (versionName is what the user sees and can be any string). The versionCode must be an integer. The Android Market determines whether it should perform an update of the applications for the existing installation based on that versionCode. You typically start with “1” and increase this value by one if you roll-out a new version of your application. Define the applicationId. This defines the base package for the Java objects referred to in this file. If a Java object lies within a different package, it must be declared with the full qualified package name. Google Play requires that every Android application uses its own unique package name. Therefore, it’s a good habit to use your reverse domain name here. This will avoid collisions with other Android applications.
- Define the buildTypes. Here we can define any Android build variation including its own rules, like to minify your code or use certain proguard configurations. The default type is release but you can have a debug one for development.
- Add dependencies. This part of the gradle let us define external libraries which can be used in our app. These libraries will be incorporated by Gradle.
With all this explanation we are ready to go and start programming our app (finally!). In the next episode we will learn how to parse a JSON response into Java objects (GSON) in a couple minutes and use that information to create our app with some Android widgets and material design. I hope this information can be useful for you to at least know how to setup your Android workspace and be ready to jump into the mobile development. See you in the next post.