Published on

Structuring the Android Studio's File Structure

Authors
  • avatar
    Name
    Rosa Tiara
    Twitter

Introduction

When learning Android Development, there are a lot of things to learn, and sometimes it can be stressful for beginners, including me. One of the most important things to know is the basics of Android Studio's file structure. Why? Well, because Android Studio is the bestie for Android Developers. In this post, I'll try to explain it briefly and see their significance or role in our project.


Inside the Project tool window

color harmonies

The project tool window has several views, including the Android view, Project view, and Packages view. The arrow located just right the Android is the arrow for toggling and choosing which view you want to use. In this post, I'm using the Android view which is set by Android Studio as the default.

Disclaimer: I'm using the Empty Activity Template for this explanation

In the Android view, you'll see two main folders for our project, which are app and Gradle Scripts as shown below.

color harmonies


app

The app folder contains three subfolders that make up our application, which is manifests, java, and res that stands for resources.

color harmonies

manifests folder

color harmonies

In most cases, there is only a single manifest file in the manifests folder just like so. But this doesn't rule out the possibility that an app may have several manifest files. This usually happens due to application versioning or for supporting specific hardware.

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.filestructure">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.FileStructure">
        <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

color harmonies

The java folder—as the name implies, contains the Java source code files which consists of three folders, com.example.filestructure, com.example.filestructure (androidTest), and com.example.filestructure(test). Each of those folders consist a file which are MainActivity, ExampleInstrumentedTest, and ExampleUnitTest respectively.

The java folder is also used to store all the source code of your application. Since I'm using Kotlin, the main source code of the application will be in the MainActivity.kt file. If you're using Java, your file will be created by default as MainActivity.java

MainActivity.kt
package com.example.filestructure

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

As seen above, there'll also be a file called ExampleInstrumentedTest.kt. This file is used to perform instrumented tests that will execute on an Android device. These are the tests that run on a hardware device or emulator. For more information about how to add a new test to your application, you can read more about the details here.

ExampleInstrumentedTest.kt
package com.example.filestructure

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
 * Instrumented test, which will execute on an Android device.
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */

@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
    @Test
    fun useAppContext() {
        // Context of the app under test.
        val appContext = InstrumentationRegistry.getInstrumentation().targetContext
        assertEquals("com.example.filestructure", appContext.packageName)
    }
}

The last file in the java folder is ExampleUnitTest.kt, a file to build local unit tests which will execute on the development machine (host).

ExampleUnitTest.kt
package com.example.filestructure
import org.junit.Test
import org.junit.Assert.*

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
class ExampleUnitTest {
    @Test
    fun addition_isCorrect() {
        assertEquals(4, 2 + 2)
    }
}

Differences Between Instrumentation & Unit Test

Unit tests are the tests that run only on your local machine. These tests are compiled to run locally on the JVM to minimize execution time. If you have no dependencies on the Android framework, use this approach. To simplify, this test is used by running plain java code to test (for example) a content provider, database connections, input, and output of methods. This doesn't run on Android. You DON'T NEED a device to run a unit test.

Instrumentation tests are the unit tests that run on an Android device or emulator. These tests have access to instrumentation information, such as the context of the app under test. Use this approach to run unit tests that have Android dependencies that mock objects cannot easily satisfy. You NEED a device (physical or emulator) to run an instrumentation test because it mocks how the user will use the actual app. This test has access to views, activities, context, etc.

res folder

The term res stands for resources. This folder is where all your non-code sources like images, XML layouts, and UI strings are stored.

res folder
res folder (detailed)
  • res/drawable: the folder where we need to place all the images needed for our application development.

  • res/layout: contains all XML layout files which you'll use to define the user interface for our app. activity_main.xml file is consisted of three displays: Code, Split (Code & Design, side-by-side), and Design. You can choose any display based on your preference.

Code

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

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

Split

res folder (detailed)

Design

res folder (detailed)
  • res/mipmap: contains the launcher icon (a graphic that represents your app to users) files for the app.
  • res/values: contains XML files that contain simple values, such as strings, integers, colors, and definitions. This folder is used to keep track of the values you'll be using in your application. This is the example of colors.xml file, a file that acts as the color palette for your app.
<?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>

Gradle Scripts

Before we jump straight to what Gradle Scripts are, we have to know what Gradle is & why do we use this in our application.

What is Gradle?

Gradle is a build automation tool known for its flexibility to build software in languages like Java, Scala, Android, C/C++, and Groovy. The building process includes compiling, linking, and packaging the code, and Gradle makes these processes more consistent.

Why is Gradle used?

Some of the reasons to use Gradle are:

  • resolves all the issues faced on other build tools (Maven, ANT).
  • focuses on maintainability, usability, extendibility, performance, and flexibility.
  • highly customizable.
  • high-speed performance, nearly twice as fast as Maven.
  • supports a wide variety of IDEs which then leads to a better user experience as different people have their preference IDE.

This is how the Gradle Scripts on Android Studio looks like:

res folder (detailed)

In build.gradle (Project) there are builds cripts and in build.gradle (Module) there are plugins and implementations that are used to build configurations that can be applied to all our application modules, such as configuration files, properties files, and settings files. When we start an app, these gradle build scripts are automatically created. If you have special requirements needed for your app, simply specify those requirements there.


References

  1. Android Developers. (2022) Test your app | Android Developers. Retrieved January 27, 2022, from https://developer.android.com/studio/test
  2. Praveenruhil. (2021) Android Project folder Structure - GeeksforGeeks. Retrieved January 27, 2022, from https://www.geeksforgeeks.org/android-project-folder-structure/
  3. Multiple Authors. (2015) Difference between Android Instrumentation test and Unit test in Android Studio? - Stack Overflow. Retrieved January 27, 2022, from https://stackoverflow.com/questions/30393629/difference-between-android-instrumentation-test-and-unit-test-in-android-studio
  4. shubhamkhan. (2021) Android | Android Application File Structure - GeeksforGeeks. Retrieved January 27, 2022, from https://www.geeksforgeeks.org/android-android-apps-file-structure/