Project Overview
Get introduced to the Android project structure, main project files, and folders in this lesson.
We'll cover the following...
Structure
Let’s take a look at the structure of what a typical Android project looks like:
- app - root module folder
- build.gradle - module config file
- src/main/AndroidManifest.xml - module manifest file
- src/main/java - module source folder for Java or Kotlin files
- src/main/res - module resource folder
- build.gradle - project config file
- gradle, gradle.properties, gradlew, gradlew.bat - Gradle related files to build android project
- settings.gradle - project settings file
project-name
├── app
│ ├── build.gradle
│ └── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com/travelblog/MainActivity.kt
│ └── res
│ ├── layout/activity_main.xml
│ └── values/
├── build.gradle
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradle.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
The Android project may consist of one or several modules. Small to medium projects usually have one module, while large projects tend to have multiple modules. Each module may contain a separate feature or common logic. The definition and composition of modules will be explained in detail later in the lesson.
Project files usually apply configuration to every module, which is part of this project. Module files usually contain our source code and resources.
Project files
The settings.gradle file is a project file which contains the list of modules and project name. In our case, the module name is ‘app’, and the project name is ‘Travel Blog’.
The gradle.properties file defines settings to configure a build environment.
The gradle, gradlew, gradlew.bat files are related to the Gradle wrapper. This means that we don’t have to manually install Gradle ourselves.
Finally, build.gradle file is a top-level build file where we can add configuration options common to all modules. In our case, we want all of our modules to have access to Google’s Maven repository and Bintray’s JCenter repository for dependencies. These repositories allow projects to use the core Android functionalities. We also specify Gradle and Kotlin versions in this file.
Module files
Every module has a unique name. In our case, the module is called ‘app’. That’s where we put application source code.
The module build.gradle file contains a set of configurations related to this module only, such as:
apply plugin- plugins used in this module:com.android.application- this plugin specifies that this is an Android application modulekotlin-android-extensions- this plugin contains a number of useful kotlin utilities for Android projectskotlin-android- this plugin adds Kotlin language support (on top of Java)
compileSdkVersion- the version of Android SDK to compile the projectminSdkVersion- the minimal supported Android versiontargetSdkVersion- the target version of Android SDK, used to tell the system to enable compatibility behaviorsapplicationId- unique identifier of the application on the device and in Google Play StoreversionCode- an internal version numberversionName- the version name displayed to userscompileOptions- compile options to achieve some features of Java 1.8dependencies- first-party and third-party library dependencies, discussed in the next lessons
The AndroidManifest.xml is the place where we declare all main components used in our application, such as activities, services, permissions, etc.
Our manifest file currently contains the following:
package- the package name of the application, in our casecom.travelblogtheme- the global application theme, in our caseMaterialComponentsthemelabel- the label which is used as a value for the application iconactivity- the activity, we currently only have oneMainActivityintent-filter- the set of options used to give the activity some unique behavior, in our case we declare that it’s the main activity which should be launched when the user clicks on the application icon from the launcher
All resource-related files must be placed inside one of the predefined, sub-folders of the src/main/res folder. One of such predefined folders is the layout folder, which contains all of our layout files. The other is the values folder, which usually contains colors, styles, dimensions, etc.
For now, we only have one layout file, activity_main.xml, which describes the layout structure. We are going to talk about it in more detail in the next lesson.
Finally, we have a src/main/java folder, which contains Java/Kotlin source code divided by packages.
Currently, we only have one class file MainActivity, which represents our main screen. We will talk about it in more detail in the next lesson.
Android widget
In this lesson, we will use Educative’s Android widget to run and test the Android projects. You can navigate to see all the different files and their content.
Hit the run button and wait until the Android application is compiled and the emulator is started. The first time it may take up to 3 minutes to build the project, but all consecutive builds will be much faster.
If you make some changes to the code, save your changes and press run again to restart the application. Please note that it might take some time, so you can observe the terminal tab to get an idea of the progress.
package com.travelblog
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}