by John Lombardo
digg this story |
Android Emulator
(Click to enlarge)
So, what Is Android?
Android is a complete software stack for mobile devices such as cell phones, PDAs and high end MP3 players. The software stack is split into four layers:
- The application layer
- The application framework
- The libraries and runtime
- The kernel
That's right, Android applications are almost exclusively written in Java. The Android core library is a big .jar file that is supported by the Dalvik Virtual Machine -- a fast and efficient JVM work-alike that enables java-coded applications to work on the Android cell phone. This is similar to, but not the same as using Sun's JVM directly.
Building your development environment
Google provides three versions of the SDK; one for Windows, one for Mac OSX (intel) and one for Linux (x86). They also provide two development environments -- one is Eclipse based, and the other is a "roll your own." The Eclipse based environment is quite feature-rich and should suffice for most developers. There is no reason that you can't use both Eclipse and "roll your own."
If you get stuck in eclipse (like I did), you may find yourself dropping to the command-line interfaces to see what's really going on. However for this article, I'll assume that you're using the Eclipse IDE for your Android software development needs. Once you've downloaded the Android SDK, Eclipse and the Eclipse Plugin, you should work through the first few sections of Google's install document (System and Software Requirements, Installing the SDK, Installing the Eclipse Plugin). I'd leave the rest of the document for later as it does get quite detailed quickly.
Read the Friendly Manual
Google has done a good job of writing a lot of documentation for Android. However, there isn't a great way of knowing what's important to read now vs. what can wait. Here are some links to documents that are important to understand what Android is and how to develop applications using it. If you read them in the order listed, then you'll gain understanding more quickly as you read increasingly detailed documents. Note that a lot of the documentation is available both online and in the SDK_ROOT/docs/index.html directory on your machine. If you have a fast enough connection, I would suggest using the on-line versions since they will be more up to date.
Here's the order in which I suggest you read the documentation:
- What is Android? Explains what Android is and gives a high-level overview of its features and architecture. Don't dive into the links just yet -- just get a feel for this overall document.
- Read the Anatomy of an Android Application page. This details the four building blocks of an Android app: Activity, Intent Receiver, Service and Content Provider. Again, don't follow the links just yet -- just get an overview of the architecture. You may want to reread sections on Activities and Intent Receivers -- gaining an understanding of these concepts is critical to understanding how to build an Android application. If you don't get it yet, you'll see it again when you go through the Notepad Application Tutorial.
- Next read the Development Tools document. Again -- just get a flavor, don't dive into the detail yet.
- Read the Lifecycle of an Android Application page.
- Now, finally, it's time to get your hands dirty. Work through the Hello Android page. Make sure you actually do it using Eclipse.
Note: I had a problem here. The first time I ran the application, it worked fine. However on subsequent runs my application would not appear on the emulator. I killed and restarted the emulator, killed and restarted eclipse several times -- no joy. Finally, somewhat frustrated, I was going to re-install everything. However before I did, I found an invisible instance of the Android Debug Bridge (adb) running. I killed it and everything worked again. I thought I had to close the emulator after each run of an application, but this turns out not to be the case. When I closed the emulator after my first run, it left the instance of adb running, which interfered with the subsequent instances.
- Now go back and read the rest of the Installing the SDK document -- the bottom half of it details some great debugging tips and features.
- Next, go through the Notepad Application Tutorial. This is where the rubber really meets the road. If you spend the time to go through this series of exercises and really understand the code, you will be well on your way to becoming an Android expert.
- Read the Developing Android Applications pages. This will take some time -- these articles go into a lot of detail about several topics including how to implement the UI, data storage/retrieval and the security model.
- Finally, go back through this list and follow the links in the previous documents as topics interest you.
Dissecting the SDK
Whenever I download an SDK, I like to take a look at the files I've installed. Often, there is a wealth of information hidden in the SDK itself that is not readily visible from the documentation. So here's what you'll find in the Android SDK on a Windows machine:
- android.jar - The Android application framework. Unzipping this jar reveals the entire class structure and all of the supporting classes of the framework. Currently there is no source.
- docs - 100 megabytes worth of documentation, samples, etc.
- samples - Six different sample applications - ApiDemos, HelloActivity, LunarLander, NotePad, SkeletonApp and Snake
- tools - the various SDK binaries such as aapt, acp, and emulator live here.
- lib - various templates and supporting jar files live in this directory
- activityCreator - the activityCreator python application lives here.
- images - The Linux file system images are found in this directory: ramdis.img, system.img and userdata.img. They are YAFFS2 file system images, so I couldn't open them without additional kernel support on my Fedora system.
- skins - supporting emulator graphics for HVGA and QVGA screens in both landscape and portrait format.
- activityCreator - the activityCreator python application lives here.
- lib - various templates and supporting jar files live in this directory
Now that you have read the documentation and set up and debugged a simple project, it's time to look at some real code. Since Google has provided us with several sample applications, the best place to begin is by examining them.
- If you have not already done so, execute the first few sections of Google's install document. Stop after you've installed the Eclipse plugin successfully.
- Now work through the Hello Android page if you haven't already. This will get you started working with Android applications and the debugger. Note that it's probably a good idea to create a new workspace for your Android projects if you already use Eclipse.
- Next we're going to set up Eclipse projects for each of the sample applications. You can never have too much sample code. I'll walk through setting up the Lunar Lander example and leave it as an exercise to the reader to set up the rest.
- Bring up the same Eclipse workspace that you used for the Hello Android, and close the project (Right click on the project in package explorer->Close Project).
- File->New->Android Project
- Project Name: LunarLander
- Click the "Create Project from existing source" radio button
- Browse to the samples/LunarLander directory in the SDK. If you find the right directory, the Properties fields will auto-fill with the correct information from the Package.
- Click Finish
- Bring up Eclipse's Console window (Window->Show View->Console) if it's not already visible in a tab at the bottom of the screen. It will show you the build process that Eclipse went through to create the application.
- Create a Run Configuration: Run->Open Run Dialog
- Highlight "Android Application" in the treeview to the left.
- Click the "New button".
- Name: Lunar Lander
- Click the Browse button next to Project
- Double-click the LunarLander project and hit OK
- Click the down arrow for the Activity and choose the one and only Activity: com.google.android.lunarlander.LunarLander
- Click Apply
- Click Run
- Switch to the Emulator and play a few rounds of Lunar Lander. Kinda fun.
- Bring up the same Eclipse workspace that you used for the Hello Android, and close the project (Right click on the project in package explorer->Close Project).
A File System Explorer Application
Finally, we'll use our new understanding of the Android to develop a simple file system explorer. The version in this article is pretty simple, but it can serve as a jumping-off point for a more serious application down the road.
Design
Before we start writing code, let's think about what a reasonable MP3 player should do. It should
- Phase I features
- Show a list of files and directories
- Allow the user to navigate through the directory structure by clicking on directories
- Warn the user that he has clicked on a file
- Show a list of files and directories
- Phase II features
- Allow the user to display a dump of a file when it is clicked
- Use a tree view instead of a simple list
- Show a dialog box with the filesystem information (size, permissions, etc) when the user clicks on an icon next to each file
- Give this application permissions to read any file on the file system
- Allow the user to display a dump of a file when it is clicked
- Phase III features
- Do all of phase II with pretty graphics, such as thumbnails, nstead of boring dropdowns and list boxes
- Execute applications that we understand, such as mp3 files
- Do all of phase II with pretty graphics, such as thumbnails, nstead of boring dropdowns and list boxes
This article will only cover Phase I of the project -- but when we're done, we'll have a functional file system explorer in just a few dozen lines of code.
New Android Project
(Click to enlarge)
It works!
If you clicked above to follow the hands-on example, you found that in about twenty lines of Java, and a small amount of XML, you've created a useful little application that will allow you to explore the Android's file system. For example, I found the ringtones in /system/media/audio/ringtones, as shown below.
Oooh, ringtones
(Click to enlarge)
As I mentioned in the design section, a lot can be done with this application, and we've hardly touched the surface of what you can do with the Android application environment. There's thee billion cell phones out there. I suspect Google will get their fair share of them, so start cranking out code!
Conclusion
Android is a well-engineered development environment. Writing an Eclipse plug-in was a smart move by Google -- one that should be emulated by other SDK developers. Eclipse gives a developer and environment where he can really think about the business problem without worrying about the boring details. Adding the functionality of the plugin helps developers just sit down and start coding -- without having to worry about all the ins and outs of configuration files and the like.
Dislikes
Android is brand new to the general developer's world. As I write this, it's Wednesday, and the SDK came out on Monday of this week. Since it's brand new, there are some little problems that will have to be solved in the coming releases.
- Many more examples for the APIs.
- A more thorough explanation of what does and does not work under the emulator. My first example application was a simple MP3 player.
- Release the source code. This will make it a lot easier to debug Android applications, as well as write them in the style that the Google developers wrote them.
Theres a lot to like about Android:
- It's by Google -- so it has a company with some clout behind it.
- Application Developers write their code in Java. Since the learning curve for Java is much less than that of C/C++/ObjectiveC, there will be many many developers who are eager to start writing applications for Android.
- The SDK and API are well designed. There is some complexity there, and as I mentioned, the Documentation needs improvement (Google: call me :) -- but a well designed system is easier to understand and learn, even without lots of great examples.
沒有留言:
張貼留言