Android KitKat Development Tools

Written by: on November 12

One of the best parts of working with the Android platform is the wide range of development tools available to make our everyday lives easier.

Many of us will have used ProGuard to shrink and optimize our code, or the hierarchy viewer for looking into why a few controls are not working how we expect. These development tools are essential to making our everyday lives easier and more productive. With the KitKat release, we have a few more tools added to our toolbox.

Procstats

Many brave souls have ventured into the Dalvik Debug Monitor Server (DDMS) when faced with memory usage questions. Even for an experienced user, DDMS can be a little overwhelming. In KitKat, we may be able to answer some of our more general, high level, memory usage questions with the new easy to use procstats tool.

Procstats is designed to give a high level overview of memory usage and runtime for your app and other ones running on the system. It’s especially useful when your application launches background services and you want to see how long they are running, or how much memory they are using. All this information is stored by the system automatically. This makes it easy to check memory and runtime assumptions at a glance without adding additional code or hooking up a profiler. For example, after running an application with two services that run once at startup, we can get the following output:

# Output from 'adb shell dumpsys procstats --details'.
# For those that prefer a GUI view on the device, this can be found
# at Settings > Developer Options > Process Stats > Select App

  * com.test.procstats / u0a51:
      * com.test.procstats / u0a51:
               TOTAL: 100% (4.4MB-5.0MB-6.1MB/3.0MB-3.1MB-3.1MB over 3)
                 Top: 1.7% (6.1MB-6.1MB-6.1MB/3.1MB-3.1MB-3.1MB over 1)
             Service: 90% (4.4MB-4.4MB-4.4MB/3.0MB-3.0MB-3.0MB over 2)
          Service Rs: 8.1%
      * com.test.procstats.ExpectedSlowService:
        Process: com.test.procstats
            Running count 2 / time 0.23%
            Started count 1 / time 0.23%
          Executing count 2 / time 0.01%
      * com.test.procstats.ExpectedQuickService:
        Process: com.test.procstats
            Running count 1 / time 92%
            Started count 1 / time 92%
          Executing count 1 / time 0.01%

We see here that the app has two services, ExpectedSlowService and ExpectedQuickService. These are the services that run once at app startup. However, we can also see that our knowledge of these services, our assumptions, are contrary to what procstats reports. The service we are expecting to run quickly is running 92% of the apps run time! Ok, maybe that total time is not very long. Let us have another look:

# Output from 'adb shell dumpsys -a'.

* com.test.procstats / u0a51:
      Process com.test.procstats (3 entries):
        Screen On  / Norm / Top       : +30s259ms
                            Service   : +26m35s118ms (running)
                            Service Rs: +2m23s130ms
                            TOTAL     : +29m28s507ms
        # ... snip ...
        mActive=true
        mNumActiveServices=1 mNumStartedServices=1
      Service com.test.procstats.ExpectedSlowService:
        Process: com.test.procstats
        Running op count 2:
          Screen On  / Norm / +4s116ms
                       TOTAL: +4s116ms
        # ... snip ...
      Service com.test.procstats.ExpectedQuickService:
        Process: com.test.procstats
        Running op count 1:
          Screen On  / Norm / +27m4s66ms (running)
                       TOTAL: +27m4s66ms
        # ... snip ...

In the first few lines it is apparent our service is running for a lot longer than we expected. Also, we can see that the service is still running which leads me to believe something is hung up in the service. Just by taking a quick glance at this report, we already have a good idea of where the problem is and its severity.

Screenrecord

If you have ever had to record a video of your device, then you know how much of a hassle it was. There are a variety of apps in the marketplace, both free and paid, and many requiring the phone to be rooted, that try to solve this problem. Often, with the apps that don’t require a rooted phone, there are issues with device support and recording. Another method is to use a video camera or webcam to record the video.  I have even seen custom stands made to hold a camera over the screen. If you are shaking your head and remember doing something similar, have no fear, support for screen recording is here!

KitKat includes a shell utility for screen recording. This utility can be accessed via the command line using ‘adb shell screenrecord,’ or if you use Android Studio there is a button for it in the Android DDMS window. The utility supports changing the video size, bit rate, and even setting a time limit for the recording.

These tools are just two of the new ones available to Android developers with the KitKat release. It is great to see these additions and the continued support to previous ones. I hope you all find them as helpful as I do.

This piece is the second of eight in our KitKat Developer’s Guide. Check back later this week for new updates or follow us on twitter.

Caleb Smith

Caleb Smith is a Software Engineer at Double Encore, specializing in Android Development.

Article


Add your voice to the discussion: