The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.
When you work on creating an app for Android, you will do so by working in a “project”. The project is a directory containing your source code and other files, like images and UI definitions. Your IDE or other build tools will take what is in your project and generate an Android app (APK) as output.
The details of how you get started with a project vary based upon what IDE you are using, so this chapter goes through the various possibilities.
The various ways we set up Android projects have some common elements.
The “Application Name” is the initial name of your project as seen by the user, in places like your home screen launcher icon and the list of installed applications.
The “Project Name” is the name of the project as it is represented inside of the IDE. As you type in an application name, the project name will automatically be filled in to match the application name, with whitespace and other invalid characters removed. Of course, you can change this as you see fit. In the case of Android Studio, the project name also forms the name of the directory that will hold the project.
The “Package Name” refers to a Java package name (e.g.,
package name will be used for generating some Java source code, and it also is used as
a unique identifier of this package, as was mentioned earlier in this book.
The “Minimum Required SDK” refers to how far back in Android’s version history you are willing to support. The lower the value you specify here, the more Android devices can run your app, but the more work you will have to do to test whether your app really does support those devices. Nowadays, for new development, a minimum required SDK of 15 is reasonable, and you can change your chosen value later on if needed.
The “Target SDK”, roughly speaking, is the version of Android you were thinking of when you were writing the code for this app. Usually, you will set this to be the latest shipping Android API level, then change it over time as new versions of Android are released and you decide that you are ready for some of those changes. We will be exploring the ramifications of target SDK versions throughout the book.
The “Compile With” (a.k.a., “build SDK” or
is the version of Android whose classes and methods you want to compile against.
This can be newer than the minimum required SDK, and it often is newer. On newer devices running
newer versions of Android, you might want to take advantage of some new features, and you will
“route around” that code on older devices to maintain backwards compatibility. Hence, typically,
your build SDK is set to a fairly new version of Android, certainly one new enough to support all
of the classes and methods from the Android SDK that you want to use. Note that to set
this to API Level 21 or higher, you will need to be using Java 7 or higher for your
The “Theme” is a general statement of the look and feel of your app, particularly in terms of color scheme. The current default (“Holo Light with Dark Action Bar”) means that the body of your UI will be dark text on a light background, except for the “action bar” across the top, which will be light text on a dark background. You will be able to create your own custom themes, overriding various characteristics from one of these stock themes, to set up your own color scheme and the like. We will explore that process later in the book.
You may have chosen to use Android Studio as your IDE.
With Android Studio, to work on a project, you can either create a new project from scratch, you can copy an existing Android Studio project to a new one, or you can import an existing Android project into Android Studio. The following sections will review the steps needed for each of these.
You can create a project from one of two places:
This brings up the new-project wizard:
Figure 15: Android Studio Create-Project Wizard, First Page
The first page of the wizard is where you can specify:
By default, the package name will be made up of two pieces:
If this is not what you want, click the tiny “Edit” link on the far right side of the proposed package name, which will now allow you to edit the package name directly:
Figure 16: Android Studio Create-Project Wizard, First Page, with Editable Package Name
Clicking “Next” will advance you to a wizard page where you indicate what sort of project you are creating, in terms of intended device type (phones/tablets, TVs, etc.) and minimum required SDK level:
Figure 17: Android Studio Create-Project Wizard, Second Page
Developers just starting out on Android should only check “Phone and Tablet” as the device type. The default “Minimum SDK” value also usually is a good choice, and it can be changed readily in your project, as we will see later in the book.
Clicking “Next” advances you to the third page of the wizard, where you can choose if Android Studio should create an initial activity for you, and if so, based on what template:
Figure 18: Android Studio Create-Project Wizard, Third Page
None of these templates are especially good, as they add a lot of example material that you will wind up replacing. “Empty Activity” is the best of the available options for first-time Android developers, simply because it adds the least amount of this “cruft”.
If you choose any option other than “Add No Activity”, clicking “Next” will advance you to a page in the wizard where you can provide additional details about the activity to be created:
Figure 19: Android Studio Create-Project Wizard, Fourth Page
What options appear here will vary based upon the template you chose in the previous page. Common options include “Activity Name” (the name of the Java class for your activity) and “Layout Name” (the base name of an XML file that will contain a UI definition of your activity).
The “Backwards Compatibility (AppCompat)” checkbox indicates if you want to use a library known as AppCompat. We will discuss using libraries later in the book, as well as what this “AppCompat” is. Unless you know for certain that you want to use AppCompat — and few of this book’s example apps do — uncheck this checkbox.
Clicking “Finish” will generate your project files.
Android Studio projects are simply directories of files, with no special metadata held elsewhere. Hence, to copy a project, just copy its directory.
You can import a project from one of two places:
Then, choose the directory containing the project to be imported.
What happens now depends upon the nature of the project. If the project was already set up for use with Android Studio, or at least with Gradle for Android, the Android Studio-specific files will be created (or updated) in the project directory.
However, if the project was not set up for Android Studio or Gradle for Android, but does
have Eclipse project files (or at least a
you will be led through an Eclipse import wizard.
The first page of that wizard is where you specify where Android Studio should make a copy of the project, so it does not modify anything in the original directory:
Figure 20: Android Studio Eclipse Import Wizard, First Page
Clicking “Next” will bring up a page where you can configure some automatic fixes that the import wizard will apply to the imported project code. The details of what is going on here are well past what we have covered so far in the book. Normally, the defaults are fine.
Figure 21: Android Studio Eclipse Import Wizard, Second Page
Clicking “Finish” will perform the project conversion. Android Studio will
open up an
import-summary.txt file outlining some details of how the conversion
was accomplished. At this point, the copied-and-modified project is ready for use.
The preview of this section was traded for a bag of magic beans.