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
- Apache Tomcat
- Integrated development environments
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/.
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 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 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:
Alternatively, you will see an error. If this occurs, try to install it again to resolve it.
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:
- Go to help -> Eclipse Marketplace from the Eclipse toolbar.
- In the search box, write Kotlin, there you will find the Kotlin plugin.
- Install it and you can write code in Kotlin:
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.
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.
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:
- Download a binary distribution of the core module from the link.
- Extract the file. This creates a folder named apache-tomcat-9.0.11 (version number can be changed).
- To access it with ease, rename the folder Tomcat and move it to /usr/local (for Linux) or /Library (for macOS):
- 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/
- Type ls to see a list of this directory:
- 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/
- Make all scripts executable:
- For Linux: sudo chmod +x /usr/local/Tomcat/bin/*.sh
- For macOS: sudo chmod +x /Library/Tomcat/bin/*.sh
- 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
- 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:
- To start and stop, type the following:
- For macOS:
- For Linux:
- To turn on and off the Tomcat server, use this command:
- After starting the server, go to your browser and enter http://localhost:8080, which will show you the default page:
This is how we can configure Tomcat from the Terminal.
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 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.
To use Kotlin in Eclipse, you will need to install the Kotlin plugin.
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:
- Visit new > New Dynamic Web Project.
- Provide a Project Name.
- To integrate Tomcat, click New Runtime:
- Download version 9+, select Apache Tomcat v9.0, and click Finish:
- Select the latest Dynamic web module version.
- Click Finish.
You will find these files after creating the project:
- Go to the Server tab, which is in the bottom-left window:
- Select Tomcat v9.0 Server at localhost.
- Hit the start button.
- Once the server is started, verify it by visiting http://localhost:8080 in a browser.
- If everything is OK, you can start and stop the Tomcat server from here.
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.
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.
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.
Is the Spring Framework built on Java SE or Java EE?
What are the alternative IDEs of Eclipse and IntelliJ IDEA for developing Spring?
Is Tomcat a web server or an application server?
What are the alternatives of the Tomcat server for running Spring?
Is Android Studio the IDE to develop Android?
- Mastering Spring 5.0 (https://www.packtpub.com/application-development/mastering-spring-50) by Ranga Karanam