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

Using the CCACHE (Should know)


The CCACHE is a compiler cache for C/C++ code. The Android framework distribution consists of several native libraries that live in the /external directory. You may have noticed that on a clean build, these libraries are rebuilt, even if they were never changed. The rebuilding of these libraries takes a lot of time. To save you that time, you can use the CCACHE mechanism. In this recipe, we will highlight certain tips and tricks that make the life of an Android systems developer easier. In particular, we will explain how to use the CCACHE mechanism, how to selectively compile modules, and how to test them.

Getting ready

Navigate to your Android sources root and issue the make clean command. We want to build up the cache from scratch.

How to do it...

  1. In a terminal at the Android sources root, execute the following commands:

    export USE_CCACHE=1
    export CCACHE_DIR=/<path_of_your_choice>/.ccache
    

    The preceding two environment variables control whether we use CCACHE, and if so, where the CCACHE directory is located. You are free to use any directory for the CCACHE.

  2. Inside prebuilt/linux-x86, there is the ccache directory that contains the ccache binary. Just executing the following command presents us with options of what this binary can do:

    prebuilt/linux-x86/ccache/ccache
    

    This command provides an output like:

    ccache, a compiler cache. Version 2.4
    Copyright Andrew Tridgell, 2002
    Usage:
      ccache [options]
      ccache compiler [compile options]
      compiler [compile options]    (via symbolic link)
    Options:
    -s                      show statistics summary
    -z                      zero statistics
    -c                      run a cache cleanup
    -C                      clear the cache completely
    -F <maxfiles>           set maximum files in cache
    -M <maxsize>            set maximum size of cache (use G, M or K)
    -h                      this help page
    -V                      print version number
    

    Therefore, we can set the maximum cache size with the help of the following command:

    prebuilt/linux-x86/ccache/ccache -M 20G
    

    This will set it to be 20 GB.

  3. Now, if you start a make for any target (choose emulator since it's the easiest), we can watch how the CCACHE is being used. In another terminal, use the following command:

    watch -n1 -d prebuilt/linux-x86/ccache/ccache -s
    

    The watch command is used to monitor the cache status and usage.

    The helper file should be saved as build_helper.sh:

    #!/bin/sh
    source build/envsetup.sh
    export USE_CCACHE=1
    export CCACHE_DIR=<YOUR_CACHE_PATH>/.ccache
    prebuilt/linux-x86/ccache/ccache -M 20G
    lunch $1

    An example invocation for the emulator target is:

    ./build_helper.sh 1
    

    and it outputs the following:

    including device/htc/passion/vendorsetup.sh
    including device/samsung/crespo/vendorsetup.sh
    Set cache size limit to 20971520k
    
    =========================================
    
    PLATFORM_VERSION_CODENAME=REL
    PLATFORM_VERSION=2.3.4
    TARGET_PRODUCT=generic
    TARGET_BUILD_VARIANT=eng
    TARGET_SIMULATOR=false
    TARGET_BUILD_TYPE=release
    TARGET_BUILD_APPS=
    TARGET_ARCH=arm
    HOST_ARCH=x86
    HOST_OS=linux
    HOST_BUILD_TYPE=release
    BUILD_ID=GRJ22
    
    =========================================

How it works...

The Android build system is written in such a way that if CCACHE is activated by the environment variable, it will be used automatically.

The above shell script is really simple and simplifies the developer's task of initializing an environment for further work. The commands within the shell script have to be executed whenever a new terminal is opened for Android systems development. Hence it is easier to execute one shell script instead of several individual commands.