Reader small image

You're reading from  How to Build Android Apps with Kotlin

Product typeBook
Published inFeb 2021
Reading LevelIntermediate
PublisherPackt
ISBN-139781838984113
Edition1st Edition
Languages
Right arrow
Authors (4):
Alex Forrester
Alex Forrester
author image
Alex Forrester

Alex Forrester is an experienced software developer with more than 20 years of experience in mobile, web development, and content management systems. He has been working with Android for over 8 years, creating flagship apps for blue-chip companies across a broad range of industries at Sky, The Automobile Association, HSBC, The Discovery Channel, and O2. Alex lives in Hertfordshire with his wife and daughter. When he's not developing, he likes rugby and running in the Chiltern hills.
Read more about Alex Forrester

Eran Boudjnah
Eran Boudjnah
author image
Eran Boudjnah

Eran Boudjnah is a developer with over 20 years of experience in developing desktop applications, websites, interactive attractions, and mobile applications. He has been working with Android for about 7 years, developing apps and leading mobile teams for a wide range of clients, from start-ups (JustEat) to large-scale companies (Sky) and conglomerates. He is passionate about board games (with a modest collection of a few hundred games) and has a Transformers collection he's quite proud of. Eran lives in North London with Lea, his wife.
Read more about Eran Boudjnah

Alexandru Dumbravan
Alexandru Dumbravan
author image
Alexandru Dumbravan

Alexandru Dumbravan has been an Android Developer since 2011 and worked across a variety of Android applications which contained features such as messaging, voice calls, file management, and location. He continues to broaden his development skills while working in London for a popular fintech company.
Read more about Alexandru Dumbravan

Jomar Tigcal
Jomar Tigcal
author image
Jomar Tigcal

Jomar Tigcal is an Android developer with over 10 years of experience in mobile and software development. He worked on various stages of app development for small startups to large companies. Jomar has also given talks and conducted training and workshops on Android. In his free time, he likes running and reading. He lives in Vancouver, Canada with his wife Celine.
Read more about Jomar Tigcal

View More author details
Right arrow

1. Creating Your First App

Overview

This chapter is an introduction to Android, where you will set up your environment and focus on the fundamentals of Android development. By the end of this chapter, you will have gained the knowledge required to create an Android app from scratch and install it on a virtual or physical Android device. You will be able to analyze and understand the importance of the AndroidManifest.xml file, and use the Gradle build tool to configure your app and implement UI elements from Material Design.

Introduction

