Book Image

Kotlin Blueprints

By : Ashish Belagali, Akshay Chordiya, Hardik Trivedi
Book Image

Kotlin Blueprints

By: Ashish Belagali, Akshay Chordiya, Hardik Trivedi

Overview of this book

Kotlin is a powerful language that has applications in a wide variety of fields. It is a concise, safe, interoperable, and tool-friendly language. The Android team has also announced first-class support for Kotlin, which is an added boost to the language. Kotlin’s growth is fueled through carefully designed business and technology benefits. The collection of projects demonstrates the versatility of the language and enables you to build standalone applications on your own. You’ll build comprehensive applications using the various features of Kotlin. Scale, performance, and high availability lie at the heart of the projects, and the lessons learned throughout this book. You’ll learn how to build a social media aggregator app that will help you efficiently track various feeds, develop a geospatial webservice with Kotlin and Spring Boot, build responsive web applications with Kotlin, build a REST API for a news feed reader, and build a server-side chat application with Kotlin. It also covers the various libraries and frameworks used in the projects. Through the course of building applications, you’ll not only get to grips with the various features of Kotlin, but you’ll also discover how to design and prototype professional-grade applications.
Table of Contents (16 chapters)
Title Page
Credits
About the Authors
Acknowledgments
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Preface

Kotlin is evolving rapidly as a universal language—a single language with which one can do many things and do it elegantly! It can be used to create a wide range of applications, spanning from large server applications that can take advantage of the most modern advances in parallel processing and rich internet applications (RIA) that run in the ecosystem of a web browser to Android apps and tiny applications that run within tiny IoT processors.

In May 2017, when Google announced official support to Kotlin to develop Android applications, the status of Kotlin was elevated overnight from a nicecool language to a language that needs to be taken seriously. Shortly thereafter, the Spring framework, which is mighty on the server-side development, added Kotlin-only features. The Kotlin-favoring trend seems to have continued since then.

The vast array of applications that Kotlin can be used in and the value-add that Kotlin does in each of the cases with Kotlin-specific flavors can be quite overwhelming. This book is written to help the programmers find these in one place so as to put their hands around the diverse use case scenarios.

While there are books and plenty of online material covering the language basics, and then there are those covering single niche areas, there was nothing that could be referred to for understanding the vast spectrum of usage scenarios. This book tries to fill the void. This is a single book that can be used as a reference to these various scenarios.

This is not a theory book. It is a practical guide to creating industry-grade applications with Kotlin. Each chapter takes one moderately sized requirement in a given area and shows how to create a Kotlin application to fulfill it. It contains a complete set of instructions that a programmer can follow and learn the applications by coding them first hand. It also highlights the special uses of Kotlin features as they are applicable to the problem at hand.

Except for the first introductory chapter, all other chapters are independent of one another. You can, therefore, jump straight to the chapter corresponding to the application area that would be the most relevant for you, and dive deep into it to learn the skill quickly and put it to practice. It, thus, helps in accelerating your understanding and increasing the productivity in a short time.

We hope that the fast-growing community of Kotlin programmers will find this book immensely useful.

What this book covers

Chapter 1, The Power of Kotlin, is the introductory chapter. It covers why Kotlin is quickly becoming a force to reckon with. Kotlin positions itself as the smart choice to the various stakeholders, such as the programmer, the manager, and the businesses. The chapter looks at the technology and business reasons that fuel the adoption of Kotlin. 

Chapter 2, Geospatial Messenger – Spring Boot, covers the use of Spring Boot technology to create robust server-side applications. Spring is one of the most well-known and well-respected server-side frameworks in the Java space, and Spring Boot is its less verbose and more powerful version. This chapter covers how a Geospatial Messenger application is created with Spring Boot technology using the Kotlin language.

Chapter 3, Social Media Aggregator Android App, is using Kotlin to create native Android apps. With Google officially recognizing Kotlin's use to develop Android apps, this is the most widespread application to use the Kotlin language. Today's mobile apps hardly work in isolation. They work with a server. This common scenario is illustrated with a social media aggregator app.

Chapter 4, Weather App Using Kotlin for JavaScript, explores the use of Kotlin to create rich internet apps that work within the browser. JavaScript is clearly the de facto standard language that works across all the browsers. However, Kotlin is clearly superior to JavaScript in many ways. This chapter shows how one can have the best of both worlds by doing the coding in Kotlin and, then, transpiling the code to JavaScript so that it works with the browsers seamlessly. The example that we will build is a simple weather application.

Chapter 5, Chat Application with Server-Side JavaScript Generation, explores how the same facility of transpiling Kotlin to Javascript can be used on the server side. JavaScript is used on the server side within the popular and fast Node.js framework. Kotlin can be used to create Node.js applications, as the JavaScript code that runs with Node.js can be generated from it. This chapter shows how to do so by creating a simple chat application.

Chapter 6, News Feed – REST API, covers the use of Kotlin specifically to create REST services. We will develop a News Feed application with the Ktor framework, which is a leading Kotlin-only framework used for server-side applications.

Chapter 7, CSV Reader in Kotlin Native, explores a bleeding edge technology—Kotlin Native—which is about compiling Kotlin code directly to platform-specific executables. Although not mature, Kotlin Native is worth watching as it quickly marches to its promise of becoming the only language to create native applications across disparate platforms such as iOS and Raspberry Pi. In this chapter, a small CSV reader utility is built with Kotlin/Native.

Chapter 8, Dictionary Desktop Application - Tornado FX, is about using Kotlin to create a cross-platform desktop application based on Java technology. Tornado FX is a Kotlin-specific framework, which is based on the most advanced Java GUI framework, that is, Java FX. This chapter illustrates the power of Tornado FX with a dictionary application.

What you need for this book

You will need to have the following:

  1. JDK8 (download it from http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html)
  2. An IDE with the Kotlin plugin—you can use Eclipse (http://www.eclipse.org) and install the Kotlin plugin, or IntelliJ idea (https://www.jetbrains.com/idea/download/). The latter is more popular in the Kotlin community and is used in most of the chapters. The community edition is enough for most of the chapters, unless specified otherwise in the chapter.
  3. Additional plugins may be needed to be installed into the IDE as specified in the respective chapter.

Who this book is for

This practical guide is for programmers who are already familiar with Kotlin. If you are familiar with Kotlin and want to put your knowledge to work, then this is the book for you. Kotlin programming knowledge is a must.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in the text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: " We will break the Result object returned from the function and catch it inside two objects named result and status."

A block of code is set as follows:

    var createTweet: Button? = null
    createTweet.setOnClickListener({
      // Do button click operation
    })

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

    fun ViewGroup?.inflate(layoutId: Int, attachToRoot: Boolean) =  
     LayoutInflater.from(this?.context).inflate(layoutId, this, 
     attachToRoot)

Any command-line input or output is written as follows:

npm init

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Create a new project in IntelliJ, select Gradle and add the dependencies related to Spring Boot and Kotlin."

Note

Warnings or important notes appear in a box like this.

Note

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply email [email protected], and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your email address and password.
  2. Hover the mouse pointer on the SUPPORT tab at the top.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box.
  5. Select the book for which you're looking to download the code files.
  6. Choose from the drop-down menu where you purchased this book from.
  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows
  • Zipeg / iZip / UnRarX for macOS
  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Kotlin-Blueprints. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/KotlinBlueprints_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.