Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
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
Free Trial
Paperback Jan 2019 392 pages 1st Edition
eBook
NZ$44.99 NZ$64.99
Paperback
NZ$80.99
Subscription
Free Trial
Arrow left icon
Profile Icon Sunnat Profile Icon Igor Kucherenko
Arrow right icon
Free Trial
Paperback Jan 2019 392 pages 1st Edition
eBook
NZ$44.99 NZ$64.99
Paperback
NZ$80.99
Subscription
Free Trial
eBook
NZ$44.99 NZ$64.99
Paperback
NZ$80.99
Subscription
Free Trial

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Learn Spring for Android Application Development

About the Environment

The title of this book makes reference to two of the greatest stage names—Spring, ostensibly the best framework of Java, and Android, which has the greatest number of clients of any operating system. This book will help to you learn and develop a product-ready application on your own which will be lightweight, secure, powerful, and responsive.

Before start learning about the Spring and Android, we will demonstrate examples and code from Kotlin, as this programming language is very new to developers. These days, Kotlin is so popular that Google has declared it the official language of Android. Moreover, the Spring language also supports Kotlin. In this book, we will figure out how to make a robust, secure, and intense server dependent on Spring in the Kotlin language, and use the substance and utilize of this server in an Android application as a client.

In this chapter, you will learn how to set up the environment to create Spring and Android projects, including the required tools and applications. This will include going through steps with accompanying images for visualization purposes. The developers who know Java, at that point, will have some leeway since it is the common platform among Spring and Kotlin. We will demonstrate the code and models with Kotlin that runs on JVM. The Kotlin is designed by JetBrains. On the off-chance that you are new to Kotlin and Spring, being familiar with Java will allow you to write code in Kotlin with ease.

The following topics will be covered in this chapter:

  • Setting up the environment
  • Spring
  • Java
  • Kotlin
  • Apache Tomcat
  • Integrated development environments
  • Android

Technical requirements

To run these frameworks, we need some tools and a specific operating system. Here is the list of these:

  • Operating system: Linux and macOS are recommended for development because we can find all the required packages for these OSes and they are lighter than Windows.
  • IDE: My recommended IDE is IntelliJ IDEA (Ultimate version). This is the best IDE for Java, but you have to purchase it to use it. You can also use Eclipse and Netbeans; only one of these is necessary to develop Spring applications. We will show all the projects in IntelliJ, but we will also learn the setup of the environments for Spring in both IntelliJ IDEA and Eclipse.

You can find all the examples from this chapter on GitHub: https://github.com/PacktPublishing/Learn-Spring-for-Android-Application-Development/.

Setting up the environment

An environment setup is one of the prime parts before developing an application. To the developers who are currently working with Spring, feel free to skip this part. This section is for new developers, who need to set up the foundation and the instruments to begin developing.

Here are the steps of how to set up the environment in the accompanying segments.

Spring

Spring is the most powerful Java application framework; it is currently the most popular in the enterprise world. It helps to create high-performing applications that have easily-testable and reusable code. This is open source and was written by Rod Johnson, first released under the Apache 2.0 license in June 2003.

To create and run Spring applications, you need some tools and language supports. You also need a server to test and run your project in your operating system. We will show you how to set up the environment for Spring.

The following software and tools are needed with the current version:

  • Java (version 1.8)
  • Kotlin (version 1.3)
  • Apache Tomcat (version 9.0.11)
  • IntelliJ Ultimate (version 2018.2.2) or Eclipse Photon
  • Spring Framework Libraries (version 5.0.8.RELEASE)

Java

Java is available in two editions:

  • Standard Edition (J2SE)
  • Enterprise Edition (J2EE)

Here, we will opt for Standard Edition. Java is free to download and use for all operating systems.

You can download Java 10.0.2 from http://www.oracle.com/technetwork/java/javase/downloads/index.html.
Download for your operating system.

After installation, please check whether Java is installed. To check, open your Terminal and type java --version. If Java is installed successfully, you will see the following Java version:

Check java version

Alternatively, you will see an error. If this occurs, try to install it again to resolve it.

Kotlin

Developed by JetBrains, Kotlin is an open source and statically-typed programming language. It runs on the Java Virtual Machine (JVM) and can be compiled to JavaScript source code or use the LLVM compiler infrastructure. Kotlin is easy to learn, especially for Java developers.

To use Kotlin, you don't need to download or set it up separately like Java. It comes with the IDEs. Kotlin is a built-in feature of Android Studio, IntelliJ Ultimate, or IntelliJ Community. To use Kotlin in Eclipse, you need to follow these steps:

  1. Go to help -> Eclipse Marketplace from the Eclipse toolbar.
  2. In the search box, write Kotlin, there you will find the Kotlin plugin.
  3. Install it and you can write code in Kotlin:

