Android App Project Structure Explanation

Till now, we have learnt about installing android studio and creating new project in android studio. In this tutorial, we will learn about android app project structure created using android studio. During this process, we will know different android file structures i.e. files like – .gitignore, build.gradle, gradlew, gradle.properties etc., folders like – app/src folder, app/src/main/java folder, app/src/main/res folder, AndroidManifest.xml file etc. We will know about everything, what are those files, why are we using it etc.

1. Getting Started

A project, in android studio, contains everything that defines workspace for an app, from source code and assets, to test code and build configurations. Whenever any project is created, Android studio creates basic folder structures that can be used –

  • to write code to do the things dynamically.
  • to write code to show the UI.
  • to store images, audio, videos or other media files.
  • to change the UI based on the device configurations.
  • to test the functionality of application.
  • to build the application and store application apk file.

2. Creating New Project

We have already created new project till now. However, visit our post on creating new project if you have not created yet.

There are many modes in which you can see created project. You can see created project in any mode. These modes are available at left-top corner.

Tutorialwing - Android Project View Modes

Tutorialwing – Android Project View Modes

Whenever a new project is created, it’s default view is shown in Android Mode. In Android Mode, you will see only app module related things. Since we are going to see the folder structure, we need to change this mode to Project. It will show maximum details about the project. You just need to select Project mode from the dropdown menus.

3. Android Project Structure

Folder structure in the newly created project in Project mode would look like below.
Tutorialwing - Android Folder Structure

Different folders and files are as below –

  • HelloWorld: This is project root directory.
  • .gradle: In this folder, you can find all the settings and files used by gradle to build the project. You can delete these files if you want. These files will be created when you will build the project again.
  • .idea: Android Studio stores project specific meta-data in this folder. It contains a set of configuration files. Each file contains configuration data for a certain functional area. The name of the file explain the functional area itself. For example, compiler.xml, encodings.xml, modules.xml file etc.
  • app: This is actual project folder where code related to project is stored. Here, you write code, create UI, stores assets etc. We will discuss about this section in detail soon.
  • build: This folder contains build related data. Data is generated when you build the project.
  • gradle: You would notice that there is wrapper folder inside this. When you distribute the wrapper with your project, anyone can work on the project without installing the Gradle. Another advantage is other users are guaranteed to use same version of Gradle that build was designed to work properly.
  • .gitignore: We include all the files in this folder that we want to exclude from version control system.
  • buld.gradle: This is a top-level build file where we add configuration options common to all sub-projects/modules. This is different from app/build.gradle file.
  • gradle.properties: This is where you configure project-wide Gradle settings. For example, Gradle daemon’s maximum heap size etc.
  • gradlew: This file is related to gradle. It is created once and updated when a new feature or plugin is required that needs updated gradle version.
  • gradlew.bat: This file is related to gradle. It is created once and updated when a new feature or plugin is required that needs updated gradle version.
  • local.properties: It contains information specific to your local configuration. For example, path to SDK etc. Since it contains local information, it should not be checked into version control system.
  • settings.gradle: This file handles project, module and other kinds of names and settings
  • External Libraries: This is not actually a folder but a place where referenced libraries are shown.



Now, we will see the app section in detail.

3.1 Android App Folder Structure – app Directory

Till now, we have seen overall android project structure in any app. Now, we will see folder structures within any app module. Here, we have app module inside HelloWorld project. HelloWorld/app section looks like below.
Tutorialwing - Android app directory structure

app folder has following files and sub-folders.

  • app/build: This contains output generated when we run the application. i.e. It contains build outputs.
  • app/libs: It contains libraries being used in this app module.
  • app/src: It contains all code and resource files(images, audio, video etc.) that are needed in the app module. It has sub folders too. We will discuss, in detail, about this later.
  • app/.gitignore: We include all the files(present in the app module) that we do not want to check into version control system.
  • app/build.gradle: This file contains build configurations specific to this app module.
  • app/proguard-rules.pro: This is where we add custom ProGuard rules. ProGuard detects and removes unused classes, fields, methods and attributes from your packaged app, including those from included code libraries. It also optimises the byte code, removed unused code instructions, and obfuscates the remaining classes, fields, and methods with short names. The obfuscated code make the APK difficult to reverse engineer, which is extremely valuable, especially when we have payment related information.

