Welcome! In this chapter, we won't waste any time in getting started with developing Android apps.
We will look at what is so great about Android, what Android and Java are exactly, how they work and complement each other, and what this means to us as future developers.
After this, we will spend a little time setting up our development environment and then get straight to building and deploying our first app.
By the end of this chapter, we will have done the following:
Set up the Java Development Kit (JDK), part of the required Android development environment
Installed Android Studio, the final part of our Android development environment
Built our very first Android app
Deployed an Android emulator
Run our app on an Android emulator and a real device
After we write a program in Java for Android, we click on a button to change our code into another form that is understood by Android. This other form is called Dalvik EXecutable (DEX) code, and the transformation process is called compiling. Compiling takes place on the development machine after we click on that button. We will see this work right after we set up our development environment.
Android is a fairly complex system, but you do not need to understand it in depth to be able to make amazing apps. The part of the Android system that executes (runs) our compiled DEX code is called the Dalvik Virtual Machine (DVM). The DVM itself is a piece of software written in another language that runs on a specially adapted version of the Linux operating system. So what the user sees of Android, is itself just an app running on another operating system.
The purpose of the DVM is to hide the complexity and diversity of the hardware and software that Android runs on but, at the same time, its purpose is to expose all of its useful features. This exposing of features generally works in two ways. The DVM itself must have access to the hardware, which it does, but this access must be programmer friendly and easy to use. The way the DVM allows us access is indeed easy to use because of the Android Application Programming Interface (API).
The Android API is the code that makes it really easy to do exceptional things. A simple analogy could be drawn with a machine, perhaps a car. When you step on the accelerator, a whole bunch of things happen under the hood. We don't need to understand about combustion or fuel pumps because a smart engineer has provided an interface for us. In this case, a mechanical interfaceâthe accelerator pedal.
Take the following line of Java code as an example; it will probably look a little intimidating if you are completely new to Android:
However, once you learn that this single line of code searches for the available satellites and then communicates with them in orbit around the Earth while retrieving your precise latitude and longitude on the planet, it is easy to begin to glimpse the power and depth of the Android API in conjunction with the DVM. Even if that code does look a little challenging at the moment, imagine talking to a satellite in some other way!
The Android API is mainly a whole bunch of Java code. So, how do we use all this code to do cool stuff without getting swamped by its complexity? How do we find and manipulate the pedals, steering wheel, and sunroof of the Android API?
There are many different estimates to the number of lines of code that have gone into Android. Some estimates are as low as 1 million, some as high as 20 million. What might seem surprising is that, despite this vast amount of code, Android is known in programming circles for being "lightweight".
Java is a programming language that has been around a lot longer than Android. It is an object-oriented language. This means that it uses the concept of reusable programming objects. If this sounds like technical jargon, another analogy will help. Java enables us and others (such as the Android development team) to write Java code that can be structured based on real-world "things" and, here is the important part, it can be reused.
So, using the car analogy, we could ask the question: if a manufacturer makes more than one car in a day, do they redesign each and every part for each and every car?
The answer, of course, is no. They get highly skilled engineers to develop exactly the right components that are honed, refined, and improved over years. Then, that same component is reused again and again, as well as occasionally improved. Now, if you are going to be picky about my analogy, then you can argue that each of the car's components still have to be built from raw materials using real-life engineers, or robots, and so on.
This is true. What the software engineers actually do when they write their code is build a blueprint for an object. We then create an object from their blueprint using Java code and, once we have that object, we can configure it, use it, combine it with other objects, and more. Furthermore, we can design blueprints and make objects from them as well. The compiler then translates (manufactures) our custom-built creation into DEX code.
Objects in a nutshell
Java is a language that allows us to write code once that can be used over and over again.
This is very useful because it saves us time and allows us to use other people's code to perform tasks we might otherwise not have the time or knowledge to write for ourselves.
Most of the time, we do not even need to see this code or even know how it does its work!
One last analogy. We just need to know how to use that code, just as we only need to learn to drive the car.
So, a smart software engineer up at Google HQ writes a desperately complex Java program that can talk to satellites. He then considers how he can make this code useful to all the Android programmers out there. One of the things he does is he makes features such as getting the device's location in the world a simple one-line task. So the one line of code we saw previously sets many more lines of code in action that we don't see. This is an example of using somebody else's code to make our code infinitely simpler.
We know that to get things done on Android, we write Java code of our own, which also uses the Java code of the Android API. This is then compiled into DEX code and run by the DVM, which in turn has connections to an underlying operating system called Linux.
Then the manufacturers of the Android devices and individual hardware components write advanced software called drivers, which ensure that their hardware (CPU, GPU, GPS receivers, and so on) can run on the underlying Linux operating system.
Our compiled Java code, along with some other resources, is placed in a bundle of files called an Android application package (APK), and this is what the DVM needs to run our app. This process is explained in the following figure:
In summary, all we need to do is learn how to read and code Java, so we can begin to learn and take advantage of the Android API.
All these tools are free, so let's take a look at the development environment we will be using.
We talked a fair bit about compiling our Java code, as well as other people's Java code, into DEX code that will run on the DVM, on people's Android devices. In order to use Java code, we need a free software called the JDK. The JDK also includes other people's code, which is separate from the Android API.
There is a whole range of tools that are required to develop for Android, and we also need the Android API, of course. This whole suite of requirements is collectively known as the Android software development kit (SDK). Fortunately, downloading and installing a single application will give us these things all bundled together. This single application is called Android Studio.
Android Studio is an integrated development environment (IDE) that takes care of all the complexities of compiling our code and linking with the JDK and the Android API. Once we have installed the JDK and Android Studio, we can do everything we need inside this application.
What could possibly go wrong?
I got a lot of feedback via my website http://gamecodeschool.com about a previous book of mine that showed how to install an Android development environment. People said that setting this up can be the hardest part. So I've written this section to be as thorough as possible because not everybody has a smooth setup experience.
Hopefully, you won't need all the extra tips and detailed figures, but there are a few vagaries that can trip us up while setting up the development environment.
Most likely, these instructions will get you up and running quickly and cover all your issues. If there is an issue that I haven't managed to anticipate, don't let it beat you! I guarantee that you are not the first to have that exact issue. Perform a web search (use Google), be really specific in your search criteria, and I am confident you will be coding in no time at all.
This guide will get around 99% of the Windows user's setup in a couple of hours. All the coding and development information you need will be covered 100% step by step.
For Mac and Linux users, most of these instructions can be easily interpreted as the key points of the tutorial are more about what we do inside of the setup programs and less about the specific environment we are using. My apologies for not providing comprehensive instructions for every operating system.
There are multiple options regarding which software to use when developing Android apps. In the early days of developing Android, an IDE called Eclipse was most commonly used. A few years ago, a new "official" contender for the best Android IDE was released. This was Android Studio. The problem after its first release was that it was still in the beta (not finished) stage. It had numerous bugs, including some quite awkward ones. However, even then it was the preferred IDE for many because of its smooth operation, cool looks, official status, and prestigious heritage.
Now that Android Studio has exceeded version 1, there is virtually no reason to use anything else, especially if you are just getting started with Android. If you already have Eclipse set up for Android development and really don't want to change, that's fine, the code in this book will work. However, there will be significant differences in the instructions, especially regarding the user interface (UI) designer. Also, the code in this book has been thoroughly tested by others and me in Android Studio.
The sections that follow will set up a development environment with the JDK and Android Studio as our IDE of choice.
This can be as simple as downloading, double-clicking on the downloaded file, and following the installation instructions. However, sometimes it isn't. So, it is probably worth running through the installation process step by step and pointing out along the way a few options that can make things easier.
Most PC/Mac computers already have Java installed. Many modern apps require Java, and a classic example of this is the game Minecraft. Java is subtly, but significantly, different from the JDK. Java on its own just runs programs that have been written in Java for PC. This is the PC equivalent to the DVM. Java on its own, however, will not compile our code or make other people's code that we need available. This more common version of Java is called the
Java Runtime Environment (JRE). When we install the JDK, it will also install the JRE, whether you already have it or not. So, if you are conscientious about keeping your hard drive in order, you could uninstall the JRE using the Windows Control Panel in the usual way before proceeding. Then find and delete your existing
This tutorial will then reinstall the latest version of the JRE as well as the JDK. If, however, you know that you have a program that uses Java and you don't want to mess with its configuration, then go ahead with this tutorial, but just be aware that there will be a JRE and an associated
Java folder in two places on your hard drive.
To summarize the preceding in case anything is unclear: this tutorial assumes that you don't have an existing folder named
Java, but will work just fine even if you do.
Do I have a 32-bit or 64-bit Windows system?
To find out, right-click on My Computer (This PC on Windows 8) icon, left-click on the Properties option, and look under the System heading of the System type entry like this:
Now we are ready to install the JDK. This fairly simple set of steps will set up the JDK quickly. The only slight delay is the download itself, which could take a while on slower Internet connections. The actual installation process should be fast and trouble free:
Visit the Java JDK downloads page at http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html.
If the link has changed, conduct a web search for Java JDK download. The only potential stumbling block at this stage is that you click on a link for the JRE instead. JDK is what we need. The following is a screenshot of the important part of this page:
Your download page will most likely be slightly different. There will be a newer version of the JDK by the time you read these words, so the 8u51 will be different. That's OK, we just need whichever is the newest version. Simply click on the Accept License Agreement radio button that is highlighted in the previous screenshot and then click on the download link, which is on the right-hand side column corresponding to your operating system in the Product/File Description column. If you are not sure whether you have 32-bit or 64-bit Windows, refer to the tip before this section.
Wait while the JDK is downloaded to your hard drive.
In the folder where you've downloaded the JDK, right-click on the
jdk-8u51-windows-x64.exefile and select Run as administrator. The precise name of the file you have will vary based on whether you have 32-bit or 64-bit Windows and what the current version of the JDK happens to be at the time.
There will be a series of windows that will guide us through the installation process. The most we have to do is just click on Next to proceed. As promised, I will guide you through them one at a time and point out when you might like to make changes or make a note of things. The following is a screenshot of the first window that you will see during the installation:
On the window pictured in the previous screenshot, we can leave all the options at their default values. They are just what we need. The Install to setting is worth considering, however. By the time we install the JDK, Android Studio, and all the extra tools and files that come with it, we will have around 6 gigabytes of files and folders. Now, consider that we will also be making lots of projects throughout the course of this book. Ideally, we want all of these files, folders, and projects to be on the same hard drive. They don't have to be, but we might avoid some problems later on if they are. So, do you have at least 6 gigabytes of space on the hard drive that you've chosen by default? If not, you might like to browse to another folder. In addition to this, as we will see later on in this tutorial, it will be handy (but not essential) to simplify the folder names used to install the JDK. So, click on the Change button and you will see this window:
Browse to the hard drive where you will be installing all of your development tools. Then simplify the names of the folders in which you will install the JDK to just
Java\JDK\. As you can see in the next screenshot, I have also switched to my
D:\drive, as I have more space there:
It actually doesn't matter what you call these folders as long as you remember where they are and what they are called, including whether they are uppercase or lowercase letters. Perhaps you can jot down a note or copy and paste them to a file on your desktop. When you are happy with the chosen installation location and folder names, go ahead and click on Next.
Understandably, this window could cause confusion if you have never done any Java development before. You might be thinking that we had already chosen the destination folder for Java. We chose the location for the JDK. This window refers to the bog-standard Javaâthe JRE. This is what runs Java programs on your PC and, since we are developing for Android, we don't need it. However, we still need to choose a location in order to proceed. Accept the default and just click on Next. If you are a tidiness obsessive like me, you can change to the same
Javafolder from step 8 and then click on Next.
Next, you will see the window that says 3 Billion Devices Run Java. Android has over 1.1 billion devices alone at the time of writing this despite a slow start in 2015. You might also like to know that there are more than 18,000 different distinct devices. Seriously, Google it if you think it sounds crazy. You can do this while you wait for the installation to complete.
Click on Close, and we are almost done installing the JDK, with just a few more precautionary steps left.
Now, we will make sure that Windows (and all its applications) know where to find the JDK. Right-click on your My Computer (This PC on Windows 8) icon and Properties | Advanced system settings | Environment Variables | New (under System variables and not under User variables). Now you can see the New System Variable dialog as follows:
As shown in the previous screenshot, type
JAVA_HOMEin Variable name and enter
D:\Java\JDKin the Variable value field. If you've installed the JDK somewhere else, then the file path you enter in the Variable value field will need to point to where you've placed it. Be sure to type it correctly, make sure that the slashes
\are the right way around, and don't add any extra slashes.
We have successfully installed the JDK, which we need to develop Android apps with Android Studio, and we have also installed the JRE, if it wasn't installed already, which we won't be using, but this won't cause us any problems either.
Now that the JDK is installed and ready to go, we are only one step away from building our first Android app. Installing Android Studio can take a bit longer than the JDK and is a little more nuanced, but it is nothing a determined, aspiring, developer won't be able to handle with ease.
What could possibly go wrong?
This whole process could take an hour or two. Not because there is lots of work for us to do, but because we need to initiate some fairly large downloads. Also, at several different stages in the process, Android Studio will connect to the internet and update itself. If you have a fast internet connection, then you can probably knock a good percentage off of my rough estimate of the time required.
Now that we know what to expect, we can get on with the installation of Android Studio. Follow the given steps to do so. There is nothing especially tricky about this, and the few aspects that might need some consideration will be discussed as they arise:
Visit https://developer.android.com/sdk/index.html and click on the Download Android Studio for Windows button. If at the time of reading this the link has changed, simply Google
Download Android Studio.
Click on the I have read and agree with the above terms and conditions checkbox as highlighted in the previous screenshot.
Now, click on the DOWNLOAD ANDROID STUDIO FOR WINDOWS button. Wait for the download to complete.
Open the folder where you have downloaded Android Studio. Right-click on the
android-studio-bundle-141.1980579-windows.exefile and select Run as administrator. Your file will most likely have a different name based on whichever is the current version of Android Studio at the time.
When you get the Do you want the following program to be allowed to make changes to this computer message, click on Yes. You will see the first window of the installation process.
Make sure that all the options have a tick next to them, and then click on Next.
The next window is the license agreement. Click on I Agree and you will see some settings that warrant a short discussion. Take a look at the next screenshot that shows you the Install Locations window:
In this step, we want to install the Android Studio IDE and Android SDK to the same hard drive where we installed the JDK. So you might just be able to click on Next at this point. However, if you've installed the JDK to another drive, then we need to change the drive and the folders we use at this step too. This isn't strictly essential, but it can avoid problems for some users.
For Android Studio Installation Location, choose the root of the drive where you've installed the JDK followed by
\Android Studio. So in my case, this will be
D:\Android Studio. For Android SDK Installation Location, choose the same hard drive and simply add
Android\sdkas the location. So if, like me, you've installed the JDK on
D:, then choose
D:\Android\sdk. The next screenshot makes this clear:
Click on Next when you have selected your installation locations.
Next, you might see the Emulator Setup window as pictured in the next figure. If you do, then accept the default settings and click on Next; otherwise, you can skip to step 15. Don't worry if you don't see this screen, it is a minor issue to do with running the Android emulators a bit more smoothly. Most of the time, you will probably want to use a real device anyway.
We are nearly there now. The next window asks you to choose a start menu folder, just as when we install any new Windows app. You might want to make a note of this location. Click on Install to accept the default settings, and Android Studio will begin to install itself and extract the SDK to the appropriate folder that we selected earlier. This might take some time.
Click on Finish to bring up the second to last window of the installation process. Assuming that this is your first time using Android Studio, click on the I do not have a previous version of Android Studio or I do not want to import my settings radio button and click on OK.
Now, in the next figure, you get to choose the theme that Android Studio will use. If you like a conventional black text on white background appearance, then choose IntelliJ, and if you want a cool dark style, choose Darcula. You can alter any of these schemes from within Android Studio if you change your mind later.
Click on Next when you have chosen your theme.
Now Android Studio will connect to the Internet and download some of the Android tools that we will be using soon. Again, this could take a while.
When the Downloading Components window has done its work, it will present you with a Finish button. Click on it.
This was a fairly lengthy tutorial but, hopefully, it wasn't too tough. Android Studio and the supporting tools that we need are installed and ready to go. We are really close now to building our first app.
First, let's look at the composition of an Android app a little.
We already know that we will write Java code that will itself use other people's Java code and will be compiled into DEX code that runs on the DVM. In addition to this, we will also be adding and editing other files as well. These files are known as Android resources.
Our app will include resources such as images, sounds, and user interface layouts that are kept in separate files from the Java code. We will slowly introduce ourselves to them over the course of this book.
They will also include files that contain the textual content of our app. It is a convention to refer to the text in our app through separate files because it makes them easy to change, and this makes it easy to create apps that work for multiple different languages.
Furthermore, the actual UI layouts of our apps, despite the option to implement them with a visual designer, are actually read from text-based files by Android.
Android (or any computer), of course, cannot read and recognize text in the same way that a human can. Therefore, we must present our resources in a highly organized and predefined manner. To do so, we will use Extensible Markup Language (XML). XML is a huge topic but, fortunately, its whole purpose is to be both human and machine readable. We do not need to learn this language, we just need to observe (and then conform to) a few rules. Furthermore, most of the time when we interact with XML, we will do so through a neat visual editor provided by Android Studio. We can tell when we are dealing with an XML resource because the filename will end with the
You do not need to memorize this, as we will constantly be returning to this concept throughout the book.
In addition to these resources, it is worth noting that Java, as used in Android, has a structure to its code. There are many millions of lines of code that we can take advantage of. This code will obviously need to be organized in a way that makes it easy to find and refer to. It is organized under predefined packages that are specific to Android.
Whenever we create a new Android app, we will choose a unique name known as a package. We will see how to do this in the Our first Android app section. Packages are often separated into subpackages, so they can be grouped together with other similar packages. We can simply think of these as folders and subfolders.
We can also think of all the packages that the Android API makes available to us as books that contain code, from a library. Some common Android packages we will use include the following:
As you can see, they are arranged and named to make what is contained in them as obvious as possible.
If you want to get an idea for the sheer depth and breadth of the Android API, then take a look at the Android package index at http://developer.android.com/reference/packages.html.
Earlier, we learned that reusable code blueprints that we can transform into objects are called classes. Classes are contained in these packages. We will see in our very first app how to easily import other people's packages along with specific classes from those packages for use in our projects. A class will almost always be contained in its own file, with the same name as the class, and have the
.java file extension.
In Java, we further break up our classes into sections that perform the different actions for our class. We call these sections methods. These are, most often, the methods of the class that we will use to access the functionality provided within all those millions of lines of code. We do not need to read the code. We just need to know which class does what we need, which package it is in, and which methods from within the class give us precisely the results we are after.
The next diagram shows a representation of the Android API. We can think about the structure of the code that we will write in exactly the same way, although we will most likely have just one package per app. Of course, because of the object-oriented nature of Java, we will only be using selective parts from this API. Also note that each class has its own distinct data. Typically, if you want access to the data in a class, you need to have an object of that class.
By the end of this chapter, we will have imported multiple packages and some classes from them and we will have used other people's methods. By the end of Chapter 2, Java â First Contact, we will have even written our very own methods as well.
In programming, it is a tradition for the first app of a new student to use whichever language/OS they are using to say hello to the world. We will quickly build an app that does just that, and in Chapter 2, Java â First Contact, we will go beyond this and add some buttons that actually respond to the user.
We will start with the creation of this project. What follows here are the steps to create a new project. My instructions at each step might seem a little verbose, but that is just because this is the first time we are running through them. After a few chapters, when we create new projects, I will be able to describe the instructions in a sentence and you will be able to create the project in 30 seconds. As this is the first one, let's do this step by step and learn it all along the way:
Start Android Studio by clicking on its icon in the start menu folder that you chose in step 15 of the previous tutorial.
Now, you will see the Welcome to Android Studio start menu that we saw at the end of the previous tutorial. Go ahead and click on Start a new Android Studio project. You will see the New Project screen pictured next. Starting a new project is very easy and mainly involves accepting the default settings on a few different screens. Let's take a closer look at some of the options, however, because they will increase our understanding of Android and Android Studio:
First, note that we can name our application in the Application name field. You can choose any name you like for your first application, but just be aware that Android Studio will soon generate a whole bunch of code and files for us and that the name you choose will be reflected in them. If you want your code and files to be identical to those that we will be examining shortly, call your application
Next, note the Company Domain field. This is where you will enter the details of your company. It is a convention and very practical (because it is unique) to use the domain name of your company website. If you don't have a company or domain name, enter the domain name I am using:
gamecodeschool.com. Unlike the application name, using a different company domain will have almost zero effect on the code and files that we will examine later. Although if you are using your own domain name, do refer to the tip Using your own domain name or application name, after this tutorial. Choose and enter a company domain name.
Now, look at the Package name field. It has been automatically derived from the previous two fields. If you used the same two values as suggested in the previous two steps, your package name will be
com.gamecodeschool.helloandroid. Remember that a package is a collection of the Java classes that are our code files, and our apps can comprise one or more packages if we wish them to, but they must comprise at least one package. We can edit the package name by clicking on the edit link, but we have no need to do so here.
Finally, in the Project location field, you can accept the default settings or browse to the location where you would like to store all your Android projects. You have the option to change this for each project that you create. I've put personal projects in my
Dropboxfolder so, if my PC breaks down mid project, I know my files are safe. Android Studio can also work from an online code repository, such as GitHub, but we are not covering that in this book. Here is a screenshot of the New project window after steps 5, 6, and 7:
Click on the Next button to continue and you will see the Target Android Devices window:
Here, we can see that we have the option of developing an application for a phone, tablet, TV, and wear. Wear is the range of Android-enabled smartwatches. In the preceding screenshot, in the grayed-out part at the bottom of the window, we can also see that we can develop for Glass, Google's trendy Android-enabled glasses, although we would need to install additional files to do this. In this book, we will be developing for phones and tablets, so the already selected option is just what we need. The only other thing we need to choose on this screen is the Minimum SDK. We already know that the Android SDK is a collection of packages of code that we will be using to develop our apps. Like any good SDK, the Android SDK is regularly updated, and each time it gets a significant update, the version number is increased. Simply put, the higher the version number, the newer the features you get to use; the lower the version number, the more devices our app will work on. For now, the default setting API 15: Android 4.0.3 (IceCreamSandwich) will give us lots of great features and at least 90% compatibility with the Android devices that are currently in use. If, at the time of reading this, Android Studio is suggesting a newer API, then go with it.
Click on the Next button. Now we can see the Add an activity to Mobile window:
In the previous sections, we discussed Java classes in brief. An
Activityclass is a special Java class and every Android app must have at least one. It is the part of the code where our app will start when it is launched by the user, and this also handles any interaction with the user. The options on this screen provide different ready-made templates of the
Activityclass code in order to give programmers a fast start when creating various types of apps. As we are starting from scratch, the most appropriate option for us is Blank Activity. Make sure that Blank Activity is selected by clicking on it and then clicking on Next. Now, take a look at the Customize the Activity window:
The Customize the Activity window gives us four things that we can play with. It is perfectly possible to leave them all at their default settings, but let's change them to make them more meaningful and discuss what each one is. Activity Name is the name of the class that will contain our code. If this sounds odd, to be more specific, we will have a class that is an activity called whatever we name it here. Name the activity
The next field is Layout Name. We discussed earlier in this chapter that Android UI layouts are usually defined in a separate XML text file, not with our Java code. The layout name is what this file will be called. Name the layout
The next field is Title. This is different than the Activity Name field and will be used by Android on the device's screen as the name of the app. Name the title
Finally for this screen, we have Menu Resource Name. Menus are the pop-up options that you get on Android when (perhaps unsurprisingly) you click on the menu button. They might also be shown on the topmost bar of the app known as the action bar. This varies depending on the version of Android a device is running. These are considered to be part of the UI as well and are usually defined in this separate XML file. Name our menu file
Click on the Finish button, and Android Studio will now create a project for us based on our choices from the previous 16 steps.
We now have a project ready for us to deploy or modify.
Using your own domain name and application name
Using your own domain and application name is absolutely fine. The only thing to be aware of is that the first line of code that we will see when we look at our Java code (in Chapter 2, Java â First Contact), will be, and needs to be, different. This is the package name. In the context of this book, we won't ever need to change the package name or do anything with it, so it doesn't matter what you call your apps or whether you use your own domain name.
The only time this could cause a problem is when you create a project (with a different name or package name) and then copy and paste the code from the download bundle. The reason for this is that this code will have a different package name. All you need to do to overcome this problem is either create your project with the same package name as the code files you are copying and pasting from or change the very first line of code, after you have copied and pasted it, to reflect the package name of the project you've created.
In this book, we will be covering everything you need to know to publish professional apps. One thing we will not be discussing is working collaboratively as part of a team or any backup strategies for your code. If you only intend to build apps on your own, then you don't need to know any more. However, Android Studio works almost seamlessly with a number of different Version Control Systems (VCS) and code repositories. VCS and their related code repositories have a number of advantages, which are as follows:
You can allow multiple programmers to contribute to the same project and even the same file
You can click on a button to roll back your work to a specified position if you realize you have gone wrong
You can store your code seamlessly in a repository on the cloud/Web and work on your project from any development machine without any concern about backing up the files
Achieving all these benefits is not complicated and, in some cases, it is even free. The problem with introducing VCS in a book like this is that there are an awful lot of options and there is no simple, universally best option that will suit everybody. In addition, it introduces yet another hurdle before we can get down to the business of making apps.
If the topic of version control sounds interesting, try exploring these two providers:
Now, you might be surprised to know that we can actually deploy our app to an emulator or a real Android device. Throughout this book, all the code for each project is supplied in the download bundle in a folder that reflects the chapter number. There is no code, however, in the
Chapter 1 folder because all the code in this project has been automatically generated by Android Studio and we don't need to type anything for this project. We will, however, examine, modify, and add to this autogenerated code in the next chapter.
We need to test our apps frequently throughout development to check for any errors, crashes, or anything else unintended. It is also important to make sure that the app looks good and runs correctly on every device type/size that you want to target. Clearly, we do not own each of the many thousands of Android devices. This is where emulators come in.
Emulators, however, are sometimes a bit slow and cumbersome, although they have improved a lot recently. If we want to get a genuine feel of the experience that our user will get, then nothing beats deploying an app to a real device. So we will probably want to use both real devices and emulators while developing our apps.
Deploying to an emulator is a matter of a few mouse clicks. There is a ready-made emulator that is included with Android Studio. At the time of writing this, it is Nexus 5. This may well be updated to a newer device by the time you read this. The basic functionality described here will likely be identical, but the device name and screenshots might vary slightly from your experience:
Just below the menu bar is a row of neat-looking buttons that we will explore as the book progresses. The button we want right now is the green triangle that represents play. Identify the button using the next screenshot and click on it:
Next, you will see the Choose Device window. Click on the Launch emulator option as shown in the next screenshot:
Click on OK to launch the emulator. Nexus 5 comes preconfigured when you install Android Studio, but you can also choose and customize emulators yourself. Refer to the tip Creating a new emulator after this tutorial.
The emulator might take some time to start! Sometimes, it might even take 5 minutes or more. This is partly dependent on whether Android Studio has detected whether your PC can run HAXM (Hardware Acceleration Execution Manager) during the setup phase or not. Either way, all the tutorials in this book will work just fine. Wait for the emulator to start. When your emulator has loaded, you will see something like the next screenshot:
We need to simulate swiping a finger up on the screen to unlock. So click and drag up from somewhere near the bottom on the emulator screen. You can now see your first app in action, as shown in the following screenshot:
Note that you can see the launcher icon for your app with the name you gave it when we created the project. Obviously, we will need to replace the default Android icon before we release our app into the world. This will do for now though.
You can click on the launcher icon to run the app again or try running any of the other apps. Play with the emulator for a bit.
Emulators take time to start up
If you are planning to use the emulator again soon, then leave it running in order to avoid having to wait for it to start again.
If you want to try out your app on a tablet, TV, or even a watch, you're going to need a different emulator.
Creating a new emulator
If you want to create an emulator for a different Android device, this is really simple. From the main menu, navigate to Tools | Android | AVD Manager. On the AVD Manager window, click on Create New Virtual Device. Now click on the type of device you want to create: TV, Phone, Wear, or Tablet. Now simply click on Next and follow the instructions to create your new AVD. The next time you run your app, the new AVD will appear as an option to run the app on.
Now we can take a look at how to get our app onto a real device.
Many of the more recent Android devices don't need any drivers at all. You could try simply plugging it in!
The next few steps will set up the Android device for debugging. Note that different manufacturers structure the menu options slightly differently to others. However, the following sequence is probably very close, if not exact, to enable debugging on most devices:
Click on the Settings menu option or the Settings app.
Click on Developer options.
Tick the checkbox for USB Debugging.
Connect your Android device to the USB port of your development system. You can see in the following figure that on the Devices | Logcat subtab of the Android tab, at the bottom of the Android Studio window, my Samsung GT I9100 has been detected:
Click on the Play icon from the Android Studio toolbar, just as we did when running the app on the emulator.
When the Choose Device window appears, click on Choose running device and then on your device; in my case, this would be Samsung GT I9100.
Finally, click on OK to debug the app on your chosen device.
Note that although the app is running on a real device, this is still running in the debug mode so we can get feedback about what is happening to our app. In Chapter 29, Publishing Apps, we will see how to generate a signed, distributable app that can be uploaded to Google Play.
So is Android not really an operating system, but just a virtual machine? And are all phones and tablets really Linux machines?
No, all the different subsystems of an Android device, which includes Linux, the DVM, and the libraries and drivers together, are what make up the Android operating system.
I still don't understand all of these technical terms, such as DVM, object-oriented, APK, and so on. Should I reread this chapter?
No, that isn't necessary, as we just need to introduce this jargon, and we will be revisiting them all as well as clarifying them as the book progresses. You just need to understand the following:
We will be writing Java code and creating other resources
Android Studio, with the help of the JDK, will turn this code and these resources into real Android apps
Now, you are good to go and can move on to Chapter 2, Java â First Contact.
So far, we have set up an Android development environment and created and deployed an app on both an emulator and a real device. If you still have unanswered questions (and you probably have more than at the start of this chapter), don't worry because as we dig deeper into the world of Android and Java, things will become clearer. As the chapters progress, you will build a very rounded understanding of how everything fits together, and then success will just be a matter of practice and digging deeper into the Android API.
In the next chapter, we will edit the UI using the visual designer and raw XML code, write our first Java methods, and get to use some methods provided to us by the Android API.