Android development can be challenging, but through the effective use of Android Developer Tools (ADT), you can make the process easier and improve the quality of your code. This concise guide demonstrates how to build apps with ADT for a device family that features several screen sizes, different hardware capabilities, and a varying number of resources. With examples in Windows, Linux, and Mac OS X, you’ll learn how to set up an Android development environment and use ADT with the Eclipse IDE. Also, contributor Donn Felker introduces Android Studio, a Google IDE that will eventually replace Eclipse. Learn how to use Eclipse and ADT together to develop Android code Create emulators of various sizes and configurations to test your code Master Eclipse tools, or explore the new Android Studio Use Logcat, Lint, and other ADT tools to test and debug your code Simulate real-world events, including location, sensors, and telephony Create dynamic and efficient UIs, using Graphical Layout tools Monitor and optimize you application performance using DDMS, HierarchyViewer, and the Android Monitor tool Use Wizards and shortcuts to generate code and image assets Compile and package Android code with Ant and Gradle Copyright 4 Table of Contents 5 Preface 11 Requirements for Android Developer Tools 11 Development Process for Android Developer Tools 12 Development on a Variety of OS Platforms 12 Conventions Used in This Book 12 Using Code Examples 13 Safari庐 Books Online 14 How to Contact Us 14 Acknowledgments 15 Chapter聽1.聽Getting Started 17 Minimum Requirements 17 Installing Java 18 Installing the Android Software Development Kit 19 Installing the ADT Bundle 19 Developing Without Eclipse 21 Command-Line Usage 22 Using a Different IDE 22 Configuring a Device for Development 23 Chapter聽2.聽Essential Tools 27 SDK Manager 27 SDK Components 27 Starting the SDK Manager 28 Viewing Installed and Available Components 29 ADT Preview Channel 31 Android Debug Bridge (ADB) 33 Starting ADB 33 Querying for Device Instances 34 Issuing Commands 35 The Shell Command 36 ADB Does a Lot More 38 Resetting the ADB Server 39 Additional Resources 40 Chapter聽3.聽Configuring Devices and Emulators 41 Using a Physical Device for Development 41 Capabilities and Limitations 41 Configuring a Physical Device for Development 42 Using an Emulator for Development 42 Supported Features 43 Android Virtual Devices 44 Creating AVDs 44 Emulator Options 48 Advanced Emulator Configuration 49 Using Hardware Acceleration 49 Disabling the Boot Animation 53 On Windows: Dedicating a Core to the Emulator 53 Chapter聽4.聽Using Devices and Emulators 55 Using the Emulator 55 Starting the Emulator 56 The Emulator Application 63 The Devices Tool 64 Keyboard Shortcuts 66 File Explorer 66 Developer Tools Application 68 Chapter聽5.聽Developing with Eclipse 71 Anatomy of the Eclipse Workspace 71 Uncovering Additional Tools and Views 73 Quick Button 75 Code Templates 75 Properties Editors 81 XML Formatter 83 The Android Key 83 Quick Outline for XML 84 Other Essential Eclipse Shortcuts 85 Refactor Menu 86 Chapter聽6.聽Developing with Android Studio 87 Installing Android Studio 87 Bundled SDK 88 Default Project Location 90 Anatomy of the Android Studio IDE 90 Panels 90 Toolbars 91 Useful Actions in Android Studio 92 Navigation 92 The New Structure of an Android Project 93 A Tour Around the New Structure 93 Running and Debugging an Android Project 94 Creating New Android Components 95 Layout Designer and Layout Preview 96 Layout Designer 96 Layout Preview 97 Generating an APK 98 Interacting with Maven and Gradle 99 Interacting with Maven 99 Interacting with Gradle 100 Version Control Integration 101 Migrating from Eclipse 102 Android Studio Tips and Tricks 102 Refactoring and Code Generation 103 Miscellaneous Shortcuts 103 Chapter聽7.聽Testing Your Code 105 Logcat 105 Viewing the Logcat File 105 Anatomy of a Log Message 105 Filtering Based on Logging Level 107 Using Tags to Filter Output 108 Getting the Most Out of Logcat 108 Viewing Alternative Log Buffers 109 Predefined Output Formats 109 Logcat Viewer in Eclipse 109 Logcat Example 110 Debugging 114 Setting Your App to Debuggable 115 Setting a Debug Point 115 The Eclipse Debug Perspective 116 Debugging Example 117 Lint 123 Command-Line Usage 124 Running in Eclipse 126 Chapter聽8.聽Simulating Events 131 Simulating Location and Routes 131 Simulating Telephony Operations 134 Changing Networking Parameters 137 Using a Device with Sensor Emulation 138 Advanced Sensor Testing 138 Supported Sensors 139 Simulating Sensor Events in Real Time 139 Recording Sensor Scenarios 140 Developer Options Menu 142 Chapter聽9.聽Build Tools 145 Compiling Your Code 145 Packaging an APK for Release 146 Signing Your App 146 Building from the Command Line Using Ant 149 Setting Up Your Project 151 Building in Debug Mode 152 Building an App to Release to the Play Store 152 Additional Ant Commands 154 Advanced Packaging Steps 155 ProGuard 155 Zipalign 160 Gradle-Based Build Tools 160 Installing Gradle 160 Creating Multiple Build Variations 161 Build File 163 Build Tasks 164 Generating a Gradle Build from Eclipse 164 Using the Maven Tools 165 Chapter聽10.聽Monitoring System Resources 167 Memory Usage in Android 167 Dalvik Debug Monitor Server (DDMS) 168 Launching the DDMS Perspective 168 Analyzer Tool 170 Threads 171 Heap 172 Traceview 173 Memory Analyzer Tool (MAT) 175 Generating an HPROF File 176 HPROF File 176 Installing MAT into Eclipse 176 Launching MAT from Within Eclipse 176 Using MAT to Analyze HRPOF Files 177 The MAT Overview Screen 178 Viewing a Report 178 Chapter聽11.聽Working with the User Interface 181 Android Layout Basic Concepts 181 Defining Layouts Using XML 181 Views and ViewGroups 183 Resources 186 Leveraging ADT to Build Great UIs 187 Editing XML Files Directly 188 Using Templates 188 Editing XML Directly 189 Working with Graphics 198 The Asset Tool 199 Using Nine-Patch Images 201 Asset Studio Website 203 Chapter聽12.聽Using the Graphical Editor 205 Generating Layouts Using the Graphical Layout Editor 205 Palette 207 Canvas 208 Layout Actions 210 Context-Sensitive Menu 210 Outline View 212 Properties Editor 212 Configuration Chooser 213 Chapter聽13.聽Optimizing the User Interface 217 Introduction to UI Performance 217 How Android Draws Views, and How It Affects UI Performance 217 Hierarchy Viewer 219 Starting the Hierarchy Viewer 219 Loading the View Hierarchy into the Tools 219 Navigating the Tree Hierarchy 219 Gathering View Information 221 Gathering View Rendering Details 223 Example: Debugging a UI Using the Hierarchy Viewer 224 Fixing Problems Using Lint 234 Application Exerciser Monkey 235 Grooming the Monkey 236 Monkeyrunner 237 Using Python to Create the Script 237 Thanks for Reading! 238 Index 239 About the Author 249 Android development can be challenging, but through the effective use of Android Developer Tools (ADT), you can make the process easier and improve the quality of your code. This concise guide demonstrates how to build apps with ADT for a device family that features several screen sizes, different hardware capabilities, and a varying number of resources.With examples in Windows, Linux, and Mac OS X, you’ll learn how to set up an Android development environment and use ADT with the Eclipse IDE. Also, contributor Donn Felker introduces Android Studio, a Google IDE that will eventually replace Eclipse.Learn how to use Eclipse and ADT together to develop Android codeCreate emulators of various sizes and configurations to test your codeMaster Eclipse tools, or explore the new Android StudioUse Logcat, Lint, and other ADT tools to test and debug your codeSimulate real-world events, including location, sensors, and telephonyCreate dynamic and efficient UIs, using Graphical Layout toolsMonitor and optimize you application performance using DDMS, HierarchyViewer, and the Android Monitor toolUse Wizards and shortcuts to generate code and image assetsCompile and package Android code with Ant and Gradle