Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Learn Spring for Android Application Development
Learn Spring for Android Application Development

Learn Spring for Android Application Development: Build robust Android applications with Kotlin 1.3 and Spring 5

Arrow left icon
Profile Icon Sunnat Profile Icon Igor Kucherenko
Arrow right icon
€22.99 €32.99
eBook Jan 2019 392 pages 1st Edition
eBook
€22.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Sunnat Profile Icon Igor Kucherenko
Arrow right icon
€22.99 €32.99
eBook Jan 2019 392 pages 1st Edition
eBook
€22.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€22.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
Table of content icon View table of contents Preview book icon Preview Book

Learn Spring for Android Application Development

Overview of Kotlin

Kotlin is the official Android programming language and is statically typed. It is fully interoperable with Java, meaning that any Kotlin user can use the Java framework and mix commands from both Kotlin and Java without any limitations. In this chapter, we will cover the basics of Kotlin and will look at how to set up the environment. We will also look at its flow structures, such as if { ... } else { ... } expressions and loops. In addition to this, we will look into object-oriented programming for Kotlin, and we will cover classes, interfaces, and objects. Functions will also be covered, along with parameters, constructors, and syntax.

This chapter will cover the following topics:

  • Setting up the environment
  • Build tools
  • Basic syntax
  • Object-oriented programming
  • Functions
  • Control flow
  • Ranges
  • String templates
  • Null safety, reflection, and annotations
...

Technical requirements

Introduction to Kotlin

The 3.0 version of Android Studio was released by Google, and it promoted Kotlin as a first class language for Android development. Kotlin is developed by JetBrains in the same way as the Intellij IDEA platform, which is the basis of Android Studio. This language was released in February 2016, it was in development for five years before it was released. It's easy to gradually convert the code base of a project from Java to Kotlin, and a developer that is familiar with Java can learn Kotlin in a few weeks. Kotlin became popular before its release, because this language is full of features and is designed to interoperate with Java. The following diagram shows how Kotlin and Java code are compiled to the same bytecode:

As you can see, part of our application can be written in Java and another part in Kotlin. The kotlinc compiler compiles Kotlin source...

Setting up the environment

To get started with Android development, you will need to download and install the Java Development Kit (JDK) from http://www.oracle.com/technetwork/java/javase/downloads/index.html. You will also need to download and install the Android Studio Integrated Development Environment (IDE), from https://developer.android.com/studio/.

To create a new project, launch Android Studio and press Start a new Android Studio project. Then, you should type a project name and your unique application ID, as shown in the following screenshot:

In the preceding screenshot, the Application name field is filled according to the name of this book, and the Company domain field is packt.com. Android Studio concatenates these two values and creates the Package name identifier that is equal to the application ID identifier. In our case, the application ID is as follows:

com...

Build tools

Android Studio is an official IDE for Android development, and it is based on the Intellij IDEA platform and uses the Gradle build tool system. A typical project structure looks as follows:

The build.gradle file contains the project configuration and manages the library dependencies. To add a dependency to the Spring for Android extension, we should add the following lines:

repositories {
maven {
url 'https://repo.spring.io/libs-milestone'
}
}

dependencies {
//.......
implementation 'org.springframework.android:spring-android-rest-template:2.0.0.M3'
}

Basic syntax

Syntax is a significant part of the programming language, defining a set of rules that must be applied to combinations of symbols. Otherwise, a program can't be compiled, and will be considered incorrect.

This section will describe the basic syntax of Kotlin, covering the following topics:

  • Defining packages
  • Defining variables
  • Defining functions
  • Defining classes

Defining packages

Packaging is a mechanism that allows us to group classes, interfaces, and sub-packages. In our case, a declaration of a package in a file may look as follows:

package com.packt.learn_spring_for_android_application_development

All citizens of the file belong to this package and must be located in the appropriate folder.

...

Object-oriented programming

Object-oriented programming is a model of programming language that is based on objects that can represent data. Kotlin supports object-oriented programming in the same way that Java does, but even more strictly. This is because Kotlin doesn't have primitive types and static members. Instead, it provides a companion object:

class Bar {
companion object {
const val NAME = "Igor"

fun printName() = println(NAME)
}
}

The companion object is an object that is created once, during class initialization. In Kotlin, we can refer to members of companion object in the same way as static in Java:

fun test() {
Bar.NAME
Bar.printName()
}

However, under the hood, the nested Companion class is created, and we actually use an instance of this class, as follows:

Bar.Companion.printName();

Moreover, Kotlin supports the following...

Functions

To define a function in Kotlin, you have to use the fun keyword, as follows:

fun firstClass() {
println("First class function")
}

The preceding snippet demonstrates that we can declare functions as first class citizens. We can also define functions as class members, as follows:

class A {
fun classMember() {
println("Class member")
}
}

A local function is a function that is declared in another one, as follows:

fun outer() {
fun local() {
println("Local")
}

local()
}

In the preceding snippet, the local function is declared inside of the outer function. The local functions are only available in the scope of a function where they were declared. This approach can be useful if we want to avoid duplicate code inside of a function.

This section will cover the following topics:

  • Functional programming
  • Higher-order...

Control flow elements

In Kotlin, control flow elements are expressions. This is different from Java, in which they are statements. Statements just specify the flow of a program, and don't return any values. This section will cover the following control flow elements:

  • The if { ... } else { ... } expression
  • The when { ... } expression

The if { ... } else { ... } expression

In Kotlin, the if control flow element can be used in the same way as it is used in Java. The following example demonstrates the use of if as a usual statement:

fun ifStatement() {
val a = 4
if (a < 5) {
println(a)
}
}

If you are using the if { ... } else { ... } control flow element as an expression, you have to declare the else...

Ranges

Kotlin supports the concept of ranges, which represent sequences of comparable types. To create a range, we can use the rangeTo methods that are implemented in classes, such as Int, in the following way:

public operator fun rangeTo(other: Byte): LongRange = LongRange(this, other)

public operator fun rangeTo(other: Short): LongRange = LongRange(this, other)

public operator fun rangeTo(other: Int): LongRange = LongRange(this, other)

public operator fun rangeTo(other: Long): LongRange = LongRange(this, other)

So, we have two options for creating a range, as follows:

  • Using the rangeTo method. This may look as follows—1.rangeTo(100).
  • Using the .. operator. This may look as follows—1..100.

Ranges are extremely useful when we work with loops:

for (i in 0..100) {
// .....
}

The 0..100 range is equal to the 1 <= i && i <= 100 statement.

If you want...

String templates

Kotlin supports one more powerful feature—string templates. Strings can contain code expressions that can be executed, and their results concatenated to the string. The syntax of the string template assumes that we use the $ symbol at the start of an expression. If the expression contains some evaluation, it has to be surrounded by curly braces.
The simplest use of string templates looks like the following:

var number = 1
val string = "number is $number"

A more advanced example that contains an expression is as follows:

val name = "Igor"
val lengthOfName = "length is ${name.length}"

As you can see, the string templates feature allows us to write code in a more concise way than the usual concatenation or the StringBuilder class.

Null safety, reflection, and annotations

Although we have already covered the most common topics that relate to a basic overview of Kotlin, there are a few more topics that have to be touched upon.

This section will introduce the following topics:

  • Null safety
  • Reflection
  • Annotations

Null safety

Kotlin supports a more strict type system when compared to Java, and divides all types into two groups, as follows:

  • Nullable
  • No-nullable

One of the most popular causes of an app crashing is the NullPointerException. This happens as a result of accessing a member of a null reference. Kotlin provides a mechanism that helps us to avoid this error by using a type system.

The following diagram shows what the class hierarchy looks like...

Summary

In this chapter, we took a close look at the basic syntax of Kotlin. We also introduced and looked at examples of some features, such as lambdas, string templates, and ranges. Furthermore, you learned that control flow elements, such as if { ... } else { ... } and when { ... }, can be used as expressions that can make our code more concise and readable.

In the next chapter, we will take a look at an overview of the Spring framework.

Questions

  1. What is Kotlin?
  2. How does Kotlin support object-oriented programming?
  3. How does Kotlin support functional programming?
  4. How do we define variables in Kotlin?
  5. How do we define functions in Kotlin?

Further reading

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Build native Android applications with Spring for Android
  • Explore Reactive programming, concurrency, and multithreading paradigms for building fast and efficient applications
  • Write more expressive and robust code with Kotlin using its coroutines and other latest features

Description

As the new official language for Android, Kotlin is attracting new as well as existing Android developers. As most developers are still working with Java and want to switch to Kotlin, they find a combination of these two appealing. This book addresses this interest by bringing together Spring, a widely used Java SE framework for building enterprise-grade applications, and Kotlin. Learn Spring for Android Application Development will guide you in leveraging some of the powerful modules of the Spring Framework to build lightweight and robust Android apps using Kotlin. You will work with various modules, such as Spring AOP, Dependency Injection, and Inversion of Control, to develop applications with better dependency management. You’ll also explore other modules of the Spring Framework, such as Spring MVC, Spring Boot, and Spring Security. Each chapter has practice exercises at the end for you to assess your learning. By the end of the book, you will be fully equipped to develop Android applications with Spring technologies.

Who is this book for?

If you’re an aspiring Android developer or an existing developer who wants to learn how to use Spring to build robust Android applications in Kotlin, this book is for you. Though not necessary, basic knowledge of Spring will assist with understanding key concepts covered in this book.

What you will learn

  • Get to grips with the basics of the Spring Framework
  • Write web applications using the Spring Framework with Kotlin
  • Develop Android apps with Kotlin
  • Connect a RESTful web service with your app using Retrofilt
  • Understand JDBC, JPA, MySQL for Spring and SQLite Room for Android
  • Explore Spring Security fundamentals, Basic Authentication, and OAuth2
  • Delve into Concurrency and Reactive programming using Kotlin
  • Develop testable applications with Spring and Android

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jan 31, 2019
Length: 392 pages
Edition : 1st
Language : English
ISBN-13 : 9781789341911
Vendor :
Pivotal
Category :
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning

Product Details

Publication date : Jan 31, 2019
Length: 392 pages
Edition : 1st
Language : English
ISBN-13 : 9781789341911
Vendor :
Pivotal
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 101.97
Android Programming with Kotlin for Beginners
€29.99
Hands-On Data Structures and Algorithms with Kotlin
€29.99
Learn Spring for Android Application Development
€41.99
Total 101.97 Stars icon

Table of Contents

12 Chapters
About the Environment Chevron down icon Chevron up icon
Overview of Kotlin Chevron down icon Chevron up icon
Overview of Spring Framework Chevron down icon Chevron up icon
Spring Modules for Android Chevron down icon Chevron up icon
Securing Applications with Spring Security Chevron down icon Chevron up icon
Accessing the Database Chevron down icon Chevron up icon
Concurrency Chevron down icon Chevron up icon
Reactive Programming Chevron down icon Chevron up icon
Creating an Application Chevron down icon Chevron up icon
Testing an Application Chevron down icon Chevron up icon
Assessments Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.