Android Project Structure

Before getting started in android applications, we need to understand the project structure of the android projects. The android app project will contain different types of app modules, source code files, and resource files. We will explore all the folders and files in the android app.

  • Manifests Folder
  • Java Folder
  • res (Resources) Folder
    • Drawable Folder
    • Layout Folder
    • Mipmap Folder
    • Values Folder
  • Gradle Scripts

Manifest File

Every project in Android includes a manifest file, which is AndroidManifest.xml, stored in the root directory of its project hierarchy. The manifest file is an important part of our app because it defines the structure and metadata of our application, its components, and its requirements.
This file includes nodes for each of the Activities, Services, Content Providers, and Broadcast Receiver that make the application, and using Intent Filters and Permissions determines how they co-ordinate with each other and other applications.

A typical AndroidManifest.xml file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.myapplication">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Java Folder

This folder will contain all the java source code (.java) files and kotlin source code(.kt) that we’ll create during the application development, including the JUnit test code. Whenever we create any new project/application, by default the class file MainActivity.kt will create automatically under the mentioned package name as shown below.

Res(Resource) Folder

It’s an important folder that will contain all non-code resources, such as bitmap images, UI strings, and XML layouts as shown below.

res/drawable Folder

A Drawable folder contains a resource type file (something that can be drawn). Drawables may take a variety of files like Bitmap (PNG, JPEG), Nine Patch, Vector (XML), Shape, Layers, States, Levels, and Scale.

res/layout Folder

The layout folder contains all XML layout files which we used to define the user interface of our application. I

res/mipmap Folder

This folder contains launcher.xml files to define icons that are used to show on the home screen. It contains different density types of icons depending upon the size of the device such as hdpi, mdpi, xhdpi.

res/values Folder

Values folder contains a number of XML files like strings.xml, dimens.xml, colors.xml, and styles definitions.

Color.xml

It is used for XML files that describe colors.

Below is a sample colors.xml file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="purple_200">#FFBB86FC</color>
    <color name="purple_500">#FF6200EE</color>
    <color name="purple_700">#FF3700B3</color>
    <color name="teal_200">#FF03DAC5</color>
    <color name="teal_700">#FF018786</color>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>
</resources>

string.xml

The strings.xml file contains string resources of the Android application. The different string value is identified by a unique name that can be used in the Android application program. This file also stores string arrays by using XML language.

Below is a sample strings.xml file:

<resources>
    <string name="app_name">My Application</string>
    <string name="date_format">MM/DD/YYYY</string>
    <string name="default_location">Chennai</string>
    <string name="welcome_format">Welcome To %1s</string>
    <array name="locations">
        <item>Chennai</item>
        <item>Mumbai</item>
        <item>Delhi</item>
        <item>Pune</item>
    </array>
</resources>

theme.xml

 The theme.xml file contains resources of the theme style in the Android application. This file is written in XML language.

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
</resources>

Gradle Folder

In android, Gradle means automated build system and by using this we can define a build configuration that applies to all modules in our application. In Gradle build.gradle (Project), and build.gradle (Module) files are useful to build configurations that apply to all our app modules or are specific to one app module.

Following is the structure of Gradle Scripts in the android application.

application-level build.gradle file sample,

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.2.0' apply false
    id 'com.android.library' version '7.2.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

module-level build.gradle file sample,

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdk 26
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

This defines the module-specific build configurations. Here you can add dependencies that you need in your Android application.

Leave a Reply

Your email address will not be published.