41,99 €
Unleash the power of Android Studio 3 to develop mobile applications faster and efficiently.
This book targets developers, with experience of developing for Android, who are new to Android Studio or wish to migrate from another IDE such as Eclipse. This book will show you how to get the utmost from this powerful tool.
Android Studio is an Integrated Development Environment (IDE) designed for developing Android apps. As with most development processes, Android keeps resources and logic nicely separated, and so this book covers the management of imagery and other resources, and the development and testing tools provided by the IDE.
After introducing the software, the book moves straight into UI development using the sophisticated, WYSIWYG layout editor and XML code to design and test complex interfaces for a wide variety of screen configurations.
With activity design covered, the book continues to guide the reader through application logic development, exploring the latest APIs provided by the SDK. Each topic will be demonstrated by working code samples that can be run on a device or emulator.
One of Android Studio's greatest features is the large number of third-party plugins available for it, and throughout the book we will be exploring the most useful of these, along with samples and libraries that can be found on GitHub.
The final module of the book deals with the final stages of development: building and distribution. The book concludes by taking the reader through the registration and publication processes required by Google. By the time you have finished the book, you will be able to build faster, smoother, and error-free Android applications, in less time and with fewer complications than you ever thought possible.
This is a step-by-step guide with examples demonstrating how Android Studio can be used as a complete solution for developing, testing, and deploying apps from start to finish.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 205
Veröffentlichungsjahr: 2017
BIRMINGHAM - MUMBAI
Copyright © 2017 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: August 2017
Production reference: 1230817
ISBN 978-1-78646-744-7
www.packtpub.com
Author
Kyle Mew
Copy Editor
Dhanya Baburaj
Reviewer
Jessica Thornsby
Project Coordinator
Ritika Manoj
Commissioning Editor
Wilson D'souza
Proofreader
Safis Editing
Acquisition Editor
Shweta Pant
Indexer
Mariammal Chettiyar
ContentDevelopmentEditor
Aditi Gour
Graphics
Jason Monteiro
Technical Editor
Akansha Bathija
Production Coordinator
Shantanu Zagade
Kyle Mew has been programming since the early '80s and has written for several technology websites. Also, he has written three radio plays and four other books on Android development.
Jessica Thornsby studied poetry, prose, and scriptwriting at Bolton University before discovering the world of open source and technical writing, and has never looked back since. Today, she is a freelance technical writer and full-time Android enthusiast; She is also the author of Android UI Design and the co-author of iWork: The Missing Manual.
For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www.packtpub.com/mapt
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at www.amazon.com/dp/1786467445.
If you'd like to join our team of regular reviewers, you can e-mail us at [email protected]. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the color images of this book
Errata
Piracy
Questions
Workspace Structure
How Android Studio differs
Workspace structure
Editor windows
Tool windows
The Android SDK
Virtual devices
Instant Run
Importing projects into Android Studio
Summary
UI Design
The Layout Editor
Linear and relative layout classes
The constraint layout
Creating a ConstraintLayout
Applying constraints
Graphic Properties tool
Baseline alignment
Controlling position with bias
The constraints toolbar
Multiple screen previewing
Hardware profiles
Virtual storage
Summary
UI Development
Material design
Android styles
Material themes
The Theme Editor
XML fonts
The design library
The coordinator layout
Snackbars and Floating Action Buttons
Collapsing app bars
Raw text resources
The percent library
Summary
Device Development
Screen orientation
Virtual devices
Layout and image qualification
Scale and platform
Alternative emulators
Android Wear
Connecting to a wearable AVD
Wearable layouts
The WatchViewStub class
Shape - aware layouts
Accessing sensors
Sensor emulation
Device monitoring
Project templates
Monitoring and profiling
Summary
Assets and Resources
Asset Studio
Image Asset Studio
Layered icons
Launcher icon tools
Vector Asset Studio
Vector drawables
Animated icons
Other animations
General drawables
The palette library
Summary
Templates and Plugins
Project templates
The Navigation Drawer template
The Structure Explorer
Class inspection plugins
The Master/Detail Flow template
Custom templates
Third-party templates
Project samples
Third-party plugins
ADB Wi-Fi
Codota
Summary
Language Support
Kotlin support
The advantages of Kotlin
Extending Kotlin
C/C++ support
The NDK
Android Things
Development kits
Creating a Things project
Summary
Testing and Profiling
Logcat filters
JUnit testing
Local unit tests
Testing a UI
Testing views
Testing lists and data
Recording tests
Remote testing
Stress testing
Performance monitoring
CPU profiling
Memory profiler
Network profiler
Summary
Packaging and Distribution
Gradle build configurations
Command-line options
Product flavors
An APK analysis
Publishing applications
Generating a signed APK
Managing keystores
Google Play app signing
Automatic signing
Summary
Welcome to Mastering Android Studio 3, a comprehensive guide to the latest and most sophisticated Android development environment. Whether you are new to IDEs or want to migrate from another, such as Eclipse, this book uses practical examples to demonstrate how Android Studio facilitates each stage of development.
Beginning with an introduction to the workspace itself, the book then explores the wide variety of UI design tools the IDE provides, including the powerful visual layout editor, automatic constraint layout tools and animated material icons.
Once the IDE's design tools have been mastered, the book continues by exploring code development with Android Studio and its many helpful and innovative programming tools, such as code completion, template customization, and most importantly, the fantastic testing and profiling tools that are provided with Android Studio 3.
Android Studio is not just a great tool for vanilla coding; it provides all manner of additions and extensions in the form of plugins and native language support for languages such as C++ and Kotlin. It is this extensibility of the native SDK that makes Mastering Android Studio 3 such an essential skill-set for any mobile developer, and the most useful and popular of these are covered in detail to give the reader mastery of what is without doubt one of today's most exciting development tools.
Chapter 1, Workspace Structure, an introduction the overall workspace. It covers the major features and will be of great use to those who are brand new to the IDE.
Chapter 2, UI Design, introduces the subject of UI design and development, looking at the automating and time saving features of the layout editor.
Chapter 3, UI Development, remaining with UI development tools, explores more sophisticated layouts and how these can be easily implemented using code libraries that come packaged in the support repository.
Chapter 4, Device Development, extends the previous work and looks at developing for physical devices and form factors, covering topics such as screen rotation and shape-aware layouts for wearable devices.
Chapter 5, Assets and Resources, looks at resource management, in particular Android's use of material icons and vector assets. It demonstrates how Android Studio provides great time-saving features for this aspect of development.
Chapter 6, Templates and Plugins, is the first of two chapters on extending Android Studio beyond vanilla usage. Here, we look at ready-made and freely available code samples, provided not only within the IDE but also via third-party plugins.
Chapter 7, Language Support, continues the theme of the previous chapter. Here, we look at how to include C++ and Kotlin code seamlessly.
Chapter 8, Testing and profiling, explores the powerful testing and profiling tools provided by the IDE and how to use them to test and fine-tune our work.
Chapter 9, Packaging and Distribution, covers the final aspects of the development cycle. This involves taking a close look at Gradle and covers monetization technologies.
Both Android Studio SDK are open source and can be downloaded from developer.android.com.
Various third-party plugins are mentioned throughout the book-along with their download locations where relevant.
This book is for Android developers of any experience level who are looking to migrate to or simply master Android Studio 3.
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning. Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "In the previous examples, we used app:srcCompat as opposed to android:src. "
A block of code is set as follows:
public class ExampleUnitTest { @Test public void addition_isCorrect() throws Exception { assertEquals(4, 2 + 2); } }
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
buildTypes { release { . . . } }
productFlavors { flavorDimensions "partial", "full"
Any command-line input or output is written as follows:
gradlew clean
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Besides MakeBuild and Analyze, the Build menu has other useful entries, for example, the Clean Project item, which removes build artifacts from the build directory "
Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/MasteringAndroidStudio3_ColorImages.pdf.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title. To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy. Please contact us at [email protected] with a link to the suspected pirated material. We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.
Android Studio is a powerful and sophisticated development environment, designed with the specific purpose of developing, testing, and packaging Android applications. It can be downloaded, along with the Android SDK, as a single package, but as we shall see throughout the course of this book, it is, in reality, a collection of tools and components, many of which are installed and updated independently of each other.
Android Studio is not the only way to develop Android apps; there are other IDEs, such as Eclipse and NetBeans, and it is even possible to develop a complete app using nothing more than Notepad and the command line, although this last method would be very slow and cumbersome.
Whether you are migrating from another IDE or just want to get the most from Android Studio, this book will take you through all of its most useful features in the order that these would be encountered during the course of developing an application, starting with UI development and progressing through coding and testing to building and distribution. Android Studio provides some useful and smart tools to assist us at every step of this journey.
Built for a purpose, Android Studio has attracted a growing number of third-party plugins that provide a large array of valuable functions, not available directly via the IDE. These include plugins to speed up build times, debug a project over Wi-Fi, and many more. The most useful and popular of these will be covered in the relevant sections. Throughout the book, we will be finding ways to speed up tedious and difficult tasks using these plugins and Android Studio's built in components.
In this chapter, you will be engaged with the following topics:
Exploring the differences between Studio and other IDEs
Taking a brief guided tour
Learning how the workspace is structured
Exploring editor windows
Creating a Material theme
Understanding Tools windows
Exploring device filesystems
Using Instant Run to speed up the build process
Exploring the SDK manager
Introducing the virtual device manager
Importing a project from another IDE
If you are already familiar with Android Studio, then you may wish to skip some sections of this introductory chapter, as it is written more with those migrating from other IDEs in mind. However, there are a number of handy tips that you may not have come across before.
Despite being arguably a superior tool, there are some very good reasons for having stuck with another IDE, such as Eclipse. Many developers develop for multiple platforms, which makes Eclipse a good choice of tool. Every developer has deadlines to meet, and getting to grips with unfamiliar software can slow them down considerably at first. This book will speed up this transition so that migrating developers can begin to take advantage of the added features provided by Android Studio with as little interruption as possible.
There are many ways that Android Studio differs from other IDEs and development tools. Some of these differences are quite subtle, such as the way support libraries are installed, and others, for instance the build process and the UI design, are profoundly different.
Before taking a closer look at the IDE itself, it is a good idea to first understand what some of these important differences are. The major ones are listed here:
UI development
: The most significant difference between Studio and other IDEs is its layout editor, which is far superior to any of its rivals, offering text, design, and blueprint views, and most importantly, constraint layout tools for every activity or fragment, an easy-to-use theme and style editors, and a drag-and-drop design function. The layout editor also provides many tools unavailable elsewhere, such as a comprehensive preview function for viewing layouts on a multitude of devices and simple-to-use theme and translation editors.
Project structure
: Although the underlying directory structure remains the same, the way Android Studio organizes each project differs considerably from its predecessors. Rather than using workspaces as in Eclipse, Studio employs modules that can more easily be worked on together without having to switch workspaces.
This difference in structure may seem unusual at first, but any Eclipse user will soon see how much time it can save once it becomes familiar.
Code completion and refactoring:
The way that Android Studio intelligently completes code as you type makes it a delight to use. It regularly anticipates what you are about to type, and often a whole line of code can be entered with no more than two or three keystrokes. Refactoring too, is easier and more far-reaching than alternative IDEs, such as Eclipse and NetBeans. Almost anything can be renamed, from local variables to entire packages.
Emulation:
Studio comes equipped with a flexible virtual device editor, allowing developers to create device emulators to model any number of real-world devices. These emulators are highly customizable, both in terms of form factor and hardware configurations, and virtual devices can be downloaded from many manufacturers. Users of other IDEs will be familiar with Android AVDs already, although they will certainly appreciate the preview features found in the Design tab.
Build tools:
Android Studio employs the Gradle build system, which performs the same functions as the Apache Ant system that many Java developers will be familiar with. It does, however, offer a lot more flexibility and allows for customized builds, enabling developers to create APKs that can be uploaded to TestFlight, or to produce demo versions of an app, with ease. It is also the Gradle system that allows for the modular nature discussed previously. Rather than each library or a third-party SDK being compiled as a JAR file, Studio builds each of these using Gradle.
These are the most far-reaching differences between Android Studio and other IDEs, but there are more as well as many features that are unique. Studio provides the powerful JUnit test facility and allows for cloud platform support and even Wi-Fi debugging. It is also considerably faster than Eclipse, which, to be fair, has to cater for a wider range of development needs, as opposed to just one, and it can be run on less powerful machines.
Android Studio also provides an amazing time-saving device in the form of Instant Run. This feature cleverly only builds the part of a project that has been edited, meaning that developers can test small changes to code without having to wait for a complete build to be performed for each test. This feature can bring these waiting times down from minutes to almost zero.
Whether you are new to Android Studio or want to gain more from it, the first step is to take a broad look at its most prominent structures.
The overall structure of Android Studio is not dissimilar to other IDEs. There are windows for editing text and screen components, others for navigating project structures, and others still for monitoring and debugging. The IDE is highly flexible and can be configured to suit many specific needs and preferences. A typical layout might look like this:
Although these windows can be arranged in any way we please, generally speaking, in the previous screenshot, the four panes might have the following functions:
Navigating a project, module, or library
Editing text and designing layouts
Defining component properties or screen previews
Monitoring and debugging
There are many different perspectives we can take on our projects and many ways to organize them. The best way to see how is to take a look at each in turn.
Naturally, the most important of all the windows in an IDE are those where we create and modify the code that underlies all our apps. Not only do we use editors for our XML and Java, there are, among others, editors for simplifying other resources, such as translations and themes. However graphical the editor may be, all Android resources end up as XML files in the res directory.
It is quite possible to create most Android resources without ever having to write any code at all. Themes can be created with the corresponding editor with nothing more than a few clicks of a mouse. Nevertheless, if we are to consider ourselves as experts, it is important that we have a good understanding of the underlying code and how and where Studio stores these resources. The following example demonstrates how to create a new Android theme using the theme editor:
Start or open an Android Studio project.
Open the theme editor from
Tools | Android | Theme Editor
.
From the
Theme
drop-down in the top right corner of the editor, select
Create New Theme
and enter a name in the
New Theme
dialog.
Leave the
Theme parent
field as-is.
Click on the
colorPrimary
thumbnail.
Choose a color you like from the resultant swatch with a weight of
500
.
In the same manner, select the same color with a weight of
700
for the secondary color.
Select a color with a weight of
100
that contrasts nicely with your primary colors for the accent.
Open a preview or the design editor to view these changes.
In the preceding example, we created a new theme that will be automatically applied throughout the application. We could have simply edited the default AppTheme, but this approach will simplify matters if we later decide to employ more than one theme. The IDE applies these changes straightaway by adding something like the following line to the res/values/styles.xml file:
<style name="MyTheme" parent="AppTheme" />
The actual color changes can be found in the res/values/colors.xml file.
The theme editor demonstrates rather nicely how Studio editors can create and modify code after little more than a few mouse clicks from us.
It is also possible to change the theme of the IDE itself by selecting Settings | Editor | Colors and Fonts from the File menu, as displayed in the following image:
Android Studio comes equipped with just one alternative color scheme, Darcula. This theme presents light text on a dark background and, as such, is far easier on the eye than the default settings, especially for those long, late night development sessions. There are other schemes available online and it can be a lot of fun to design one's own. However, for the purposes of producing printed material, we will stick with the default IDE theme here.
