Project Overview

Get introduced to the Android project structure, main project files, and folders in this lesson.

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’.

include ':app'
rootProject.name='Travel Blog'

The gradle.properties file defines settings to configure a build environment.

# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app's APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true

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.

buildscript {
ext.kotlin_version = '1.3.70'
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:3.5.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}

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 module
    • kotlin-android-extensions - this plugin contains a number of useful kotlin utilities for Android projects
    • kotlin-android - this plugin adds Kotlin language support (on top of Java)
  • compileSdkVersion - the version of Android SDK to compile the project
  • minSdkVersion - the minimal supported Android version
  • targetSdkVersion - the target version of Android SDK, used to tell the system to enable compatibility behaviors
  • applicationId - unique identifier of the application on the device and in Google Play Store
  • versionCode - an internal version number
  • versionName - the version name displayed to users
  • compileOptions - compile options to achieve some features of Java 1.8
  • dependencies - first-party and third-party library dependencies, discussed in the next lessons
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.travelblog"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
}
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
}
dependencies {
// kotlin
implementation "androidx.core:core-ktx:1.2.0"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
// ui
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.1.0'
}

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 case com.travelblog
  • theme - the global application theme, in our case MaterialComponents theme
  • label - the label which is used as a value for the application icon
  • activity - the activity, we currently only have one MainActivity
  • intent-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
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.travelblog">
<application
android:theme="@style/Theme.MaterialComponents.DayNight.NoActionBar"
android:label="Travel Blog">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

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.

<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

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.

package com.travelblog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

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)
    }
}