Eclipse Marketplace
We highly recommend using IntelliJ IDE to implement the latest version of Kotlin. The Eclipse plugin does not have the latest version of Kotlin.

Apache Tomcat

We require a steady, free, and open source web server that we can use to create and run Spring-Framework-based ventures. We will utilize Apache Tomcat, which is easy to understand for all developers of Java. You can also use Jetty or Undertow to develop in Spring.

Tomcat is an open source web server. This allows the utilization of Java Servlets and JavaServer Pages (JSP) for the Java server. The core segment of Tomcat is Catalina.

Apache Tomcat is a web server and not an application server.

You can download Tomcat 9.0.11 from https://tomcat.apache.org/download-90.cgi.

If you use Tomcat version 9, you have to use Java version 8 or later. According to the Apache Tomcat source, this version builds on Tomcat 8.0.x and 8.5.x, and implements the Servlet 4.0, JSP 2.3, EL 3.0, WebSocket 1.1, and JASPIC 1.1 specifications (the versions required by the Java EE 8 platform).

Let's see how to configure and verify the Tomcat server.

Configuring Tomcat

You can configure the Tomcat server in two ways—either using the Terminal or from the IDE. To set up the server, you have to download the Tomcat server's content from https://tomcat.apache.org/download-90.cgi.

Configuring Tomcat by these following steps:

  1. Download a binary distribution of the core module from the link.
  2. Extract the file. This creates a folder named apache-tomcat-9.0.11 (version number can be changed).

  1. To access it with ease, rename the folder Tomcat and move it to /usr/local (for Linux) or /Library (for macOS):
Project files
  • For Linux, use these steps:
// If you have an older version of Tomcat, then remove it before using the newer one
sudo rm -rf /usr/local/Tomcat // To remove exist TomCat

sudo mv ~/Download/Tomcat /usr/local // To move TomCat from the download directory to your desire direction
  • For macOS, use these steps:
// If you have an older version of Tomcat, then remove it before using the newer one
sudo rm -rf /Library/Tomcat // To remove exist TomCat

sudo mv Downloads/Tomcat /Library/ // To move TomCat from the download directory to your desire direction

To check the current directory, type the following:

  • For Linux: cd /usr/local/Tomcat/
  • For macOS: cd /Library/Tomcat/
  1. Type ls to see a list of this directory:
