Book Image

Instant Android Systems Development How-to

By : Earlence Fernandes
Book Image

Instant Android Systems Development How-to

By: Earlence Fernandes

Overview of this book

<p>Android is by far the most popular open source mobile operating system. Learning to write high quality code at the platform level and learning how the systems works internally is a vital skill. This book teaches you these skills with clear and concise explanations and code examples.</p> <p>Instant Android Systems Development How-to provides a gentle introduction to the platform internals without sacrificing depth. Source code examples are designed to be meaningful, but at the same time, do not disguise their real purpose, which is to illustrate systems development techniques and common design patterns in android systems programming. Readers will be guided through several examples that give a hands-on experience.</p> <p>Readers begin by downloading the android source code, which is a topic of much discussion on android forums. They are then guided through the android boot process, and later on learn various common android systems development paradigms. More importantly, the book provides advice on when to use certain techniques which is often a mystery for the novice developer. Readers who complete the book will have high confidence in developing good systems code for Android.</p> <p>The book discusses how to setup a development machine and how to obtain the android source code and kernel code. It describes the source code organization and how the system boots up with precise references to various points in the source code. It highlights the common systems design patterns followed and how to create a custom system service. It then covers the all important flashing of phones. This is a topic of much confusion and the book provides direct steps to achieve safe flashing of developer phones. It describes the user application library mechanism and the platform library mechanism. Native code is needed for certain operations and an example service utilizing native code is explained. Modification of core system applications is explained and useful tips are provided on how to speed up the build-test cycle. The book concludes with a case study of two real world android platform extensions which give the user a reference while developing their own extensions.</p> <p>Instant Android Systems Development How-to is a well rounded book on platform internals that provides simple explanations without sacrificing depth and rigor.</p>
Table of Contents (7 chapters)
Instant Android Systems Development How-to
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
1
Instant Android Systems Development How-to

Modifying the search widget application (Should know)


In this recipe, we will modify the default search text in the quick search widget. The aim of this recipe is to make the reader comfortable with making changes, compiling them, and testing those changes.

Getting ready

The search widget's code is in the directory named QuickSearchBox under ANDROID_SRC/packages/apps/. Navigate to this directory and open up ANDROID_SRC/packages/apps/QuickSearchBox/res/drawable/text_field_search_empty_google.xml.

How to do it...

  1. Edit the XML layout file by replacing:

    <item android:drawable="@drawable/hint_google" />

    with

    <item android:drawable="@drawable/packt" />
  2. Now, use a popular image editor to create an icon of your choice with dimensions of 97 x 40 pixels. We create a Packt logo variant for this task in GIMP. Make sure you save the image as a PNG type. Copy this image to three locations: drawable-hdpi, drawable-ldpi, and drawable-mdpi.

    Note

    For each screen density, an image with different parameters should be stored. Otherwise, you might notice problems in rendering; images may not appear to be clear. As we are simply demonstrating a concept here, I do not focus on creating differently sized images.

  3. Now we will build the new QuickSearchWidget application. In a terminal, as usual, navigate to the Android source directory, include the environment setup and lunch the emulator target. Issue the following command:

    mmm packages/app/QuickSearchWidget
    
  4. This will build only the search widget. After building is complete, start the emulator and drop to an adb shell. We need to remount the system partition as read/write, since system applications are installed on to this partition which is mounted read-only. To be able to write a new executable of the QuickSearchWidget application, and without rebuilding and reflashing the entire operating system, we will simply "push" the generated APK onto the remounted directory.

  5. We need to find out where the /system partition is mounted. In an adb shell, execute the following command:

    cat /proc/mtd
    

    The output will be similar to the following:

    dev:    size   erasesize  name
    
    mtd0: 0c200000 00020000 "system"
    
    mtd1: 0c200000 00020000 "userdata"
    
    mtd2: 04000000 00020000 "cache"

    Note

    On a real device, this will appear differently. The MTD mounts vary based on the manufacturer of the device. The output seen here is on an emulator, which is where you should be doing initial development and debugging before trying anything on a real device.

  6. We see that /system exists in mtd0. To remount as read/write, in an adb shell:

    mount -o rw,remount -t yaffs2 /dev/block/mtd0 /system
    chmod 777 /system/app
    
  7. Now, exit the adb shell and navigate to ANDROID_SRC/out/target/product/generic/system/app. This is where the built APK is stored.

  8. In a terminal, execute the following command:

    adb push QuickSearchWidget.apk /system/app
    

    This will install the new widget. The change should be visible on the emulator:

How it works...

The mmm command selectively builds code. The argument to it is the path to the source directory containing an Android.mk file. The path should end with the name of a target that can be built. For example, if you open the Android.mk file, the target is identified by the LOCAL_PACKAGE_NAME tag.

As we have seen in earlier recipes, the system partition is mounted as read-only for security reasons. To install a system application, we need to remount this. The remount can only be done by a root user. On the emulator, ADB runs as root by default.

The side effect of simply pushing an APK to /system/app results in its installation.

The different drawable directories are used based on different screen densities. This has the same meaning as the directories used when writing SDK-based apps.