Thursday, April 22, 2010

Android: DDMS and filtering Logcat output

One of the most useful tools in any Android developers arsenal is logcat. Type "adb shell logcat" from the command line and you will be presented with a stream of messages. This isn't particularly useful if you want to filter out all the noise and concentrate on your application logs. However, it is useful for viewing what other applications are doing. It can often be quite interesting to run applications and watch logcat. When Google first released the ADC tool allowing Android users to vote on ADC II competition entries, there was plenty of interesting output to say the least.

Google have provided an ADT plugin if you are developing for Eclipse (you may have installed this during the SDK installation). Once you have the ADT plugin installed, click in the top right corner on the plus icon near where it says "Java". Select "other" and you will be presented with a list of options.

Select "DDMS" from the list and you will have a new view available to you. You can move the panes around to suit your personal preference. Below is a snapshot of my setup.

You can also launch this tool directly from the command line. Type "ddms" from the command line and you will see the DDMS tool start (providing you have setup Android correctly on your host machine).

If you would like a thorough understanding of how to use the tool I suggest you read the DDMS documentation.

Now we have DDMS running lets go back to our original purpose, filtering out the logcat noise. In the logcat pane you will see a '+' icon. Click on this to bring up the log filter.

Give the filter a name and fill out the attributes you want to select from the logcat output. In our example we are going to grab all the "dalvikvm" messages. You will now have a new tab in the logcat view to select. This tab will only display messages that meet your criteria.

After playing around with the phone for a couple of minutes our dalvikvm tab now shows output. If we cycle back to the main logcat tab we can see all the messages except anything that matches our selection criteria.

While developing, I will have logcat running in full screen on one of my monitors. This allows me to keep an eye on what the system is doing. Make sure before you release to clean up all your logcat output. An easy solution to this is create a final boolean such as DEBUG_MODE that you can set to true/false before compiling. We have all been guilty of releasing products that output something they shouldn't. This simple feature goes a long way to cleaning that up. Not to mention is makes live much easier by only have to change a single value before releasing your application into the wild.

final static boolean DEBUG_MODE = false;

if (DEBUG_MODE) Log.i(LOG_TAG,"Load level: " + mLevel);

Having a logcat filter setup for your application will not only stop your messages getting lost amongst the system output. It will also allow you to quickly tell if you are producing any logcat output before releasing to market.

Monday, April 19, 2010

Google Analytics for Android

Did you know Google Analytics supports mobile?

Didn't think so, most developers aren't aware this powerful tool even exists for mobile. Here are the steps to implement the most basic feature of Google Analytics for Mobile.

1. Download the Google Analytics library for Android. Direct link to current version 0.7.

2. Sign up for Google Analytics if you don't already have an account.

3. Create a new UA, also known as a Web property ID. Once you are signed into Google Analytics you will see a drop down list in the top right corner. From the list, select "Create new account" and follow the steps provided. Google documentation suggests "using a fake but descriptive website URL (e.g.". Whatever you choose is entirely up to you.

4. Write down your UA. This will be generated after you complete step 3. If you forget to write it down you can view the UA by selecting the product from your analytics home screen.

5. Add the Google Analytics library to your Android project. In Eclipse right click on your project (control + click for OSX) and select Properties. Choose Java Build Path and then select the Libraries tab. Then you will need to select the Add External JARs button and navigate to where you saved the library on your computer.
Note: The exact steps may vary depending on OS and what version of Eclipse you are running.

6. Add the following permissions to your AndroidManifest.xml file.
android.permission.INTERNET & android.permission.ACCESS_NETWORK_STATE

7. Here is the very basic logic of implementing analytics.

Declare our Analytics object
public GoogleAnalyticsTracker mAnalyticsTracker;

Obtain tracker instance and start in manual mode, usually done in onCreate(Bundle savedInstanceState)
mAnalyticsTracker = GoogleAnalyticsTracker.getInstance();
mAnalyticsTracker.start("insert_UA", this);

You can also start the tracker with a dispatch interval by specifying a seconds parameter. The following code will dispatch request every 60s.
mAnalyticsTracker.start("insert_UA", 60, this);
Note: don't forget to substitute insert_UA with your actual UA account.

The Google documentation suggests using trackPageView as the best way to track which areas of your application are being viewed by users.

Say we want to track when the users view the help screen, we simply pass HelpScreen.

If we want to track when users viewed the highscore screen we would call

The string value passed to trackPageView is entirely up to you. It makes sense to give them obvious names because when you view the Analytics dashboard you want to make it simple to understand.

If you start the tracker in manual mode you will need to call dispatch() to fire off the tracking events. This call is not required if you passed a time interval to the start call.

Finally, remember to stop the tracker when you no longer need it's services.

Once you have gone through your application and implemented tracking on the desired screens, kick back and relax while analytics does it magic. From my experience the analytics dashboard is updated daily, if anyone has a definitive answer please let me know. After a couple of days you will have clear indication of what areas of your application are most frequented visited.

You may be in for a surprise as to what your users are actually doing.

The analytics download includes a sample Android project as well for you to check out

Amazed 2 Free

Amazed 2 is now available in Android Market for free. The free version is ad supported while the the non-ad version is still available for 99cents.

Amazed 2 was originally released on 15th June. The 99c version has done alright, although nothing spectacular. If I had more spare time it would definitely be worthwhile implementing some updates I have written(untested though), but for now I am relegated to bug fixes.

Hopefully the ad supported version can spur some life into the title and give me an opportunity to keep the game fresh. I am using Mobclix for my ad provider. If they work out for me, I will write-up a follow up post on how to implement them in your own applications. Likewise if they don't work out I will let you know why.

If you have any feedback for Amazed 2 please get in contact.

Saturday, April 17, 2010

Device Seeding Program for Top Android Market Developers

Google have been kind enough to provide free Android devices to Android developers who meet their selection criteria. The criteria being 3.5 star or higher rating and more than 5,000 unique downloads. I received my email from Google on March 4th, with the actual device arriving on 24th which is great service for a free product.

While most developers already have a device you can never have to many. It is always useful to have a couple of devices running different OS versions as well as different screen resolutions. The emulator will only get you so far, being able to test your application on a variety of devices is always the best option.

I was lucky enough to receive a Nexus One (very glad because I already have a Droid). Below are a few pictures from when my device arrived.

This is a great good will gesture by Google towards developers. Thank you very much Google.

Sunday, April 11, 2010

Android Developer Day - Dallas

Yesterday I attended the Android Developer Day in Dallas. While it was mainly geared towards people just starting out with Android it was great to meet some fellow Dallas Android developers. Being new to the area I don't really know anyone so yesterday was very beneficial as a networking tool. All the speakers were knowledgeable with the platform and very friendly. Overall it was a great day and I'm definitely looking forward to future events.

As a bonus we all got free Android t-shirts.

Thanks to the following sponsors for making yesterdays event possible
Key Ring
Challenge (sorry don't have website)
TEK Systems

After speaking with a few people it is obvious I should blog more. It is actually harder than I thought to construct meaningful posts. So for the next few weeks I have set myself a goal of blogging more frequently.

We'll see how that pans out......