The Structure of an Android App

Apr 01, 2020

Learn how to identify the different files and directories that make up a project in Android Studio. In this video, we focus on the structure of an Android application in order to understand the various pieces which need to come together to build such an app and what purpose each of them serves.

One of the challenges of Android application development, at least at first, is the fact that an Android project contains many different files scattered across several folders. In this video, we focus on the structure of an Android application in order to understand the various pieces which need to come together to build such an app and what purpose each of them serves. Let us begin though at the project level.

So when you create an Android project, this is typically a container for a number of different apps. One common use case is to have a top level project and individual apps for different families of devices. So for example, you may have one variant of your app for phones, and then another one for a wearable device. For now though, let's just stick with a single app for a smartphone and within your project folder, you will have a project level build.gradle file. These will include properties which are applicable to all the apps within your project.

Then there is an idea directory, which includes Android Studio specific configs, and then an app folder, which contains definitions for an app in your project. Zooming in on the app folder, this in turn contains a number of different files and directories. One file is the AndroidManifest. This includes a lot of metadata about your application, while the Java directory is where you place the sources of your application. This includes Java or Kotlin sources as well as test definitions. Then there is a build.gradle for the individual app inside a project.

For example, this build.gradle can include dependencies for the individual application which may be different from the other apps in your project. And then there is a folder called res, which we will now expand and show that this includes a lot of sub directories containing various resources for your app. And now that I have given a high level overview of these different files in the overall project. Let's now zoom in on some of these starting with the project level files and folders. The first of these which we covered involves the Gradle scripts. And the Gradle scripts at the project level involve Project-level configurations.

Typically, the Project-level configs will remain rather small since these are meant to apply to all of the apps within your project. Any configurations which are only relevant to some of the apps should be placed within the gradle scripts for the corresponding app. A directory which is available at the project level includes the .idea folder, and this includes a lot of the IDE settings. And once again, these apply to all applications within the project. And beyond these, you will have individual App directories corresponding to each app within the project.

As I had touched upon earlier, it is common practice to have an Android Studio project for an app which is being built for multiple devices. And the variant of the app for each device will correspond to an individual app within a project. That said however, a project is merely a logical grouping of apps. And it is entirely up to you to decide what exactly your project and apps should look like. Now going back to the overall structure of an Android project, let us zoom in on the app directory.

This of course includes all of the individual files and folders corresponding to individual apps within your project. And while building an Android app, this is the level at which you're most likely to work in. One of the important pieces of an Android app is its manifest file, which includes a lot of metadata. And this file often serves as a bridge between your app and the Android operating system. For example, it includes the name of the app and also serves as a registry of activities. Activities in Android effectively translate to individual screens within your app.

Beyond that, permissions which are required for your app can also be included within the manifest file. Moving along then to the build.gradle file. This is where you have app-specific configurations. For example, the build types, the versions as well as the dependencies for your application will all be registered within its own build.gradle file. For each application, there is a resources folder, and this includes a lot of the application level resources. You will be zooming in on this a little later. But it is important to know that this is a folder in which you place all of the static content for your app which includes images, layout definitions, and a few other details.

And another folder is the Java directory. This is where you place the sources of your app which contain all of the business logic. The code here is typically written in either Java or the Kotlin programming language. And beyond these sources, you could also place your test definitions within the Java folder. In case you're wondering why the Kotlin sources are placed within the Java directory, well, this is because the two languages are quite closely related. It is possible for you to have Java code inside a Kotlin source and Kotlin eventually gets compiled to byte code which is executed on a Java virtual machine.

Going back to the project structure, having covered the project and app directories, we will now focus on the resources folder inside an app. Now it is possible to have a number of different folders here, but let's just focus on the important ones. So the resource folders can include one called drawable and this is where you would place your images. These include raster images which are of a fixed size. And Android is capable of resizing these images, though it could come at a cost of reduced precision. On the other hand, if you have vector images which can be defined in XML format, these are specifically meant for the purpose of resizing.

And these will also go into the drawable folder. Next is a menu directory. And this is where you can place the definitions of navigation menus for your app. These are also defined in an XML format and we will define one such menu during the labs of our course. Another directory in the resources folder is called layout. And this will play a significant role when we build an app, since these define the overall structure of a page or a screen in our app.

If you'd like to draw an analogy with web pages, you can think of the layouts as the HTML definition for a web page which includes the placement of different elements, and dictates the overall appearance, but does not include the business logic. Moving along then to the mipmap folder, this is where you would define icons which can be used for your application. And it is possible to have different sub directories for icons of varying revolutions. Android is smart enough to pick the correct icon depending on the overall pixel density of the device on which it is projected.

And a resource folder we will make significant use of during the labs is called values. This includes the definitions of a lot of constants such as strings, colors, fonts, and so on which can be referenced from the other files in your app. For example, you can define a background color within the colors file. And this can be referenced by one of the files in the layouts. One important feature of all these resources though, is that they are generally static. All of the control logic for the app will be placed within the Java folders and will be coded in Java or Kotlin.