Introduction to Android Studio, Project Structure, and Emulation.
Tags: beginners, android, kotlin, motivation
Published: 2022-12-01 12:22:55


**_`I am very happy about Android obviously. I use Android, and it’s actually made cellphones very usable.”
              ~ Linus Torvald.`_**

_Hellooooo🥳️🥳️🥳️I am glad🤗 that you have joined me again in this Android development journey🚀. If you haven't read the [The Fundamentals of Android Development article](https://dev.to/dbriane208/the-fundamentals-of-android-development-1ffi) ensure you go through it to caught up with us. Do not forget to 👍like, comment👍 and share with your friends👨‍👨‍👦‍👦🥳️._

<u></u>**<u>Article Objectives</u>**

In this article, we're going to look at:

1. Emulators and Android OS images and their role in Android app development.
2. Main activity and manifest code in relation to the project structure of Android apps.
3. Gradle and its role in project building in Android Studio
4. The folder structure of an Android project, and its contents including the res and layout folders.

## <u>Emulators</u>

-  An **emulator** is a computer program that's designed to imitate another kind of device.
![Emulator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wxxx3mo934zwjfwp7udd.png)
-  While different types of emulators have unique ways of operating, the overall goal remains the same;

1. _It allows you to test your app without having to install them on the physical devices you designed them for._
2. _It imitates the Android devices your App is designed for_
3. _It replicates the experience of different hardware or software_.

- Emulation requires high computing resources therefore it's important to ensure your machine has _enough storage space_ and _random access memory_ for it to run effectively.
- There are several commercial and open-source emulators available for most of the operating systems in the market. eg

1. **Blue stacks:** allows you to run Android Apps on operating systems like Windows and Mac. It is also free to use.
2. **Appetite.io:** is a web-based emulator that allows you to use iOS applications on any PC. 
3. **Super Nintendo Entertainment System(SNES):**It makes gaming possible through playing old video games on modern HD televisions.

## <u>Android Virtual Device</u>

- Android Virtual Device makes it possible for you to test and optimize different mobile apps developed for different devices and operating systems.
- It allows you to define the various features of a physical device, such as an android phone that you want to create and simulate in the android emulator.
![Avd](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/98wzg41c2ck89sqqmpip.png)
- To access the AVD interface in Android Studio click the three-dotted menu in the top right for more actions. Select virtual device manager which opens in a new window. In this window, there are two major features:
![avdmenu](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/472xdrcfru8dq92ruwgz.png)
- `Create a new device button`: this will allow you to add another device to the list of previously created virtual devices. 
- There are also other icons in the top right hand used for various actions.
  - `Play button`: it will run the emulator in a virtual device.
  - `Pencil button`: clicking it allows you to edit and save 
    changes made to the properties of the virtual device.
![newavd](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q27gbl4kczckbjb75gcn.png)
- If you click on the `triangular drop-down menu` on the top right hand, actions range from duplicating to deleting a selected virtual device display. 
- In case you don't see the three-dotted menu you can click the more action menu in the center of the displayed window. 

## <u>Mobile CPU architecture</u>

- In order to develop and deploy apps for different mobile devices, the Central Processing Unit (CPU) architecture must be considered.

_What is a CPU?_
A _CPU_ is a translator between the hardware and software of a device that translates high-level software instructions to native machine language.
- The are three major CPU components used in smartphones which include;
    - **ARM**: It is the most common and properly optimized for 
      its battery use.
![Arm1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/siylmyfrj1gi9b1vn2x2.jpeg)
    - **ARM64**: is an evolution of the original ARM architecture 
      that supports 64-bit processing for more powerful computing 
      and it’s quickly becoming the standard in newer devices.
![arm](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6xpn3drzzk0um1hdzx2x.jpeg)
    - **x86**: it is a bit more powerful than the ARM CPUs, but 
      not quite as battery-friendly.
![Intel86](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h89guogvf6c2cos0uwfb.jpeg)
-  ARM better embodies a mobile-first mentality, with simple instruction sets, efficiency, and low power consumption. It requires fewer transistors and frees up that hardware space more than makes up for the use of RAM in a mobile device.

## <u>Operating System Images</u>

- Android OS images help you leverage features of any Operating system to give your users the best experience with your app.
- OS images help you to enjoy the built-in features of any Operating system using the API level which uniquely identifies new features released for a particular OS.
- Currently the most recent Android OS images available for developers in Android Studio range from version 8, code name Oatmeal Cookie released in 2017 to version 13, code-named Tiramisu. More specifically, after the release of Oatmeal Cookie in 2017 came Pistachio Ice Cream in 2018, Quince Tart in 2019, and Red Velvet Cake in 2020. These were followed by Snow Cone in 2021, Snow Cone V2 in March 2022, and Tiramisu.
![avdmenu](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/arrjp52dodqzwbcllyhd.png)
- To access the Android OS images, click the `Three dotted menu icon` on the upper right-hand side to access more actions. Then click on `Virtual Device Manager`. On the Device Manager page, click `Create Device.` This opens the virtual device configuration where you can create an emulator.
![avd](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9pc53mpqcvj1w75yl1un.png)
**<u>Configuring an Emulator</u>**

- Emulators are a safe place to test your code, especially at the beginning of the design. Nevertheless, emulators require some configuration in order to function properly.
- Avid properties required to configure an AVD to an emulator in Android Studio.
     _- Specify what you want the device to be called_

     _- Select the landscape or portrait mode_

     _- State the number of processor cores to be used_

     _- Override the default RAM value_
- To access the virtual device manager in Android Studio click the `three-dotted menu` in the top right corner then click the `new device` which will open a new window enabling you to choose the device you want to emulate. 
- In this window, you have the option to change the device and system, choose the device portrait or landscape and specify RAM and additional memory from the advanced settings.

## <u>Common Libraries and Packages</u>

_Why use libraries?_

Libraries extend the capabilities of the Android software development kit (SDK), allowing you to use code written by other developers.
These open-source libraries are hosted on an external server and are downloaded by the build system, Gradle, when you are building a project.

**<u>Types of Libraries</u>**

- **Image loading**: It helps to avoid high memory consumption caused by loading multiple images at the same time. eg _Fresco_ provides a smooth scrolling experience while an image is loading due to smart caching to minimize storage overhead.
- **Videos**: ExoPlayers offers an alternative to Android's MediaPlayer API due to its ease of customization when playing Audio and Video locally or online.
- **Networking**: Mobile apps need some sort of network to communicate with each other. _Retrofit_ provides you with a great way to make internet calls within your application. 

## <u>Android Studio and Project Structure</u>

**<u>Project Structure</u>**
- New projects in Android Studio generate files and folders that contain everything that is required for an app to run from source code and assets to test code and build configurations.
- These basic folders and files consist of:                           

      - **Navigation bar**: It allows you to access the project 
           folders and files.
      - **Sample app folder**: It is the root directory for 
          project folders and files.
      - **.gradle folder**: This is the toolkit that guides the 
           project-building process of Gradle. It contains all 
           configurations and files used by Gradle to build your 
           project. Though they are automatically generated they 
           can be deleted.
![project struct](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4kdrf3vvyz3dyspjov92.png)
      - **.idea folder**: It is used to store specific project 
           metadata which tells the app how to use the data it 
           describes. It contains configuration files and data for 
           specific functional areas.
      - **App folder**: It contains the source code related to 
           the project. Here you write your code, create the user 
           interface, and store assets eg images.
      - **Gradle folder**: It consists of a set of tools for 
           developers to build, test, and run applications.
- The Gradle folder contains subfolders and files which include;
       - `.gitignore file`: It specifies which files to be 
           excluded from your repository systems eg GitHub.
       - `build.gradle`: It is used to specify and manage the 
         configuration options common to all sub-projects folders.

![Gradle](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/njcxw5gm2bbzj2yfljv7.jpeg)
       - `Gradlew`: It is used by the Gradle Android studio build 
         system. It is created once and is updated whenever a new 
         feature or plugin is required to build a project.
       - `local.properties file`: It contains information specific 
         to your local configuration. It is not pushed to GitHub 
         because it contains sensitive information.
       - `settings.gradle file`: It handles the various settings 
         for projects and modules. It also displays third part 
         libraries.

## <u>Main Activity</u>

- One of the most fundamental parts of an Android App is the main activity class. It is generated together with other folders and files in Android Studio. An app will always have one activity class no matter how small in terms of code and scalability. 
- _`MainActivity.kt file`_ is the entry point for your app where kotlin code is displayed which is integral to the behavior of your app.
- An Android app is usually made of multiple activities, sometimes called screens, that together form the user experience. These activities are represented by an activity class whose function is to respond to user input.
![mainActivity](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4c2b6tmrrehczt4qbwx5.png)
- Each activity class contains a layout that holds different pieces of a user interface or UI together so that the user can interact with an app. This means that the activity class is the gateway through which a user can interact dynamically with an Android app's UI. 
- The main Activity class is the first screen that displays when a user displays an App.
- The `Oncreate()` function is the entry point for your app to run successfully. Its role is to create the view and initialize the actions of the activity. This function is necessary to create an app successfully.

## <u>Gradle</u>

- It enables Android Studio to run an application project and compile many different files and folders.
- The set of build configuration files defines:
     - How the project is developed
     - Dependencies for the project
     - Results or results of the compilation process.
- For every Android project, two build.gradle files are generated. The first Gradle settings apply to every module in the project. The second module-level settings apply to app modules.
- A module is a collection of the source file and build settings that allow you to divide your projects into different units of functionalities.
- Lets examine the build.gradle file.

```
1. apply plugin: 'com.android.application'

2. android{
3. default Config{...}
4. build Types{...}
        }
5. dependencies{...}
```
- The `android block` in 2 above contains information about your project eg the OS version of your app.
- The `default Config` in 3 above allows you to specify the minimum OS version, version number, application unique ID and other configuration settings.
- The `dependencies` in 4 above specifies what the third party you wish to include in your app.

- Gradle can be started manually through the commandline tool. The commands issued include:
      - `./gradlew build `- It is used to build projects when read 
          to run an app.
      - `./gradlew clean` - It is used to delete contents
          of building directory.
      - `./gradlew wrapper` - It allows you to see gradle 
           operations in the background.

## <u>Android Manifest</u>
- It is generated once a new project is launched. AndroidManifest.xml file contains essential information which includes, your activity, your receiver, and the service and providers. This defines the permissions that you need to access protected parts of the OS.
![Android manifest](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xv5knyk1h2rjdcohj87y.jpg)
    - **Application tag**: It specifies the theme of the 
        application of the user interface, icon, and label can be 
        defined.
    - **Activity tag**: It defines all the activities to be 
        present in an application.
    - **Intent filter**: It specifies the initial running of the 
        application.

## <u>Resource folder</u>
- It contains the form of files and files and static content that your code uses, such as colors and animations.
![Resf](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5nj94rbfa446mz2sus6v.jpeg)
- Resources available include _string, color, dimension, and font_
   - **String**: It enables you to define the text in the 
     res/value/string.xml file.
   - **Color**: It is defined in the color.xml. It helps you 
     manage your colors so that they are accessible across the 
     app.
   - **Dimension**: It allows you to manage all your dimensions in 
     one place using dimensions.xml.
   - **Font**: It is used to manage all the fonts you use within 
     your entire app project.
- These resources play an important role in your app compatibility.
- If you want to show congratulations in your app
      

```
    <resources>
    <string name = "message">Congratulations!!.<string>
        </resources>
```
- These folder lets you manage, change and access your resources globally across your app.

## <u>Project Files</u>
- They allow you to access the entire file structure of a project including all files hidden within folders from the Android view. 
- These project files include
   - `.build/ `- It contains files generated after a project build 
      occurs.
   - `.libs/` - It contains private and third-party libraries used 
      within the app.
   - `src/`  - It contains all code and resource files for the 
      module in its subdirectories.
   - `androidTest/` - It contains code for testing features of the 
      app.
   -  `test/ `- it contains the unit testing code, with unit tests 
      executed on the computer.
   -  `main/ `- It contains Android code and resources for testing 
      and production deployment.
![projS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vb25n4qwubut344ewxe8.png)
   - `AndroidManifest.xml` - It describes specific information 
      about your app and its activity.
   - `java/ `- It contains java and kotlin related code files.
   -  `gen/` - It contains the java files generated by android 
     studio, which is required for the app to build successfully.
   -  `res/` - It contains application resources eg image files, 
      font, dimension, and layout files.
   -  `assets/` - It contains files that should be compiled into 
     an.apk file.
- You can navigate the directory using URLs and read files.

## 🚀🚀Thank you for following me along this journey🥳️🥳️ !!
![End](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9z9jk23vqoakhyvi2687.jpeg)