Peter Spth
Pro Android with Kotlin Developing Modern Mobile Apps
Peter Spth
Leipzig, Germany
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the books product page, located at www.apress.com/9781484238196 . For more detailed information, please visit www.apress.com/source-code .
ISBN 978-1-4842-3819-6 e-ISBN 978-1-4842-3820-2
https://doi.org/10.1007/978-1-4842-3820-2
Library of Congress Control Number: 2018955831
Peter Spth 2018
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
To Margret.
Preface
Pro Android with Kotlin is an addition to the popular Apress series for Android development targeting the Java platform. With Kotlin as a highly promising new official language in the Android environment, it allows for more elegant programs compared to the Java standard. This book deals with advanced aspects of a modern Android app. With a thorough description of the important parts of Android system internals and professional-level APIs, advanced user interface topics, advanced development topics, in-depth communication surveys, professional-level hardware topics including looking at devices other than smartphones, a troubleshooting part with guidance on how to fix memory and performance problems, and an introduction to app monetizing, the book is an invaluable resource for developers wanting to build state-of-the-art professional apps for modern Android devices.
This book is not meant to be an introduction to the Kotlin language. For this aim, please take a look at the Kotlin web site or any introductory-level book about Kotlin. What you will find here is an attempt to use as many features of Kotlin to write elegant and stable apps using less code compared to Java.
In 2017, Android versions 8.0 and 8.1 were introduced. In a professional environment, writing apps that depend on new Android 8. x features is a bad idea since the worldwide distribution of devices running an 8. x version is well below 10 percent as of the writing of this book. But you can write code targeting versions 4.0 all the way up to 8.0 (thus covering almost 100 percent of Android devices) by introducing branches in your code. This is what you will be doing in this book. I still concentrate on modern 8. x development, but if I use modern features not available to older versions, I will tell you.
Note that this book does not pay much attention to Android versions older than 4.1 (API level 16). If you look at the online API documentation, you will find a lot of constructs targeting API levels older than 16. Especially when it comes to support libraries, which were introduced to improve backward compatibility, development gets unnecessarily complicated if you look at API versions older than 16 because the distribution of such devices is less than 1 percent nowadays. This book will just assume you are not interested in such old versions, making it unnecessary to look at such support libraries in many cases and simplifying development considerably.
Introduction
The programs explained in this book, despite their strong affinity to the Kotlin way of thinking, will not be totally mysterious to Java developers or developers of other modern computer languages. One of the design goals of Kotlin is expressiveness, so understanding Kotlin programs requires little effort, even when the programs get shorter. But at some point, you have to pay for maximum brevity with a loss of expressiveness and a loss of readability.
When it comes to deciding what is better, I favor expressiveness over brevity, but be assured that a loquacious programming style is a no-go. In the end, professional developers want to write concise apps because less code means lower costs when it comes to maintenance.
The Transition from Java to Kotlin
Just to whet your appetite, you will now take a look at a really simple appone that lacks a lot of features you would want to see in a more complex and professional appand then rewrite it from Java to Kotlin. The app consists of a single activity and presents an edit field, a button, and a text field that reacts to button presses.
If you want to create it using Android Studio, you initiate a new project, disable Kotlin support, and edit the layout to contain a TextView widget, a Button widget, and an EditText widget. Then assign the IDs edit , btn , and text to them, respectively. The Java code is as follows:
package de.pspaeth.simplejava;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText et = findViewById(R.id.edit);
final Button btn = findViewById(R.id.btn);
final TextView text = findViewById(R.id.text);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String entered = et.getText().toString();
text.setText("You entered '" + entered +
"' and pressed 'Go'");
}
});
}
}
Here are a few notes about the previous Java code: