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
- Â Java
- Â Kotlin
- 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)
- Standard Edition (J2SE)
- Enterprise Edition (J2EE)
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.
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.
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Â
Tomcatand 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:
- For macOS:
lsto see a list of this directory:
check tomcat files in terminal
- Change the ownership of the
- 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
[email protected], where
-xmeans executable. Executable demonstrates to us the authorization status to get to the files:
Check the tomcatÂ executableÂ files in terminal
- To start and stop, type the following:
- For macOS:
- For Linux:
- To turn on and off the Tomcat server, use this command:
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
- To integrate Tomcat, clickÂ
new project create
- Download version 9+, select
Apache Tomcat v9.0, and click
tomcat version selection
- Select the latest
Dynamic web module version.
- Go to the
Servertab, which is in the bottom-left window:
project IDE interface
Tomcat v9.0 Server at localhost.
- Hit the start button.
- Once the server is started, verify it by visitingÂ
http://localhost:8080in 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.
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Â theTechnical 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