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.
The Android build system is organized around a specific directory tree structure for your Android project, much like any other Java project. The specifics, though, are fairly unique to Android — the Android build tools do a few extra things to prepare the actual application that will run on the device or emulator.
Here is a quick primer on the project structure, to help you make sense of it all, particularly for the sample code referenced in this book.
The details of exactly what files are in your project depend on a variety of things:
However, usually, there are many elements in common.
A default Android Studio project will have an
app/ directory off of the
project root directory (i.e., the directory in which you told Android Studio
to create the project). That
app/ directory represents what Android Studio
refers to as a module. Most projects just have this one
app/ module, though
you can have more than one module if needed.
app/ you will find a
src/main/ directory. This represents the
main source set, and it contains all of the “source” that your project is
contributing to build the application. Here, “source” means more than just
programming language source code (e.g., Java). It also includes other types
of files, such as resources and the manifest, that contribute to the app.
app/src/main/, you will find a file named
AndroidManifest.xml is an XML file describing the application being
built and what components — activities, services, etc. — are being
supplied by that application. You can think of it as being the “table of
contents” of what your application is about, much as a book has a “table
of contents” listing the various parts, chapters, and appendices that appear
in the book.
We will examine the manifest a bit more closely starting in the next chapter.
When you created the project, if you had Android Studio create an activity
for you, you
supplied the class name of the “main” activity for the
MainActivity). That will be combined with the package name
that you provided for the project to determine the fully-qualified class
name for this activity (e.g.,
You will find that
your project’s Java source tree already has the package’s directory tree in place,
plus a class representing your requested activity (e.g.,
com/commonsware/android/MainActivity.java). This will be inside the
main source set, so the full path from the project root would be something
You are welcome to modify this file and add Java classes as needed to implement your application, and we will demonstrate that countless times as we progress through this book.
Elsewhere — in directories that you normally do not work with — the Android
build tools will also be code-generating some source code for you each time you
build your app. One of the code-generated Java classes (
R.java) will be
important for controlling our user interfaces from our own Java code, and we will
see many references to this
R class as we start building applications in earnest.
You will also find that your project has a
res/ directory tree inside of
main source set. This holds
“resources” — static files that are packaged along with your application,
either in their original form or, occasionally, in a preprocessed form. Some of
the subdirectories you will find or create under
res/mipmap-*/(for a few values of
*) for images (PNG, JPEG, etc.)
res/layout/for XML-based UI layout specifications
res/menu/for XML-based menu specifications
res/raw/for general-purpose files (e.g., an audio clip, a CSV file of account information)
res/values/for strings, dimensions, and the like
res/xml/for other general-purpose XML files you wish to ship
Some of the directory names may have suffixes, like
indicates that the directory of resources should only be used in certain
circumstances — in this case, the mipmap resources are designed for
devices with high-density screens.
We will cover all of these, and more, later in this book.
The IDE needs to know how to take all of this stuff and come up with an Android APK file. Some of this is already “known” to the IDE based upon how the IDE was written. But some details are things that you may need to configure from time to time, and so those details are stored in files that you will edit, by one means or another, from your IDE.
In Android Studio, most of this knowledge is kept in files
build.gradle. These are for a build engine known as Gradle,
that Android Studio uses to build APKs and other Android outputs.
All of those items are stored in a particular directory structure in an Android Studio project… at least by default. Android Studio and Gradle are powerful and can be configured to handle other structures, though most projects stick with the standard setup.
In the root directory of your project, the most important item is the
app/ directory representing your application module. We will look at that more
in the next section.
app/ directory, the other noteworthy files in the root of your
build.gradle, which is part of the build instructions for your project, as is described above
gradlew.bat, and so forth)
local.properties, which indicates where your Android SDK tools reside
.imlfile, where Android Studio holds some additional metadata about your project
.gitignorefile, representing standard rules for what should and should not go into version control, particularly with respect to the Git version control system
Eventually, you will have:
build/directory, containing the compiled output of your app, plus various reports and other files related to the build process and app testing
.gradle/directory, containing Gradle executable code
.idea/directory — this, along with the
.imlfile, represents data needed by IntelliJ IDEA, on which Android Studio is based
app/ directory, and its contents, are where you will spend most of
your time as a developer. Only infrequently do you need to manipulate the files in
the project root.
The most important thing in the
app/ directory is the
which is the root of your project’s source sets. We will explore those more
in the next section.
src/ directory, there are a few other items of note in
build/directory, which will hold the outputs of building your app, including your APK file
build.gradlefile, where most of your project-specific Gradle configuration will go, to teach Android Studio how to build your app
app.imlfile, containing more Android Studio metadata
.gitignorefile, for overrides to the Git version control rules from the
.gitignorefile in the project root
proguard-rules.pro, which are rules for a tool called ProGuard, which helps reduce the size of your app
Source sets are where the “source” of your project is organized. As noted earlier, “source” not only refers to programming language source code (e.g., Java), but other types of inputs to the build, such as your resources.
The source set that you will spend most of your time in is
may also have stub source sets named
test, for use in testing
your app, as will be covered later in the book.
Inside of a source set, common items include:
assets/directory, representing other static files you wish packaged with the application for deployment onto the device
Figure 46: Android Studio Project Explorer, Showing EmPubLite
Some projects will have additional items, usually tied to specific add-on tools that the project happens to employ.
The preview of this section is in an invisible, microscopic font.