Project Structure

Project Structure

The Android project structure is designed to keep code and resources organized, making development more efficient.

Written by: saeed1907057

Sun, 03 Nov 2024

Creating first project

  • Click on New Project from the home or File > New Project when another project is opened.

next image

  • Then give a project name and finish.

next image

  • Project creation may take few minutes depending on internet speed and PC configuration.
  • We will get a view like this after our project has been built.

next image

  • Where
    • 1 : App name
    • 2 : Shows the emulator or connected devices.
    • 3 : Run normally or in debug mode in device.
    • 4 : Build project.
    • 5 : Shows available devices.
    • 6 : Shows currently running devices.
    • 7 : Build steps and info.
    • 8 : Logcat containing logs, error, message.
    • 9 : Built-in terminal. (Alt + F12)
    • 10: Path of the currently opened file.

File structure

  • Android uses Gradle as build tools, so it's structure is similar to gradle.

next image

  • Here,
    • 1 : Mode of the project. Android mode shows a minimized view of the whole structure that are mostly used by us.
    • 2 - app : This module contains the main code written by us. We will write almost everything inside this.
    • 3 - AndroidManifest : This can be considered as the configuration file of our app.
    • 4 - java>com>lazymind>myfirstapp : This is the main package of our app. We will write all of our java code here.
    • 5 - MainActivity : When we create a new project, an activity(page) is created by default. This MainActivity.java file is that Page's Java part.
    • 6 : This will contain the code for performing unit and ui test.(ignore for now).
    • 7 - drawable : For storing external images or icons. Ex: icons, backgrounds.
    • 8 - layout : For storing all of our xml design files(For page design or item design).
    • 9 - mipmap : Stores app launcher icons in different resolutions for various screen densities.
    • 10 - values : Contains XML files for storing hardcoded resources like strings, colors, dimensions, and styles.
    • 11 - xml : Stores custom XML files for configurations.
  • If we take a look on the gradle configuration file. It will look like this:

next image

  • Here,
    • 1 - build.gradle.kts : Main configuration file for our whole project. It includes plugins, dependencies, configuration. We won't modify it much.
    • 2 - build.gradle.kts : Same but only difference is that, this is for our app module. We will mostly work on it.
    • 3 - libs.versions.toml : Contains the versions number of our dependencies.
    • 4 - settings.gradle.kts : It specifies: project name, list of included modules, and specifies where to fetch dependencies for the project.
  • If we open our project in file explorer, we will see only folders and files like below. These folder are what we called package.
  • You may see addional folder and files as well. Remember, Android mode shows a minimized version that we mostly use.
  • We will get the exact same structure in file explorer if we open project in Project mode in Android Studio.

next image

Why do we need Gradle?

  • Automates building, like compiling code and creating APKs.
  • It saves a lot of time during compilation by using previous compiled files if any.
  • It will compile only the files that have been changed. That's why build at first time takes more time.
  • Makes it easy to add libraries and manage versions.

The AndroidManifest.xml file

  • It describes:
    • Project configuration, Permissions, All activities(pages), provider, service etc.
  • Sample AndroidManifest.xml file:
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">
    
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    
        <application
            android:allowBackup="false"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:largeHeap="true"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/Theme.SmartBoard"
            android:dataExtractionRules="@xml/data_extraction_rules"
            android:fullBackupContent="true"
            tools:targetApi="s" >
    
            <activity
                android:name=".Homepage"
                android:exported="false"
                android:launchMode="singleTop"
                android:theme="@style/AppThemeCustom" />
    
            <activity
                android:name=".StartingPage"
                android:configChanges="uiMode|orientation|screenSize"
                android:exported="true"
                android:launchMode="singleTop"
                android:noHistory="true"
                android:theme="@style/fullScreenActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <provider
                android:name="androidx.core.content.FileProvider"
                android:authorities="${applicationId}.provider"
                android:exported="false"
                android:grantUriPermissions="true">
                <meta-data
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/provider_paths" />
            </provider>
            <meta-data
                android:name="preloaded_fonts"
                android:resource="@array/preloaded_fonts" />
        </application>
    
    </manifest>
    

App level build.gradle file:

  • It contains
    • Configures Android-specific settings, like compileSdk, minSdk, and targetSdk.
    • Specifies libraries required for the project.
  • Sample build.gradle.kts file:
    plugins {
        alias(libs.plugins.android.application)
    }
    
    android {
        namespace = "com.lazymind.myfirstapp"
        compileSdk = 34
    
        defaultConfig {
            applicationId = "com.lazymind.myfirstapp"
            minSdk = 24
            targetSdk = 34
            versionCode = 1
            versionName = "1.0"
    
            testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        }
    
        buildTypes {
            release {
                isMinifyEnabled = false
                proguardFiles(
                    getDefaultProguardFile("proguard-android-optimize.txt"),
                    "proguard-rules.pro"
                )
            }
        }
        compileOptions {
            sourceCompatibility = JavaVersion.VERSION_1_8
            targetCompatibility = JavaVersion.VERSION_1_8
        }
    }
    
    dependencies {
    
        implementation(libs.appcompat)
        implementation(libs.material)
        implementation(libs.activity)
        implementation(libs.constraintlayout)
        testImplementation(libs.junit)
        androidTestImplementation(libs.ext.junit)
        androidTestImplementation(libs.espresso.core)
    }
    
  • Here,
    • android > defaultConfig : Sets the app’s default configuration.
    • dependencies : Contains libraries and frameworks needed by the app.

What is dependencies

  • Dependencies are external libraries or modules that a project needs to work properly.
  • They provide code for specific tasks—like handling images, making network request.

Why do we need dependencies?

  • Dependencies save time and effort by letting developers reuse code reliable for common tasks.
  • This speeds up development, reduces bugs, and improves code quality.
User12024-11-02

This is a comment 1.

User12024-11-02

This is a comment 2.