Practical Android Projects Lucas Jordan Pieter Greyling Apress s
w«^* ; i - -i.. ; Contents at a Glance Contents --v About the Authors x About the Technical Reviewer xi PAcknowiedgments xii Preface xiii Chapter 1: Android Fundamentals 1 Chapter 2: Development Tools in Practice 49 3: Roll Your Own Android Scripting Environment 105 4: Embedding Lua in Android Applications 155 Chapter 5: Introducing SL4A: The Scripting Layer for Android 193 Chapter 6: Creating a GUI with HTML/JavaScript and AIR 221 Chapter 7: Using REST with Facebook and Twitter 251 Chapter 8: Using the Google App Engine with Android 275 Chapter 9: Game Development: Graphics 311 Chapter 10: Game Development: Animation 341 Chapter 11: App Inventor 361 387 iv
Contents Contents at a Glance iv OAbout the Authors x About the Technical Reviewer xi Acknowledgments xii I Preface xiii I Chapter 1: Android Fundamentals 1 What Is Android? 2 Installing the Android SDK 2 Java Development Kit (JDK) 3 Android SDK and Target Platforms 3 Android SDK Test Drive 9 Android Architecture and Background 20 The Android Platform Stack 21 Android Component Architecture 23 The Android Runtime: Dalvik Virtual Machine (DVM) 23 Using an Integrated Development Environment (IDE) 27 Working with Eclipse 28 On the Web: Eclipse for Android Development 28 Quickstart: The Eclipse Android Development Tools (ADT) Plugin 29 Working with NetBeans 33 On the Web: NetBeans for Android Development 33 ' Quickstart: The NetBeans Android (NBAndroid) Plugin 34 Working with IntelliJ IDEA Community Edition 42 On the Web: JetBrains IntelliJ IDEA for Android Development 42 Quickstart: The IntelliJ IDEA Android Plugin 43 Summary 47
Chapter 2: Development Tools in Practice 49 Coding with the SDK and a Programmer's Editor 50 Development Environment Dependencies 50 Ensure that Development Kit Locations Are on the Path 50 Preparing an Android Virtual Device (AVD) 51 Frequently Used Android Development Kit Tools 52 Working with the Android Tools and a Code Editor 53 Selecting a Code Editor 53 Configuring the Editor for Android Work 56 The Example Application Project 58 Generating the Foundation Android Project 59 The Android Project Directory Structure 60 Preparing to Run the Example Application 61 Starting a Debugging Session 61 Replacing the Default Generated Code 63 Building and Installing the Project Example Code 64 Creating a Log Filter for the Application in the DDMS 64 Running the Example Application 66 What Does the Demo Application Do? 67 A Walk through the Core Application Files 74 Android Coding How to 81 Using the Android Log API 81 Centralizing Application GUI Initialization Code 85 Exiting an Application Activity 86 Enabling and Disabling Buttons (and other Views) 87 Creating Controls Dynamically (at Runtime in Code) 88 Making an Android Toast 90 Showing an Android Alert Dialog 90 Creating and Showing an Android System Notification 91 Using a Private Application File 92 Making Menus 95 Migrating the Example Application to Eclipse/ADT 97 Make a Copy of the Project 97 Open Eclipse with the ADT Plugin Installed 97 Create a New Android Project from the Copy of the Project 97 Create and Test a New Run Configuration for the Project 98 Deploying to a Real Device 99 Creating a Signed APK Package of the Example Application 101 Summary 104 Chapter 3: Roll Your Own Android Scripting Environment 105 Designing a Scripting Environment 106 The Components of a Scripting System 106 The Component Roles in a Scripting System 107 Designing for Resource-Constrained Systems 109 j Multi-Threading for Background Code 110 Programming with BASIC 110 A BASIC Backgrounder 111 vi
Cocoa A BASIC Interpreter for Java 111 Outlining the Code Projects for This Chapter 112 The Cocoa-BASIC AWT Project 112 Understanding the Cocoa-BASIC AWT Application Design 112 Running the Cocoa-BASIC AWT Desktop Application 114 Reviewing the Cocoa-BASIC AWT Source Code 118 The CocoaDroid Project 120 A Preflight Checklist 120 Understanding the CocoaDroid Application Design 121 Running the CocoaDroid Android Application 122 Summary 153 IB Chapter 4: Embedding Lua in Android Applications 155 Introducing Lua and Kahlua2 156 Lua Resources 156 Kahlua2 Resources 157 Using Kahlua2 in Your Android Java Applications 157 Development Environment Configuration 158 The Kahlua2 Project 158 Setting up the Kahlua2 Runtime Files Project 159 Building from the Console 160 Building from an IDE 160 The Kahlua2 Runtime Libraries 161 The Kahlua2 Android Interpreter Project 162 Setting up the Project 162 Building from the Console 163 Building from an IDE 163 Running the Kahlua2 Android Interpreter 164 Understanding the Basics of Embedding Kahlua2 165 The KahluaDroid Project 168 Running the KahluaDroid Application 169 Running Lua Code On or Off the Main GUI Thread 176 Exposing Android Application Methods to Kahlua2 180 Calling Application Methods as Lua Functions 183 Implementing an Application Startup Script 185 Summary 192 Chapter 5: Introducing SL4A: The Scripting Layer for Android 193 What Is Scripting Layer for Android? 194 About SL4A 194 The SL4A License 194 Using SL4A 194 SL4A Resources 195 The SL4A Code Repository 195 Running SL4A in the Android Emulator 196 Development Environment Configuration 196 Downloading the SL4AAPKs 197 Installing the SL4AAPK on the Android Emulator 197 Running SL4A on the Android Emulator 198 vii
viii Installing SL4A Interpreters 202 Understanding Scripting Layer for Android 209 Communicating Using JavaScript Object Notation (JSON) 209 Summarizing the SL4A Architecture 210 Reviewing Local Proxy Implementations 211 Getting the SL4A Source Code 215 Cloning the SL4A Source Code 216 SL4A Hello World Examples 219 Summary 220 Chapter 6: Creating a GUI with HTML/JavaScript and AIR 221 Setting Up an Android Project to Display a Web Application 222 The Android Project 223 Calling Android Methods from JavaScript 225 JavaScript Application 226 Graphics and Animation 231 User Interaction 235 JavaScript Summary 238 Using Flash and Flex Apps on Android with AIR 238 Writing a Flex Application for Android 239 Building and Deploying 240 Creating the Flex Ul with MXML 244 Writing ActionScript 246 Summary 250 Chapter 7: Using REST with Facebook and Twitter 251 Understanding REST 252 REST and JSON 253 REST from an Android Application 255 Asynchronous Tasks 258 Twitter 259 Examples in Code 261 Tweeting on Behalf of the User 265 Confirming the User Wants to Tweet 266 Understanding the Facebook API 268 Facebook and Authentication 268 Facebook's Social Graph API 272 Summary 274 Chapter 8: Using the Google App Engine with Android 275 Introducing Google App Engine 276 Getting Started with GAE 276 Using Eclipse with GAE 278 GAE Project Structure 280 Charges for the Google App Engine Service 283 Google App Engine Services 284 Examining a Sample GAE Application 287 Adding the HighScore Service 290 Querying the HighScore Service 293 Consuming GAE Services with Android 297
Exploring the Top Ten Activity 300 Viewing the Users of a Game 303 Viewing a User's Location (MapView) 305 Summary 310 Chapter 9: Game Development: Graphics 311 Introducing the Android View Package 312 Understanding XML Layout 312 Layout in Code.T..317 Custom Component 320 Understanding the Drawable Class 328 Drawable Class 328 Drawable Subclasses 330 NinePatchDrawable 334 Direct Rendering 336 Summary 340 Chapter 10: Game Development: Animation... 341 Android Animations 341 Creating Views and Animations 343 Frame By Frame Animations 353 Mixing Views and SurfaceViews 359 Summary 360 Chapter 11: App Inventor 361 Setting Up App Inventor 361 Working with Blocks 365 Understanding the Types of Blocks 367 Creating Application Logic with the Block Editor 376 Limitations of App Inventor 383 Limited Set of Components 384 Limitations in Block Editor 385 Summary 386 Index 387 ix