check tomcat files in terminal
  1. Change the ownership of the /usr/local/Tomcat or /Library/Tomcat folder hierarchy:
  • For Linux: sudo chown -R <your_username> /usr/local/Tomcat/
  • For macOS: sudo chown -R <your_username> /Library/Tomcat/
  1. Make all scripts executable:
  • For Linux: sudo chmod +x /usr/local/Tomcat/bin/*.sh
  • For macOS: sudo chmod +x /Library/Tomcat/bin/*.sh
  1. To check the contents of Tomcat, use the following command:
  • For Linux: ls -al /usr/local/Tomcat/bin/*.sh
  • For macOS: ls -al /Library/Tomcat/bin/*.sh
  1. You can see that every file is listed with -rwxr-xr-x@, where -x means executable. Executable demonstrates to us the authorization status to get to the files:
Check the tomcat executable files in terminal
  1. To start and stop, type the following:
  • For macOS:
/Library/Tomcat/bin/startup.sh
/Library/Tomcat/bin/shutdown.sh
  • For Linux:
/usr/local/Tomcat/bin/startup.sh
/usr/local/Tomcat/bin/shutdown.sh
  1. To turn on and off the Tomcat server, use this command:

Verifying Tomcat

  1. After starting the server, go to your browser and enter http://localhost:8080, which will show you the default page:
Default tomcat local hosting

This is how we can configure Tomcat from the Terminal.

Integrated development environment

When it comes to writing Java programs, you can use any text editor. However, we encourage you to use an integrated development environment (IDE) because they provide numerous features. IntelliJ IDEA, Eclipse, and NetBeans are the best of them. IntelliJ is a paid IDE, but you can use Eclipse or NetBeans, which are free.

We can use IDE to do the following:

  • Manage Tomcat
  • Develop apps and web apps where there is no need to remember the full name of the methods and signatures
  • Highlight compile errors

In this book, we will work with Eclipse and IntelliJ IDEA.
You can download the Ultimate version, which has a 30-day free trial, from https://www.jetbrains.com/idea/download/.

To download the Eclipse, visit http://www.eclipse.org/downloads/packages/.

For Spring, you should download Eclipse IDE for the Java EE Developers version.

For both, once you start IDE, it will ask for a workspace. You can create a folder of your choice and give the path of that folder.

IntelliJ IDEA

IntelliJ IDEA is a Java coordinated development environment for developing computer software. It is developed by JetBrains and is accessible as an Apache 2 Licensed people group release and in a restrictive business version. Both can be utilized for business development.

The latest version of Kotlin comes built-in with IntelliJ IDEA ultimate and IntelliJ IDEA community.

Eclipse

Eclipse is an incorporated development environment utilized in computer programming and is the most generally-utilized Java IDE. It contains a base workspace and an extensible module framework for tweaking the environment. Eclipse is composed generally in Java and its essential utility is for developing Java applications, yet it might likewise be utilized to develop applications in other programming dialects by means of modules, including Ada, ABAP, C, C++, C#, Clojure, COBOL, D, Erlang, Fortran, Groovy, Haskell, JavaScript, Julia, Lasso, Lua, NATURAL, Perl, PHP, Prolog, Python, R, Ruby (including the Ruby on Rails framework), Rust, Scala, and Scheme.

To use Kotlin in Eclipse, you will need to install the Kotlin plugin.

Eclipse doesn't have the latest version of Kotlin.

After creating a project, you’ll need to integrate the Tomcat server manually. However, if you use Spring Boot, you don't need to do anything because this comes with the Tomcat server.

Follow these steps to create a web project and implement the Tomcat server into your project:

  1. Visit new > New Dynamic Web Project.
  2. Provide a Project Name.
  3. To integrate Tomcat, click New Runtime:

new project create
  1. Download version 9+, select Apache Tomcat v9.0, and click Finish:
tomcat version selection
  1. Select the latest Dynamic web module version.
  2. Click Finish.

You will find these files after creating the project:

Project files
  1. Go to the Server tab, which is in the bottom-left window:
project IDE interface
  1. Select Tomcat v9.0 Server at localhost.
  2. Hit the start button.
  3. Once the server is started, verify it by visiting http://localhost:8080 in a browser.
  4. If everything is OK, you can start and stop the Tomcat server from here.

Android

Android is a mobile operating system developed by Google, in light of an altered form of the Linux kernel and other open source software and designed basically for touchscreen mobile gadgets, for example, cell phones and tablets. What's more, Google has additionally developed Android TV for televisions, Android Auto for vehicles, and Wear OS for wristwatches, each with a specific UI. Variations of Android are likewise utilized on IoT, advanced cameras, PCs, and various hardware. It was first developed by Android Inc., which Google purchased in 2005, and Android was disclosed in 2007. The first commercial Android devices were launched in September 2008. The current version has since experienced numerous significant discharges, with the present variant being 9 Pie, released in August 2018. The core Android source code is known as Android Open Source Project (AOSP) and is authorized under the Apache License.

In this book, we will figure out how to create a REST API, security, and a database in a Spring platform on a server. We will also learn how to make an Android application and retrieve data from the server, as well as its utilization as a client.

Android Studio is the main IDE among the different IDEs to make an Android application. This is the official IDE for Android. This is based on the IntelliJ IDEA of JetBrains, which is structured especially for Android application development.

To download Android Studio, visit https://developer.android.com/studio/. Here, you will find the latest version of Android Studio to download. The best part is that this includes JRE, the latest SDK, and other important plugins to develop.

Install the Android Studio application after downloading it. This tool is very easy to use.

Don't forget to update and download the latest version of the SDK platform. To update or install a new SDK platform, go to the SDK Manager. In the SDK Platform, you can see the list of all the Android version's platforms.

If you have read and installed the environment without any hassle, you are ready to proceed with learning the information in this book. We have submitted the code on GitHub and shared the link in the Technical requirements section, so you can use that example code.

Summary

This chapter is mainly for those developers who are new to this platform. We have shown the setup procedure using some specific tools and applications and you can also develop your project with different tools and applications. We have looked at how to set up an environment to develop Spring and Android. You are now familiar with all the required tools and software. Now can you configure the Tomcat server in your OS and familiarize yourself with how to start and stop the server. You can decide which IDE you need for developing. We also learned the installation procedure for Android Studio without any hassle. Lastly, there are no criteria to use the latest version of the tools or software.

In the next chapter, we will explore Kotlin, which is a statically-typed programming language and the official language for Android.

Questions

  1. Is the Spring Framework built on Java SE or Java EE?

  2. What are the alternative IDEs of Eclipse and IntelliJ IDEA for developing Spring?

  3. Is Tomcat a web server or an application server?

  4. What are the alternatives of the Tomcat server for running Spring?

  5. Is Android Studio the IDE to develop Android?

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 : 9781789349252
Vendor :
Pivotal
Category :
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.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
$199.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 NZ$7 each
Feature tick icon Exclusive print discounts
$279.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 NZ$7 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total NZ$ 194.97
Android Programming with Kotlin for Beginners
NZ$56.99
Hands-On Data Structures and Algorithms with Kotlin
NZ$56.99
Learn Spring for Android Application Development
NZ$80.99
Total NZ$ 194.97 Stars icon
Banner background image

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

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.