Android Programming for Beginners
上QQ阅读APP看书,第一时间看更新

The development environment

A development environment is a term that refers to having everything you need in order to develop, set up, and be ready to go in one place. We need the following two things to get started:

  • 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.

Tip

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.

A note to the existing developers who use Eclipse

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.

The JDK

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.

What if I already have Java?

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 Java folder.

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.

Installing the JDK

As a little bit of preparation before we install the JDK, you need to know which operating system you have and whether it is 32 or 64 bit. If you are unsure, use this little tip to find out.

Tip

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:

  1. Visit the Java JDK downloads page at http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html.
  2. 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:
  3. 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.
  4. Wait while the JDK is downloaded to your hard drive.
  5. In the folder where you've downloaded the JDK, right-click on the jdk-8u51-windows-x64.exe file 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.
  6. 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:
  7. Now, click on Next and you will see this window:
  8. 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:
  9. 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:
  10. 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.
  11. The next window will not appear on every installation. If you don't see the window pictured as follows, its fine, you can skip to step 13. If you do see the window, proceed to step 12.
  12. 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 Java folder from step 8 and then click on Next.
  13. 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.
  14. Now, you will see the final screen. Click on Next Steps if you are curious, but there is no need because we will be covering Android-specific next steps without delay.
  15. Click on Close, and we are almost done installing the JDK, with just a few more precautionary steps left.
  16. 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:
  17. As shown in the previous screenshot, type JAVA_HOME in Variable name and enter D:\Java\JDK in 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.
  18. Click on OK to save your new settings. Now, click on OK again to clear the Advanced system settings window.

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.

Setting up Android Studio

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.

Tip

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:

  1. Visit Android Studio for Windows button. If at the time of reading this the link has changed, simply Google Download Android Studio.
  2. Next, you will see the Terms and Conditions page as shown in the following screenshot:
  3. Click on the I have read and agree with the above terms and conditions checkbox as highlighted in the previous screenshot.
  4. Now, click on the DOWNLOAD ANDROID STUDIO FOR WINDOWS button. Wait for the download to complete.
  5. Open the folder where you have downloaded Android Studio. Right-click on the android-studio-bundle-141.1980579-windows.exe file 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.
  6. 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.
  7. Let's step through the setup process a window at a time. Pictured next is the first window that you will see when you start the setup process:
  8. Click on Next. Now we can see a few options, as in this next screenshot:
  9. Make sure that all the options have a tick next to them, and then click on Next.
  10. 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:
  11. 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.
  12. 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\sdk as the location. So if, like me, you've installed the JDK on D:, then choose D:\Android\sdk. The next screenshot makes this clear:
  13. Click on Next when you have selected your installation locations.
  14. 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.
  15. 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.
  16. When you see the Installation Complete window, click on Next. Now, you will see the following window:
  17. 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.
  18. 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.
  19. Click on Next when you have chosen your theme.
  20. 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.
  21. When the Downloading Components window has done its work, it will present you with a Finish button. Click on it.
  22. Finally, we are presented with the Welcome to Android Studio screen. This screen, among other things, allows us to start a new project or open an existing project. Take a look at the next screenshot:
  23. You can close this window and take a break or leave it open and read on because we will be back here really soon.

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.