Android App File Fundamentals

It can be quite overwhelming to start looking at code in a new android app you are creating.  Even creating an empty app in Android Studio creates dozens of file for you, and yet that empty project actually only produces one screen with the words “Hello World” written.  For this post, I want to make you familiar with what files you should focus on as a developer just beginning with creating Android apps.

We are going to talk about 4 main types of files in this post: activities, layout files, the manifest, and the build.gradle files. I have created a sample empty app to show the simplest version of each of these files.


Let’s start with the two easiest files to understand, the activities and layout files:

Activities:

These are the parts of an app, something a user can do.  So you might have, for example, a home screen activity and a picture capture activity.  The home screen might make an API call to fill a list of items.  The picture capture might use the phone’s built in camera to allow the user to take a picture.  If you want your app to do anything (have any sort of action associated with it), that is all set up in your activity (As a note, there are more complicated items such as fragments that you could place in an activity and the fragment might contain logic, but for simplicity, when beginning with Android, activities will be the bread and butter files you use most).  When creating our sample empty app, Android Studio automatically creates one starting activity for you: MainActivity.  All MainActivity does in our basic app is tell the system to load a screen and which layout file to use.  From there, you can, for example, set up click listeners for buttons, or send the user to another activity etc. In our example, this file is in Kotlin.

Layout Files:

This tells the system what to initially show when an activity inflating this layout loads.  The layout is an xml file containing one parent layout with child views contained within it.  Views can be anything: TextViews, ImageViews, Buttons, CardViews, a nested layout, or maybe even a custom view you created.  You arrange the views in the layout and set any initial settings for that view.  For example, you can specify the size, location and image for the image view. You can set the text, text color, font etc for the text view.  (Alternatively you don’t have to specify these right away in the layout file. You can instead programmatically set them in the activity after, for example, info from an API call.)

Relationship between activities and layout files:

An Activity won’t show anything unless it inflates a layout, and a layout won’t be seen until inflated by an activity.  So typically in order any create a screen, you are going to have to create both of these files for each screen.

Where they live:

I have chosen to show you the file structure using the “Project” tab in Android Studio, as this will mimic how the files appear when uploaded to Github (If instead you are viewing the “Android” tab in the file display, you will see Android reorganizes and hides some files to focus on the most commonly used items).

So inside of our SampleApp app, we have a bunch of folders, but the folder we want to focus on is the app module folder.  An app can have multiple modules in it, and each module will show up as a folder under the project [1].  You might have an app with multiple modules when you want to split up features or you have code for different devices like an android phone vs android watch, but for our simple project we are only going to have one module, the app module.

Looking in the app module, we see under src/main that we have a java folder and a res folder.  The java folder will contain all of your Java and Kotlin files, so all of your activities will live in here.  As you can see, we have MainActivity within com.example.sampleapp.  The other folder in main is the resources folder, or res.  This contains anything that your activities might use.  So, this will be where our activity_main.xml layout file lives.  It and all other layouts will be the in the layouts folder within the resources folder.  Of interesting note, the resources folder also contains a values folder.  Within that folder we have files for strings.xml, colors.xml etc.  Instead of hard coding strings or colors into your activity or layout files, define them there and then you can use the variables anywhere within the module without fear of typos [2].


Now let’s look at a couple of helper files that make the app run – the android manifest and the build.gradle files:

AndroidManifest: 

You can see the AndroidManifest file within src/main.  This file defines a lot about the app itself.  It lists the requirements of the device the app is to be installed on and the permissions the app needs when running (like access to your contacts or your camera etc) [3].  It also contains a list of the important components within your app.  It acts kind of like a table of contents, so the system knows what is inside the app without having to scan the entire APK.  If your activity is not listed within the manifest, your system will have no idea it exists and it will never be used [3].  This makes the manifest an important file to understand because when you add a new activity to your starter app, you must remember to ensure it also appears in the manifest.

Build.gradle files:

Gradle is responsible for taking your files and building them into the APK that can then be installed on a device.  The build.gradle files allow you specify anything about the build that is necessary for your app to run.  Even while first learning about Android development, there is one key thing you may need to add to the build.gradle file to make your app work: dependencies.  These dependencies might include, for example, a library that helps with api calls, or a library that provides robust testing tools.  The build.gradle file is where you define which dependency and what version you need.  You may notice that there are two build.gradle files showing, one in the SampleApp project and one in the app module.  Project build.gradle files allow you to define things that should be applied to all modules.  Module build.gradle files allow you to specify different builds for different modules within the same app [4].


A working knowledge of these four file types gives you a solid foundation for starting Android app development.  Hopefully this short peek into the key files within your app will make the jump into Android app development feel less daunting.

Sources:

[1] “Project Overview,” Aug 2, 2021 . Accessed on: Oct 6, 2021. [Online]. Available: https://developer.android.com/studio/projects

[2] “App Resources Overview,” Jul 14, 2021 . Accessed on: Oct 6, 2021. [Online]. Available: https://developer.android.com/guide/topics/resources/providing-resources

[3] “App Manifest Overview,” Oct 4, 2021 . Accessed on: Oct 6, 2021. [Online]. Available: https://developer.android.com/guide/topics/manifest/manifest-intro

[4] “Configure Your Build,” Oct 4, 2021 . Accessed on: Oct 6, 2021. [Online]. Available: https://developer.android.com/studio/build

Print Friendly, PDF & Email

Leave a comment

Your email address will not be published. Required fields are marked *