Components of an Android Project

  My previous article described the process of creating a new Android Project in Android Studio and the various steps involved in it. Here, I’ll tell you about various components of an Android Project.

Components of an Android Project

  First of all, open an existing Android Project and have a look at the left pane. You’ll see something similar to the below image –

Components of an Android Project

Components of an Android Project

  As you can see in the above image, there are lots of folders and files. At the top level, they are sorted into two main folders, namely, app and Gradle Scripts. The app folder contains all the main components of your project which are going to be a part of your final app. On the other hand, the contents of the Gradle Scripts folder are required only while developing, testing and debugging your app. They will not become part of the final app. In this article, only the app folder and its contents will be discussed. The Gradle Scripts folder and its contents have been discussed in What is Gradle.

  Now, as you see in the above image, the app folder has three subfolders, namely, manifests, java and res. I’ll discuss all of these one by one.



The manifests folder (AndroidManifest.xml)

  This folder contains only one file which is named AndroidManifest.xml. This file is a mandatory part of every Android app and it must have exactly the same name (for example, you can’t rename it as AndMft.xml or anything else). It contains essential information about your app, including the app’s name, package name, permissions required, details of activities in your app etc.

A typical AndroidManifest.xml file - Components of an Android Project

A typical AndroidManifest.xml file – Components of an Android Project

For example, the above image displays the AndroidManifest for an app that belongs to the package named com.androidcodebits.calculator. It has two activities named MainActivity and AboutActivity respectively. In this case, the app is quite simple and that’s why its AndroidManifest.xml is very short and simple. If we add more activities or add permissions etc., this file will contain more data.

  You’ll learn more about the various parts of AndroidManifest.xml once you start developing apps. If you’re a beginner, this much information is enough for you. However, if you want to view all details regarding this file right now, you can view this page – App Manifest | Android Developers.

The java folder (the source code)

  This folder contains the actual source code of your project. It contains all the .java files which may contain simple java classes, activities, services, broadcast receivers or content providers. Apart from the source code of the project, this folder also contains the files which are required to perform various tests on your app. I’ll discuss tests in a future article.

  There are three subfolders of the java folder. All these subfolders display the package name. The first subfolder contains the source code of the project. The other two subfolders contain the files required to perform the tests on your app. As a beginner, you can ignore these two subfolders. You need to bother about them only when you start running tests.

  In the first image on this page, you see five files inside the com.androidcodebits.calculator folder. Of these, AboutActivity and MainActivity are activities. The rest three files are ordinary classes. You can recognise an activity by the opened lock symbol on the left.



The res folder (the resources)

  This folder contains the resources of your project. The term resources refers to various kinds of files which are not part of the source code but they determine the appearance and functionality of the app. For example, every Android project uses some images for various purposes such as displaying app icon or other images inside the app. Also, the layouts of the activities, menus etc. are stored as resources.

  The res folder has several subfolders. As you can see in the first image on this page, there are five subfolders, namely, drawable, layout, menu, mipmap, values. There can be even more subfolders depending upon the requirements of the app.

  The drawable folder is used to store drawables. A drawable, in simplest terms, is a resource which is used to draw a shape. There are various types of drawables e.g. bitmap, 9-patch, vector, scale, shape.

  The layout folder stores the .xml files which determine the appearance of the app (the activities of the app) when it runs on a device i.e. phone or tablet. You may nclude different layouts for different types of devices. For example, you may want your app to appear differently on a 5-inch phone and a 10-inch tablet. This is usually done to take advantage of the larger screen available on tablets.

  The menu folder is used to store the .xml file(s) of the menu(s) of your app.

  The mipmap folder is used to store the images that will be used by an app. Usually, you include different files containing the same image in different resolutions. For example, suppose your app icon is stored in a file name ic_my_icon.png which has a resolution of 192*192. Since your app is going to be used on a variety of devices with different pixel densities, you should include other copies of your files with different resolutions such as 144*144, 96*96, 72*72 and 48*48. Later in this article, I’ll tell you how to do this. Android supports several image formats, viz. .jpg, .gif, .bmp, .png and .webp.

  The values folder contains several .xml files viz. colors.xml, dimens.xml, strings.xml and styles.xml. The colors.xml file stores the values of colors to be used in your app. As a beginner, you may not need to edit this file. The dimens.xml file is used to define certain values which would affect the layout of your app. The strings.xml file is used to store the text displayed on buttons, textviews etc. It main role lies in providing multi-language support for your app. I’ll discuss that in a future article. The styles.xml file is used to define the theme to be used by your app. It is also used to customize the appearance of various layout components.

  The raw folder is used to store media files like music and video. Here, the image doesn’t show a raw folder because this project doesn’t use any music or video.

  The xml folder is used to store certain xml files which do not fit in other folders. In this case, the project doesn’t have an xml folder.



How are these folders stored on the filesystem

  Till now, I have been discussing these folders according to the way they appear in Android Studio. However, if you locate these folders on your hard disk, you’ll find that they are stored in a slightly different way. To understand how thus works, locate your current Android project on you hard disk and open its folder. It would look like this –

How are these folders stored on the filesystem - Components of an Android Project

How are these folders stored on the filesystem – Components of an Android Project

  The appearance of the files and folders may be different depending on your OS, but the files and folders are the same. Here, you can see several files whose name contains the word gradle. These files are related to Gradle Build tool which is used for building the Android projects in Android Studio. You can read more about Gradle in What is Gradle.

  Now, open the app folder. There are several files and folders here too, but for now ignore them and open the src folder. In the src folder, there are three folders, namely, androidTest, main and test.

Inside the src folder - Components of an Android project

Inside the src folder – Components of an Android project

  Here, the androidTest and test folders contain the files which are used to run tests on the app. The main folder contains the java source code, the resources and the AndroidManifest.xml file.

The main folder

The main folder

  Now, open the java folder. Inside, you’ll find a folder. Open it and keep opening subsequent folders until you reach a folder which contains one or more java file(s). As you’ll find out, there are three or more subsequent folders inside java folder. These folders represent the package name of your app. For example, in this case the folders are like this –

 

com/androidcodebits/calculator

  These folders represent the package com.androidcodebits.calculator. Depending upon the name of the package in your project, the folders may have different names. Inside the last folder i.e. the calculator folder in this case, the java files are stored (as shown in the image below).

The Source Code files

The Source Code files

  Now, go back to the main folder and open the res folder. The res folder looks like this –

The resources folder - Components of an Android Project

The resources folder – Components of an Android Project

  Here, you can see all the types of folders I discussed in the resources folder section above. In the above image, you can see several folders named mipmap. WHY? Why are there so many mipmap folders? Inside Android Studio there was only one mipmap folder, wasn’t it? Well, I have already told you the reason for this. I mentioned above that you need to include images of various resolutions for supporting various screen resolutions and pixel-densities. These five folders store five copies of the same image(s) but in different resolutions. For example, if your app icon is stored in a file named ic_launcher.png, then each of these folders will have a file name ic_launcher.png. But their resolutions would be different. The mipmap-xxxhdpi folder will store the image with the highest resolution (192*192 for an icon), the mipmap-xxhdpi will have 144*144, mipmap-xhdpi will have 96*96, mipmap-hdpi will have 72*72 and finally, mipmap-mdpi will have the image with 48*48 resolution. If you have multiple images, then each image will have one file each in each of these five folders.

  Similarly, you see that there are two folders with the name values. The first folder is named values and the second one is values-w820dp. Here, the values folder stores the various .xml files for low-pixel density devices while the values-w820dp folder stores the various .xml files for high-pixel density devices. In this case, high-density refers to the devices whose width is equal to or more than 820dp (device independent pixels). Please note that if an .xml file exists only in values folder and not in the values-w820dp folder, then the same .xml file is used automatically by Android in both cases.

  If you plan to display different layouts on different devices, you’ll have include multiple layout folders, just like the mipmap and values folders as shown in the above image.

  I hope this article on components of an Android project helped you understand the structure of an Android project and the way its various components work. In What is Gradle, I’ve explained the basics of Gradle and the tasks it performs in an Android project.

(Visited 457 times, 5 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *