28,99 €
Written with the best practices, this book will help you master Kotlin and use its powerful language features, libraries, tools, and APIs to elevate your Android apps.
As you progress, you'll use Jetpack Compose and Material Design 3 to build UIs for your app, explore how to architect and improve your app architecture, and use Jetpack Libraries like Room and DataStore to persist your data locally. Using a step-by-step approach, this book will teach you how to debug issues in your app, detect leaks, inspect network calls fired by your app, and inspect your Room database. You'll also add tests to your apps to detect and address code smells.
Toward the end, you’ll learn how to publish apps to the Google Play Store and see how to automate the process of deploying consecutive releases using GitHub actions, as well as learn how to distribute test builds to Firebase App Distribution. Additionally, the book covers tips on how to increase user engagement.
By the end of this Kotlin book, you’ll be able to develop market-ready apps, add tests to their codebase, address issues, and get them in front of the right audience.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 357
Veröffentlichungsjahr: 2024
Mastering Kotlin for Android 14
Build powerful Android apps from scratch using Jetpack libraries and Jetpack Compose
Harun Wangereka
Copyright © 2024 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Group Product Manager: Rohit Rajkumar
Publishing Product Manager: Vaideeshwari Muralikrishnan
Book Project Manager: Sonam Pandey
Senior Editor: Rakhi Patel
Technical Editor: K Bimala Singha
Copy Editor: Safis Editing
Indexer: Rekha Nair
Production Designers: Alishon Mendonca and Gokul Raj S.T
DevRel Marketing Coordinators: Anamika Singh and Nivedita Pandey
First published: April 2024
Production reference: 1010324
Published by
Packt Publishing Ltd.
Grosvenor House
11 St Paul’s Square
Birmingham
B3 1RB, UK
ISBN 978-1-83763-171-1
www.packtpub.com
I extend heartfelt gratitude to my beloved wife for her unwavering encouragement and support in every endeavor, as well as her understanding during the late nights and weekends I dedicated to this book. To my mother: I express deep appreciation for her constant support. I am immensely thankful to my family, friends, and colleagues whose continual motivation propels me toward becoming a better version of myself. Special thanks to the vibrant Android254 and Kotlin Kenya communities, where I found inspiration and ideas through interactions with members and the insightful questions I regularly receive. Lastly, my sincere appreciation goes to the exceptional team at Packt for their dedication and hard work on this book.
—Harun Wangereka
Harun Wangereka is an Android engineer and a Google Developer Expert for Android. He is passionate about creating quality applications and perpetually embraces the journey of continuous learning, contributing to tech communities, and aiding fellow developers in honing their skills. His enthusiasm extends to crafting technical articles centered around Android and Kotlin topics.
He is currently working at Apollo Agriculture. In this role, he helps make financing accessible to small-scale farmers. He collaborates with a cross-functional team to define, design, and ship new features for two apps, namely Agent’s App and Agro-Dealers App.
Beyond his professional commitments, Harun is a part of the team that organizes Droidcon Kenya, leading the speakers team. He is an engaged community member at Android254 and Kotlin Kenya, channeling his passion into giving back to the community by sharing knowledge and experiences amassed over time as an Android engineer.
Dmitrii Ivashchenko is an expert software engineer with over a decade of experience in mobile development and backend systems. Starting as a junior full stack developer, Dmitrii became the lead of a mobile game development team at a major international company. An active member of the International Game Developers Association and the Academy of Interactive Arts & Sciences, Dmitrii has authored articles for Medium and HackerNoon. He has also served as a judge for international awards and often speaks at conferences. Currently, he is the lead software engineer at MY.GAMES, guiding a team dedicated to creating successful and engaging games. Dmitrii is passionate about mentoring and regularly shares his insights within the developer community.
Peter Gichia is a software engineer focused on native Android development and currently working as a freelance Android engineer and an entrepreneur on the side. He enjoys solving problems for his clients whether through code or business strategy. He is also an active contributor to the Android development community through writing and publishing Android-related articles. In the process, he has successfully published a text-based course on building scalable applications with MVVM architecture and is working on publishing another one about clean architecture in collaboration with a leading Edutech company. In his free time, Peter enjoys expanding his knowledge through podcasts and books.
In this part, you will embark on a journey into Kotlin, exploring the features that make it an optimal choice for Android development. We will guide you through the process of migrating from Java, offering valuable insights for developers transitioning from a Java background. Step by step, you will delve into constructing your inaugural Android app, including the setting up of your development environment, and becoming acquainted with Android Studio. The focus extends to mastering Jetpack Compose, unraveling the art of crafting intuitive user interfaces. To finish, we will enlighten you on incorporating Material Design 3 into your applications, shedding light on the rich features and components it brings to the development landscape.
This section contains the following chapters:
Chapter 1, Get Started with Kotlin Android DevelopmentChapter 2, Creating Your First Android AppChapter 3, Jetpack Compose Layout BasicsChapter 4, Design with Material Design 3Android, a mobile operating system developed by Google, runs on over two billion devices, such as smartphones, tablets, TVs, watches, and cars, and developers are able to write code that is compatible with these different devices.
In this chapter, we’ll create our first Android app. We will also familiarize ourselves with Android Studio, the Integrated Development Environment (IDE) that we’ll use to develop Android apps. We will also learn some tips, shortcuts, and useful Android Studio features and understand the process of creating a project in Android Studio.
In this chapter, we’re going to cover the following main topics:
Android Studio overviewCreating your Android appAndroid Studio tips and tricksTo follow the instructions in this chapter, you will need to have Android Studio Hedgehog or later (https://developer.android.com/studio/download) downloaded.
You can find the code for this chapter at https://github.com/PacktPublishing/Mastering-Kotlin-for-Android/tree/main/chaptertwo.
Developed by Google, Android Studio is the official IDE for creating Android applications. Built upon JetBrains’ IntelliJ IDEA, it provides a comprehensive platform for Android app development. It has all the features to enable you to develop Android apps with ease.
Once you download Android Studio from the official site, you need to download SDKs and set everything up for it to be ready for use. Open your newly installed Android Studio. You will see the following welcome screen:
Figure 2.1 – Android Studio welcome screen
On the top right, we have these quick options:
New Project: We use this to create new projects in Android Studio.Open: We use this when we want to open existing projects using Android Studio.Get from VCS: VCS stands for version control system. Examples of VCSs are GitHub, GitLab, and Bitbucket. We can always link our accounts and easily import projects that are hosted on VCS to our Android Studio.More options icon: This provides us with more options, such as Profile or Debug APK, Import Project, Import an Android Code Sample, SDK Manager, and Virtual Device Manager. We only use these options as needed, so we are not going to dive deep into them at this point.Now, let us look at the navigation options on the left:
Projects: This is selected by default. It shows all the projects that you have created with Android Studio if present. If you do not have any, an empty screen will be displayed.Customize: This provides a settings screen to customize the various aspects of Android Studio, as shown in the following screenshot:Figure 2.2 – Customize Android Studio screen
From the preceding screenshot, we can see that we can quickly customize the following:
Color theme: We can set the theme to dark (Dracula), light (InteliJ Light), or high contrast depending on our preferences.IDE font: Here we set the preferred font size for our IDE.Keymap: Here we configure what IntelliJ should use for mapping our keyboard and mouse shortcuts. It automatically picks the one for our operating system.At the bottom of this screen, we can see two more settings options. One is Import Settings…, which we use when we want to import settings from either a previous Android Studio installation or a custom file. The other one is All settings…, which provides more customization options.
Plugins: Here, we can install external plugins to our Android Studio and also manage our installed plugins. There are several plugins in the Marketplace that we can install depending on the needs.Figure 2.3 – Plugins screen
Now that we have an overview of several essential options on the Android Studio welcome screen, we will use the New Project option to create our first Android app in the next section.
Follow these steps to create your first Android app:
Tap on the New Project button, which will take you to the Templates screen, as shown in the following figure:Figure 2.4 – New Project | Templates screen
The IDE presents us with a variety of options to choose from while creating new projects, as seen in Figure 2.4. To start with, on the right-hand side, we need to choose the specific form factor that we are targeting. By default, Phone and Tablet is selected. We have other options, such as Wear OS if we want to target wearables, Android TV if we want to develop apps that run on Smart TVs powered by Android OS, and, lastly, Automotive, for apps that target Android Auto.
We are going to use the default option since we want to target Android and tablet devices.
Next, we have to choose a template from the options provided. There are several templates that we can use to quickly generate some functionality for our apps. For example, we have Bottom Navigation View Activity to generate a project with both UI and Koltin code for displaying bottom tabs.
We will choose Empty Activity as we want to start from scratch. We use this instead of the No Activity option since this comes with some dependencies set up for us.Tap Next, and we’ll see the screen to configure the project details, as follows:Figure 2.5 – New Project settings
As seen in the preceding screenshot, to finalize creating the project, we need to specify the following:Name: This is the unique name for our project.Package name: This is a unique identifier for our project. Normally it’s a combination of the company website and app name.Save location: Here we specify the directory that our project will be in.Minimum SDK: This is the minimum Android version that our Android app will support. Android Studio gives us the percentage of devices using all the versions to help us decide the minimum Android version to support. For our project, we’ve chosen API 24: Android 7.0 (Nougat), which will run on approximately 94% of devices. It is important to note that choosing a lower minimum SDK version means we will have to make our app compatible across the different device versions, which can be a lot of work. Also, some features are only available in newer SDK versions, so we have to add a fallback mechanism for devices forLastly, tap Finish—this creates our project. It will take a couple of minutes to prepare our project. Once done, we’ll be presented with the following screen:Figure 2.6 – New project
There are a few things about the project structure we need to understand here. We will be diving deep into them in the next section.
In this subsection, we are going to look at the whole project structure so that we can understand the different components.
On the left, we have the project structure
