Android Application Development
A Beginner's Tutorial
Budi Kurniawan
Android Application Development: A Beginner's Tutorial
First Edition: February 2015
All rights reserved. No part of this book may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage and retrieval system, without written permission from the publisher, except for the inclusion of brief quotations in a review.
ISBN: 9780992133016
Indexer: Chris Mayle
Trademarks
Oracle and Java are registered trademarks of Oracle and/or it's affiliates
UNIX is a registered trademark of the Open Group
Apache is a trademark of The Apache Software Foundation.
Firefox is a registered trademark of the Mozilla Foundation.
Google is a trademark of Google, Inc.
Throughout this book the printing of trademarked names without the trademark symbol is for editorial purpose only. We have no intention of infringement of the trademark.
Warning and Disclaimer
Every effort has been made to make this book as accurate as possible. The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information in this book.
About the Author
Budi Kurniawan is known for his clear writing style. A consultant at Brainy Software Corp., he has written software licensed by Fortune 100 companies and architected and developedlarge scale applications for various organizations around the world. He has also published more than100 articles in prestigious publications. His other books include the popular"How Tomcat Works" and "Servlet and JSP: A Tutorial."
Table of Contents
Introduction
This book is for you if you want to learn Android application development for smart phones and tablets. Android is the most popular mobile platform today and it comes with a comprehensive set of APIs that make it easy for developers to write, test and deploy apps. With these APIs you can easily show user interface (UI) components, play and record audio and video, create games and animation, store and retrieve data, search the Internet, and so on.
The software development kit (SDK) for Android application development is free and includes an emulator, a computer program that can be configured to mimic a hardware device. This means, you can develop, debug and test your applications without physical devices.
This introduction provides an overview of the Android platform and the contents of the book.
Overview
The Android operating system is a multi-user Linux system. Each application runs as a different user in a separate Linux process. As such, an application runs in isolation from other apps.
One of the reasons for Androids rapid ascent to the top is the fact that it uses Java as its programming language. But, is Android really Java? The answer is yes and no. Yes, Java is the default programming language for Android application development. No, Android applications do not run on a Java Virtual Machine as all Java applications do. Instead, up to Android version 4.4 all Android applications run on a virtual machine called Dalvik. In version 5.0 and later, Android sources are ultimately compiled to machine code and applications run with a new runtime called ART (Android Runtime). Android 4.4 was the turning point and shipped with both Dalvik and ART.
As for the development process, initially code written in Java is compiled to Java bytecode. The bytecode is then cross-compiled to a dex ( Dalvik executable) file that contains one or multiple Java classes. The dex file, resource files and other files are then packaged using the apkbuilder tool into an apk file, which is basically a zip file that can be extracted using unzip or Winzip. APK, by the way, stands for application package.
The apk file is how you deploy your app. Anyone who gets a copy of it can install and run it on his or her Android device.
In pre-5.0 versions of Android, the apk file run on Dalvik. In version 5.0 and later, the dex file in the apk is converted into machine code when the application is installed. The machine code is executed when the user runs the application. All of this is transparent to the developer and you do not have to understand intimately the dex format or the internal working of the runtime.
An apk file can run on a physical device or the emulator. Deploying an Android application is easy. You can make the apk file available for download and download it with an Android device to install it. You can also email the apk file to yourself and open the email on an Android device and install it. To publish your application on Google Play, however, you need to sign the apk file using the jarsigner tool. Fortunately, signing an apk is easy with an integrated development environment (IDE), either it is Android Studio or ADT Eclipse.
If youre interested in learning more about the Android build process, this web page explains the Android build process in detail.
https://developer.android.com/tools/building/index.html
Application Development in Brief
Before you embark on a long journey to becoming a professional Android application developer, you should know what lies ahead.
Before starting a project, you should already have an idea what Android devices will be your target. Most applications will target smart phones and tablets. However, the current Android release also allows you to develop apps for smart TVs and wearables. This book, however, is focused on application development for smart phones and tablets.
Then, you need to decide what versions of Android you want to support. Android was released in 2008, but at the time of writing this book there are already 21 API levels available, level 1 to level 21. Of course, the higher the level, the more features are available. However, many older phones and tablets do not run the latest Android and cannot run applications that target higher API levels than what are installed. For example, if youre using features in API level 21, your application will not run in Android devices that support API level 20, let alone API level 2. Fortunately, Android is backward-compatible. Applications written for an earlier version will always run on newer versions. In other words, if you write applications using API level 10, your applications will work in devices that support API level 10 and later. Therefore, you would want to aim the lowest API level possible. This topic will be discussed further in the section to come.
Once you decide what Android devices to target and the API level you should write your program in, you can start looking at the API. There are four types of Android application components:
- Activity : A window that contains user interface components.
- Service : A long running operation that runs in the background.
- B roadcast receiver: A listener that responds to a system or application announcement.
- Content provider: A component that manages a set of data to be shared with other applications.
An application can contain multiple component types, even though a beginner would normally start with an application that has one or two activities. You can think of an activity as a window. You can use Android user interface components or controls to decorate an activity and as a way to interact with the user. If you are using an IDE, you can design an activity by simply dragging and dropping controls around your computer screen.
To encourage code reuse, an application component can be offered to other applications. In fact, you should take advantage of this sharing mechanism to speed up development. For instance, instead of writing your own photo capture component, you can utilize the component of the default Camera application. Instead of writing an email sending component and reinventing the wheel, you can use the systems email application to send emails from your app.
Next page