Now, coming to the app/src sections.

3.2 Android App Folder Structure – app/src Directory

At first, we have seen overall android project structure. Then, we went through folder structures inside app module. There was one more folder – app/src which we have not seen in detail. Now, we will go through all the folders inside app/src folder.

This section contains source code and all resources that we need to build any application. Below image shows the structure.
Tutorialwing - Android directory explanation

app/src folder has three sub-folders – androidTest, main and test.

  • androidTest: We write code into this folder to perform unit testing that need android device or emulator. These tests will have access to instrumentation information. For example, context of the app you are testing. So, write code into this folder to test the code that use Android framework. Moreover, Use this folder when your tests have android dependencies that mock objects can not satisfy.
  • main: This contains the main source-code(Android code and resource files), that we need to build project. We will talk about this later.
  • test: We write code into this folder to perform unit testing that do not need android device or emulator. These tests run locally on the Java Virtual Machine(JVM). Use these test to minimize execution time when tests have no Android framework dependencies or when you can mock android framework dependencies.

Now, coming to the main folder.




3.3 Android App Folder Structure – app/src/main Directory

The next thing that comes in android app project structure is – app/src/main folder. This folder contains the actual android code and resource files. Below image clearly depicts the folder structure.

Tutorialwing - Android directory explanation

app/src/main folder contains following files and sub-folders.

  • AndroidManifest.xml: You must have this file in any android application. It describes the nature of the application and each of its components. it also contains the name of java package of the application that serves as unique identifier for the application. It also contains minimum android API level that application requires.
  • java: This folder contains java/kotlin code source.
  • res: It contains application resources, such as drawable files, layout files, and UI string, dimension, colors etc. This folder also has sub-folders as below.

    Below are the sub-folder that resides inside res folder –

    • res/drawable: This folder contains drawable xml files and images. This is default drawable folder for resources to be used in application.
    • res/drawable-v24: This folder contains drawable xml files and images.
      This folder is used if we want to provide different version of resources on different versions of android. drawable-v24 will be used when android version is greater than or equal to 24. Otherwise, drawable folder will be used and resources present in this drawable folder will be ignored.
    • res/layout: This folder contains different ui screens we define for application.
    • res/mipmap: This folder contains launcher icon. You can see many mipmap folders – mipmap-anydpi-v26, mipmap-hdpi, mipmap-mdpi, mipmap-xhdpi, mipmap-xxhdpi and mipmap-xxxhdpi. Each of them is used based on screen resolution of device in which app is installed.
    • res/values: In values folder, we store different values used in the application. For example, different colours, dimensions, strings, styles used in the application. They are basically used to remove hardcoded value in the application. This also helps in resource localisation and internationalisation. res/values-night is used for dark mode of the application.

      res/values folder contains a separate file for each type of values. They are –

      • res/values/colors.xml: This file contains different colors used in the application.
      • res/values/dimens.xml: This file contains different dimension used in the application. This file is not created automatically. We need to create it when needed.
      • res/values/strings.xml: This file contains different strings used in the application.
      • res/values/styles.xml: This file contains different styles used in the application.
    • Other folder in res folder –
      res/raw: You can create this folder in res folder and store audio, video etc. into it.

Conclusion

This tutorial covers android app project structure when you create any project using android studio. Now, we know android app file structures – what is AndroidManifest.xml file, build.gradle files etc. We have tried to cover the topic in detail. If you have any query, please feel free to contact us at below contacts. Checkout android official page to know more in detail

Leave a Reply