Android is the most widely used mobile phone operating system in the world, with over 70% of the global market share (see https://gs.statcounter.com/os-market-share/mobile/worldwide). This presents great opportunities to contribute and make an impact by learning Android and building apps that have a global reach. For a developer who is new to Android, there are many issues you must contend with in order to get started learning and becoming productive. This book will address these issues. After learning the tooling and development environment, you will explore fundamental practices to build Android apps. We will cover a wide range of real-world development challenges faced by developers and explore various techniques to overcome them.

In this chapter, you will learn how to create a basic Android project and add features to it. You will be introduced to the comprehensive development environment of Android Studio and learn about the core areas of the software to enable...

Creating an Android Project with Android Studio

In order to be productive in terms of building Android apps, it is essential to become confident with how to use Android Studio. This is the official Integrated Development Environment (IDE) for Android development, built on JetBrains' IntelliJ IDEA IDE and developed by the Android Studio team at Google. You will be using it throughout this course to create apps and progressively add more advanced features.

The development of Android Studio has followed the development of the IntelliJ IDEA IDE. The fundamental features of an IDE are of course present, enabling you to optimize your code with suggestions, shortcuts, and standard refactoring. The programming language you will be using throughout this course to create Android apps is Kotlin. Since Google I/O 2017 (the annual Google developer conference), this has been Google's preferred language for Android app development. What really sets Android Studio apart from other...

Setting Up a Virtual Device and Running Your App

As a part of installing Android Studio, you downloaded and installed the latest Android SDK components. These included a base emulator, which you will configure to create a virtual device to run Android apps on. The benefit is that you can make changes and quickly see them on your desktop whilst developing your app. Although virtual devices do not have all the features of a real device, the feedback cycle is often quicker than going through the steps of connecting a real device.

Also, although you should ensure your app runs as expected on different devices, you can standardize it by targeting a specific device by downloading an emulator skin even if you don't have the real device if this is a requirement of your project.

The screen you will have seen (or something similar) when installing Android Studio is as follows:

Figure 1.5: SDK components

Let's take a look at the SDK components that are...

The Android Manifest

The app you have just created, although simple, encompasses the core building blocks that you will use in all of the projects you create. The app is driven from the AndroidManifest.xml file, a manifest file that details the contents of your app. It has all the components, such as activities, content providers, services, receivers, and the list of permissions that the app requires to implement its features. For example, the camera permission is required to capture a photo in an app. You can find it in the Project view under MyApplication | app | src | main. Alternatively, if you are looking at the Android view, it is located at app | manifests | AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">
    <!--Permissions like camera go here-->
 &...

Using Gradle to Build, Configure, and Manage App Dependencies

In the course of creating this project, you have principally used the Android platform SDK. The necessary Android libraries were downloaded when you installed Android Studio. These are not the only libraries, however, that are used to create your app. In order to configure and build your Android project or app, a build tool called Gradle is used. Gradle is a multi-purpose build tool that Android Studio uses to build your app. By default, in Android Studio, it uses Groovy, a dynamically typed JVM language, to configure the build process and allows easy dependency management so you can add libraries to your project and specify the versions. Android Studio can also be configured to use Kotlin to configure builds, but, as the default language is Groovy, you will be using this. The files that this build and configuration information is stored in are named build.gradle. When you first create your app, there are two build.gradle...

Android Application Structure

Now that we have covered how the Gradle build tool works, we'll explore the rest of the project. The simplest way to do this is to examine the folder structure of the app. There is a tool window at the top left of Android Studio called Project, which allows you to browse the contents of your app. By default, it is open/selected when your Android project is first created. When you select it, you will see a view similar to the screenshot in Figure 1.19. (If you can't see any window bars on the left-hand side of the screen, then go to the top toolbar and select View | Appearance | Tool Window Bars and make sure it is ticked). There are many different options for how to browse your project, but Android will be pre-selected. This view neatly groups the app folder structure together, so let's take a look at it.

Here is an overview of these files with more detail about the most important ones. On opening it, you will see that it consists of...

Summary

This chapter has covered a lot about the foundations of Android development. You started off with how to create Android projects using Android Studio and then created and ran apps on a virtual device. The chapter then progressed by exploring the AndroidManifest file, which details the contents of your app and the permission model, followed by an introduction to Gradle and the process of adding dependencies and building your app. This was then followed by going into the details of an Android application and the files and folder structure. Layouts and views were introduced, and exercises iterated on to illustrate how to construct UIs with an introduction to Google's Material Design. The next chapter will build on this knowledge by learning about the activity lifecycle, activity tasks, and launch modes, persisting and sharing data between screens, and how to create robust user journeys through your apps.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
How to Build Android Apps with Kotlin
Published in: Feb 2021Publisher: PacktISBN-13: 9781838984113
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (4)

author image
Alex Forrester

Alex Forrester is an experienced software developer with more than 20 years of experience in mobile, web development, and content management systems. He has been working with Android for over 8 years, creating flagship apps for blue-chip companies across a broad range of industries at Sky, The Automobile Association, HSBC, The Discovery Channel, and O2. Alex lives in Hertfordshire with his wife and daughter. When he's not developing, he likes rugby and running in the Chiltern hills.
Read more about Alex Forrester

author image
Eran Boudjnah

Eran Boudjnah is a developer with over 20 years of experience in developing desktop applications, websites, interactive attractions, and mobile applications. He has been working with Android for about 7 years, developing apps and leading mobile teams for a wide range of clients, from start-ups (JustEat) to large-scale companies (Sky) and conglomerates. He is passionate about board games (with a modest collection of a few hundred games) and has a Transformers collection he's quite proud of. Eran lives in North London with Lea, his wife.
Read more about Eran Boudjnah

author image
Alexandru Dumbravan

Alexandru Dumbravan has been an Android Developer since 2011 and worked across a variety of Android applications which contained features such as messaging, voice calls, file management, and location. He continues to broaden his development skills while working in London for a popular fintech company.
Read more about Alexandru Dumbravan

author image
Jomar Tigcal

Jomar Tigcal is an Android developer with over 10 years of experience in mobile and software development. He worked on various stages of app development for small startups to large companies. Jomar has also given talks and conducted training and workshops on Android. In his free time, he likes running and reading. He lives in Vancouver, Canada with his wife Celine.
Read more about Jomar Tigcal