How to Build Android Apps with Kotlin: A hands-on guide to developing, testing and publishing your first apps with Android
English | 2021 | ISBN: 978-1838984113 | 750 Pages | PDF, EPUB, MOBI | 112 MB
Master the fundamentals of Android programming and apply your skills to create scalable and reliable apps using industry best practices
Are you keen to get started building Android 11 apps, but don’t know where to start? How to Build Android Apps with Kotlin is a comprehensive guide that will help kick-start your Android development practice.
This book starts with the fundamentals of app development, enabling you to utilize Android Studio and Kotlin to get started building Android projects. You’ll learn how to create apps and run them on virtual devices through guided exercises. Progressing through the chapters, you’ll delve into Android’s RecyclerView to make the most of lists, images, and maps, and see how to fetch data from a web service.
Moving ahead, you’ll get to grips with testing, learn how to keep your architecture clean, understand how to persist data, and gain basic knowledge of the dependency injection pattern. Finally, you’ll see how to publish your apps on the Google Play store.
You’ll work on realistic projects that are split up into bitesize exercises and activities, allowing you to challenge yourself in an enjoyable and attainable way. You’ll build apps to create quizzes, read news articles, check weather reports, store recipes, retrieve movie information, and remind you where you parked your car.
By the end of this book, you’ll have the skills and confidence to build your own creative Android applications using Kotlin.
What you will learn
- Create maintainable and scalable apps using Kotlin
- Understand the Android development lifecycle
- Simplify app development with Google architecture components
- Use standard libraries for dependency injection and data parsing
- Apply the repository pattern to retrieve data from outside sources
- Publish your app on the Google Play store
Homepage
Источник
How to build android apps with kotlin pdf
How to Build Android Apps with Kotlin
This is the repository for How to Build Android Apps with Kotlin, published by Packt. It contains all the supporting project files necessary to work through the course from start to finish.
Requirements and Setup
Each great journey begins with a humble step. Before we can do awesome things in Android, we need to be prepared with a productive environment. In this section, we will see how to do that.
Minimum Hardware Requirements
For an optimal learning experience, we recommend the following hardware configuration:
- Processor: Intel Core i5 or equivalent or higher
- Memory: 4 GB RAM minimum; 8 GB RAM recommended
- Storage: 4 GB available space
You’ll also need the following software installed in advance:
- OS: Windows 7 SP1 64-bit, Windows 8.1 64-bit or Windows 10 64-bit, macOS, or Linux
- Android Studio 4.1 or higher
About How to Build Android Apps with Kotlin
How to Build Android Apps with Kotlin will equip you to create high-quality, visually appealing Android 11 apps from scratch with Kotlin. You’ll discover a wide range of real-world development challenges faced by developers and explore various techniques to overcome them.
What you will learn
- Create maintainable and scalable apps using Kotlin
- Understand the Android development lifecycle
- Simplify app development with Google architecture components
- Use standard libraries for dependency injection and data parsing
- Apply the repository pattern to retrieve data from outside sources
- Publish your app on the Google Play store
About
A hands-on guide to developing, testing, and publishing your first apps with Android
Источник
Kotlin for Android Developers
Short Description
Descripción: Kotlin for android developers.
Description
Kotlin for Android Developers Learn Kotlin the easy way while developing an Android App Antonio Leiva This book is for sale at http://leanpub.com/kotlin-for-android-developers This version was published on 2017-06-02
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do. © 2015 — 2017 Antonio Leiva
Tweet This Book! Please help Antonio Leiva by spreading the word about this book on Twitter! The suggested hashtag for this book is #kotlinandroiddev. Find out what other people are saying about the book by clicking on this link to search for this hashtag on Twitter: https://twitter.com/search?q=#kotlinandroiddev
This book is dedicated to all the loyal readers of antonioleiva.com, who made me believe that writing about Android development was a powerful tool to help others learn about it. I felt this book as a necessary step forward. I also want to do a special mention to Luis Herrero Jiménez, who has designed the awesome cover of this book, and to Gautier Mechling for helping me so much by reviewing this book. It’s thanks to him that this pages are not full of typos and mistakes. And, of course, this is specially dedicated to you. With your support and your help this book is growing, and I hope it will become a reference. So any claim or suggestion you think it will improve the quality of this book will be welcomed. Feel free to write anytime to [email protected]
Contents I. About this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What is “Kotlin for Android Developers” about . . . . . . . . . . . . . .
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.1 What is Kotlin? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2 What do we get with Kotlin? . . . . . . . . . . . . . . . . . . . . . .
2 Getting ready . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.1 Android Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 Install Kotlin plugin . . . . . . . . . . . . . . . . . . . . . . . . . . .
3 Creating a new project . . . . . . . . . . . . 3.1 Create a new project in Android Studio 3.2 Convert MainActivity to Kotlin code . . 3.3 Configure Kotlin in project . . . . . . . 3.4 Include some other useful configuration 3.4 Test that everything works . . . . . . .
13 13 14 14 15 17
4 Classes and functions . . . . . . . . . . . . 4.1 How to declare a class . . . . . . . . . 4.2 Class inheritance . . . . . . . . . . . . 4.3 Functions . . . . . . . . . . . . . . . . 4.4 Constructor and functions parameters
5 Writing your first class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.1 Creating the layout . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2 The Recycler Adapter . . . . . . . . . . . . . . . . . . . . . . . . . .
6 Variables and properties 6.1 Basic types . . . . . 6.2 Variables . . . . . . 6.3 Properties . . . . . .
7 Anko and Extension Functions 7.1 What is Anko? . . . . . . . 7.2 Start using Anko . . . . . . 7.3 Extension functions . . . .
8 Retrieving data from API . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.1 Performing a request . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.2 Performing the request out of the main thread . . . . . . . . . . . . .
9 Data Classes . . . . . . . . . . . . . . 9.1 Extra functions . . . . . . . . . . 9.2 Copying a data class . . . . . . . 9.3 Mapping an object into variables
10 Parsing data . . . . . . . . . . . . . . 10.1 Converting json to data classes . 10.2 Shaping the domain layer . . . 10.3 Drawing the data in the UI . . .
11 Operator overloading . . . . . . . . . 11.1 Operators tables . . . . . . . . . . 11.2 An example . . . . . . . . . . . . 11.3 Operators in extension functions .
12 Making the forecast list clickable . . . . . . . . . . . . . . . . . . . . . .
13 Lambdas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13.1 Simplifying setOnClickListener() . . . . . . . . . . . . . . . . . . . 13.2 Click listener for ForecastListAdapter . . . . . . . . . . . . . . . . .
13.3 Extending the language . . . . . . . . . . . . . . . . . . . . . . . . . 14 Visibility Modifiers . . . 14.1 Modifiers . . . . . 14.2 Constructors . . . 14.3 Reviewing our code
15 Kotlin Android Extensions . . . . . . . . . . . . . . . . . . . . . . . . . 15.1 How to use Kotlin Android Extensions . . . . . . . . . . . . . . . . 15.2 Refactoring our code . . . . . . . . . . . . . . . . . . . . . . . . . .
16 Application Singleton and Delegated Properties 16.1 Application Singleton . . . . . . . . . . . . . 16.2 Delegated Properties . . . . . . . . . . . . . 16.3 Standard Delegates . . . . . . . . . . . . . . 16.4 How to create a custom delegate . . . . . . . 16.5 Reimplementing App Singleton . . . . . . .
70 70 71 72 76 77
17 Creating an SQLiteOpenHelper . . . 17.1 ManagedSqliteOpenHelper . . . 17.2 Tables definition . . . . . . . . 17.3 Implementing SqliteOpenHelper 17.4 Dependency injection . . . . . .
18 Collections and functional operations 18.1 Aggregate operations . . . . . . . 18.2 Filtering operations . . . . . . . . 18.3 Mapping operations . . . . . . . . 18.4 Elements operations . . . . . . . 18.5 Generation operations . . . . . . 18.6 Ordering operations . . . . . . .
86 87 90 92 93 96 97
19 Saving and requesting data from database . . . . . . . . . . . . . . . . 19.1 Creating database model classes . . . . . . . . . . . . . . . . . . . . 19.2 Writing and requesting data . . . . . . . . . . . . . . . . . . . . . .
20 Null safety in Kotlin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20.1 How Null types work . . . . . . . . . . . . . . . . . . . . . . . . . .
20.2 Nullity and Java libraries . . . . . . . . . . . . . . . . . . . . . . . .
21 Creating the business logic to data access . . . . . . . . . . . . . . . . .
22. Flow control and ranges . . . 22.1 If Expression . . . . . . . 22.2 When expression . . . . . 22.3 For loops . . . . . . . . . . 22.4 While and do/while loops 22.5 Ranges . . . . . . . . . . .
118 118 119 120 121 121
23 Creating a Detail Activity . . . . . . . 23.1 Preparing the request . . . . . . . 23.2 Providing a new activity . . . . . 23.3 Start an activity: reified functions
124 124 127 131
24 Interfaces and Delegation . . . . . . . . . 24.1 Interfaces . . . . . . . . . . . . . . . 24.2 Delegation . . . . . . . . . . . . . . . 24.3 Implementing an example in our App
133 133 134 135
25 Generics . . . . . . . . . 25.1 Basics . . . . . . . 25.2 Variance . . . . . . 25.3 Generics examples
142 142 144 146
26 Settings Screen . . . . . . . . . . . 26.1 Creating the settings activity . 26.2 Accessing Shared Preferences 26.3 Generic preference delegate .
150 150 152 155
27 A first walk into coroutines . . . . . . . . 27.1 Understanding how coroutines work 27.2 Using Anko for coroutines . . . . . . 27.3 Using coroutines in our example . . .
158 158 160 161
28 Testing your App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28.1 Unit testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
28.2 Instrumentation tests . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Extra concepts . . 29.1 Nested classes 29.2 Enum classes 29.3 Sealed classes 29.4 Exceptions . .
174 174 175 176 177
I. About this book Hey! Thanks so much for your interest on this book, I’m really glad you decided to become a Kotlin for Android expert. Many things have happened since I started writing “Kotlin for Android Developers” in mid-2015. At that point, Kotlin was still in an early access edition. But I felt so much power in the language, that I decided to explain all what I was learning in a book. Since then, the language has kept growing to the point that Google announced that they would start supporting Kotlin as an official language to develop Android Apps. These were amazing news. But it was even more amazing that both Google and Jetbrains suggested this book to Android developers that want to learn the language. As you may know, this is a lean publication. This means that the book grew and progressed thanks to the readers comments. So I can only thank you for helping me bring this dream to life.
What is “Kotlin for Android Developers” about In this book, I’ll be creating an Android app from ground up using Kotlin as the main language. The idea is to learn the language by example, instead of following a regular reference book structure. I’ll be stopping to explain the most interesting concepts and ideas about Kotlin, comparing them to Java 6. This way, you can see what the differences are and which parts of the language will help you speed up your work. This book is not meant to be a language reference, but a tool for Android developers to learn Kotlin and be able to continue with their own projects by themselves. I’ll be solving many of the most recurring problems we have to face in our daily lives as App developers, by making use of Kotlin expressiveness and some other really
I. About this book
interesting tools and libraries. However, this text covers most Kotlin features, so by the end of the reading you will have a deep knowledge about the language. The content is very practical, so I recommend you to follow the examples and the code in front of a computer and try everything it’s suggested. You could, however, take a first read to get a broad idea and then dive into practice. Even though this book now finished, I will review it from time to time to keep it up to date with new Kotlin versions. So feel free to write and tell me what you think about the book, or what could be improved. I want this book to be the perfect tool for Android developers, and as such, help and ideas will be welcomed. If you are reading a printed copy and want to receive the latest updates, please feel free to write me back to [email protected] with a proof of purchase, and I’ll give you access to the digital copy. That way, you’ll keep receiving the updates. Thanks for becoming part of this exciting project!
II. Is this book for you? This book is written to be useful to Android developers who are interested in learning Kotlin language. This book is for you if you are in some of the following situations: • You have some basic knowledge about Android Development and the Android SDK, as well as Java language. • You want to learn how to develop Android apps using Kotlin by following an example. • You need a guide on how to solve many of the common challenges Android developers find every day, by using a cleaner and more expressive language. On the other hand, this book may not be for you. This is what you won’t find in it: • This is not a Kotlin Bible. I’ll explain all language basics, and even more complex ideas when they come out during the process, just when we need them. So you will learn by example and not the other way round. • I will not explain how to develop an Android app. You won’t need a deep understanding of the platform, but at least some basics, such as some knowledge of Android Studio, Gradle, Java programming and Android SDK. You may even learn some new Android things in the process! • This is not a guide to learn functional programming. Of course, I’ll explain what you need, as Java 6 is not functional at all, but I won’t dive deep into functional topics.
III. About the author Antonio Leiva is an Android Engineer who spends time learning about new ways to get the most out of Android and then writes about it. He writes a blog at antonioleiva.com¹, focused in helping other Android developers learn Kotlin. He also leads intensive live workshops, where all the information from this book is put into practice. In 10 hours, people steps from no Kotlin knowledge to being able to create their own Apps from scratch. Antonio started as a consultant in CRM technologies, but after some time, looking for his real passion, he discovered the Android world. After getting some experience on such an awesome platform, he started a new adventure at a mobile company, where he led several projects for important Spanish companies. He now works as an Android Engineer at Plex², where he also plays an important role in the design and UX of the Android applications. You can find Antonio on Twitter as @lime_cl³ or Google+ as +AntonioLeivaGordillo⁴. ¹http://antonioleiva.com ²http://plex.tv ³https://twitter.com/lime_cl ⁴http://plus.google.com/+AntonioLeivaGordillo‘
1 Introduction Things are changing for good for Android Developers. The Android team announced during Google I/O 2017 that Kotlin is now an official language to develop Android Apps. This means that, while it’s still possible to develop Android Apps using Java, from now on Kotlin is fully supported and Google will make sure that all new Android features, the framework, the IDE and all their libraries work seamlessly with the new language. Google listened to the community, who was asking for years that Kotlin became a first-party language. So you can now take advantage from all the features of a modern language while developing Android. Throughout this book I’ll show you how, so I hope that I can help you understand the various ways how Kotlin can take you one step ahead and make your code much better. But before diving into the features of the language, let me tell you just a little bit of background.
1.1 What is Kotlin? Kotlin is a JVM based language developed by JetBrains⁵, a company known for creating IntelliJ IDEA, a powerful IDE for Java development. Android Studio, the official Android IDE, is based on IntelliJ. Kotlin was created with Java developers in mind, and with IntelliJ as its main development IDE. And these are two very interesting features for Android developers: • Kotlin is very intuitive and easy to learn for Java developers. Most parts of the language are very similar to what we already know, and the differences in basic concepts can be learnt in no time. ⁵https://www.jetbrains.com/
• We have total integration with our daily IDE for free. Android Studio can understand, compile and run Kotlin code. And the support for this language comes from the company who develops the IDE, so we Android developers are first-class citizens. But this is only related to how the language integrates with our tools. What are the advantages of the language when compared to Java 6? • It’s more expressive: this is one of its most important qualities. You can write more with much less code. • It’s safer: Kotlin is null safe, which means that we deal with possible null situations in compile time, to prevent execution time exceptions. We need to explicitly specify that an object can be null, and then check its nullity before using it. You will save a lot of time debugging null pointer exceptions and fixing nullity bugs. • It’s functional: Kotlin is basically an object oriented language, not a pure functional language. However, as many other modern languages, it uses many concepts from functional programming, such as lambda expressions, to solve some problems in a much easier way. Another nice feature is the way it deals with collections. • It makes use of extension functions: This means we can extend any class with new features even if we don’t have access to the source code. • It’s highly interoperable: You can continue using most libraries and code written in Java, because the interoperability between both languages is excellent. It’s even possible to create mixed projects, with both Kotlin and Java files coexisting.
1.2 What do we get with Kotlin? Without diving too deep into Kotlin language (we’ll learn everything about it throughout this book), these are some interesting features we miss in Java:
Expressiveness With Kotlin, it’s much easier to avoid boilerplate because the most common patterns are covered by default in the language. For instance, in Java, if we want to create a data class, we’ll need to write (or at least generate) this code: 1 2 3 4 5
Источник