Saturday, August 13, 2011

The Magic Repository of Android Knowledge

I like getting email from readers of our "Pro Android" book series, especially when they're raving about our book ;-) I received an email just yesterday from a reader who asked if there was some sort of repository of Android knowledge, for things like the following:

Having a focusable item in a row of a ListView CAUSES the
OnItemClickListener to NOT be invoked, like an AUTO-LINK.

We'd all love to see such a repository, but I'm not aware of one place where you can get all the answers. I think that's mostly due to the pace at which Android is being developed. For example, I was very surprised to see that in release 2.3.3 (Feb 2011), they deprecated some NFC API methods that were brand new in release 2.3 (Dec 2010). The fragment compatibility library had some significant bugs in it when it was released, which tells me Google doesn't do a very good job of testing all their stuff. The documentation is sometimes rather terse, sometimes missing and sometimes wrong. Ideally there would be no need for a separate repository of knowledge; everything would work as documented, and the documentation would cover everything. Yes, it can be very challenging to be an Android developer if you want to do something interesting with it.

You can imagine what it's like to write a book the size of Pro Android (1139 pages in Pro Android 3). Our sample applications can sometimes take a while to finish because we often run into strange behavior which we feel compelled to figure out for our readers. So we scour all over for the answers. Satya's methods may be slightly different than mine, but here is where I go for information (not necessarily in this order):

1) Google search. Of course, right? I'd say it's an art form to get a search string just right, but there are so many places out there with little Android tutorials, or sample programs, or forums. Sometimes you get lucky and find the answer. Sometimes I find that other people have the same question but no one has the answer. For me, the best search is to take a significant portion of the error message reported in LogCat. The more keywords in your search, the more likely you'll hit a relevant page.

2) Forums. There are many, many forums for Android developers to share information. LinkedIn alone has several but I don't get much value out of them. I've never posted questions to any of them but I've posted a few answers. Stackoverflow.com is pretty good. It's also known as "Android Beginners". There are lots of already-answered questions, and a fair number of people who post answers. In any forum, it's important to include as much relevant information as possible, including links to screenshots, or stack traces. Another forum that's good is android-developers (http://groups.google.com/group/android-developers/topics). Good questions on this forum can even get answered by the Android team at Google (Dianne Hackborn or Nick Pelly or Romain Guy for example). There are several experts who also post answers, but again, it's important to ask a good question and to have a good topic line to catch their eye. Posts with a topic of "Newbie needs help now!!!!" won't likely see any response at all. Better to post "?? Focusable item in a ListView row CAUSES the OnItemClickListener to NOT be invoked??". If your question is specific to a device, most manufacturers also have developer forums and employees providing answers.

3) ApiDemos. This is in the set of sample apps that are downloadable just like the Android SDK. If you haven't checked these out yet, you should. It shows you how to use many of the Android APIs. In some cases it answers questions on how to do things. Other times it just raises more questions. But since you have the source code and you have an app that you can run, it's possible to read the source, debug it, etc. You can import these in the New Android Project wizard. Just choose "From samples" and select ApiDemos.

4) Android SDK source code. There are a few places to go to to see Android source code. At the moment, Android 3.x source code has not been released by Google, so the best you can get now is 2.3.5. This is a good starter page: http://source.android.com/  and this is where you can view source online: http://android.git.kernel.org/  The fragment compatibility library for the pre-3.x versions of Android comes with source code. To find it, look under your local Android SDK directory after installing that library using the Android SDK and AVD Manager.

5) Google I/O online videos. The next best thing to being there, the videos have great information from the engineers creating all the Android goodness.

6) Tinker. Experiment. Try different approaches. Reduce the problem to code as simple as possible. Use the debugger and set breakpoints, then inspect the variables. Repeat. Start with code that works, then modify it towards what you want it to do, testing with each step to see where it stops working.

Hope this helps! And if you find that magic repository, be sure to let us know ;-)