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

Creating the platform client (Should know)


To test the platform library created, we need to create an Android application that uses the library. To do this, we will create a new "System APK". A System APK is an Android application that lives in the Read-Only /system partition on the device, similar to applications such as settings and contacts. System applications live in ANDROID_SRC/packages/apps.

Getting ready

Create a directory named PacktLibraryClient at that location. Inside, we write a small Android application that accesses the platform library and invokes a method.

Create the following file at ANDROID_SRC/packages/apps/PacktLibraryClient/src/com/packtclient.

How to do it…

  1. We begin by writing the client file that will access our platform library. The following code is saved as Client.java:

    package com.packtclient;
    import packt.platformlibrary.PacktPlatformLibrary;
    import android.app.Activity;
    import android.os.Bundle;
    /**
     * utilize the packt DES encryption platform library
     */
    
    public class Client extends Activity {
    
        @Override
    
        public void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
            byte [] encr = PacktPlatformLibrary.encryptDES("password", "Packt");
    
            PacktPlatformLibrary.printHex(encr);        
    
        }
    }
  2. Like any other Android application, we need a manifest file, which is created at ANDROID_SRC/packages/apps/PacktLibraryClient/. The following code is saved in a file named AndroidManifest.xml:

    <?xml version="1.0" encoding="utf-8"?>
    
    <!-- This is an example of writing a client application for a custom
    
         platform library. -->
    
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    
        package="com.packtclient">
        <application android:label="Packt Library Client">
            <!-- This tells the system about the custom library used by the
    
                 application, so that it can be properly loaded and linked
    
                 to the app when the app is initialized. -->
    
            <uses-library android:name="PacktPlatformLibrary" />
            <activity android:name="Client">
    
                <intent-filter>
    
                    <action android:name="android.intent.action.MAIN"/>
    
                    <category android:name="android.intent.category.LAUNCHER"/>
    
                </intent-filter>
    
            </activity>
    
        </application>
    
    </manifest>

    The new addition here is the <uses-library> tag. Notice that we have to specify the name of our custom platform library to indicate to the runtime that it is to be loaded with our client application.

  3. Finally, we need a make file at the same directory level as the preceding file. Save this make file as Android.mk:

    # This makefile is an example of writing an application that will link against
    # a custom shared library included with an Android system.
    LOCAL_PATH:= $(call my-dir)
    include $(CLEAR_VARS)
    LOCAL_MODULE_TAGS := optional
    # This is the target being built.
    LOCAL_PACKAGE_NAME := PacktLibraryClient
    # Only compile source java files in this apk.
    LOCAL_SRC_FILES := $(call all-java-files-under, src)
    # Link against the current Android SDK.
    #LOCAL_SDK_VERSION := current
    # Also link against our own custom library.
    
    LOCAL_JAVA_LIBRARIES := PacktPlatformLibrary
    LOCAL_PROGUARD_ENABLED := disabled
    include $(BUILD_PACKAGE)

    Note

    Notice the use of the LOCAL_JAVA_LIBRARIES tag that is used to specify the platform library against which we compile.

  4. Now we are ready to build our client APK. In a terminal, execute the following command (assuming the terminal environment is properly set up; for instructions, refer to the first recipe of this book):

    make PacktLibraryClient
    
  5. Finally, we need to build the system image for the emulator to test our code:

    make
    
  6. Ensure that the following files are included in the system image. This is done by inspecting the contents of installed-files.txt, which is located at ANDROID_SRC/out/target/product/generic/ in the case of an emulator build. Here, I have extracted the relevant contents from my copy:

            1978  /system/framework/PacktPlatformLibrary.jar
             119  /system/etc/permissions/PacktPlatformLibrary.xml
            3563  /system/app/PlatformLibraryClient.apk
  7. Therefore, all of the required pieces have been integrated into the system image. Start the emulator, and click on the PlatformLibraryClient application. Logcat should output something similar to the following:

    I/PacktDESTest(  425): DES bytes: fd068bdc755be524
    

How it works…

The platform client is simply another Android application. The only difference here is that it is bundled with the system image and is installed at /system/app—the read-only partition.

The most important line in the make file for the application is the LOCAL_JAVA_LIBRARIES tag. This specifies that we will use the functionality of the platform client.

There's more...

To help clarify the concepts presented in this recipe, it is often helpful to visualize the project structure. In the following text, we pictorially depict what platform libraries look like in the Android sources.

Platform library project organization

Most platform libraries are structured as shown in the next figure. As stated earlier, the top-level directory can change from ANDROID_SRC/vendor to ANDROID_SRC/device/ if you move from Gingerbread development to Ice Cream Sandwich development.

System application project organization

System applications have the following structure within the Android Sources. This figure depicts the organization in terms of the PacktLibraryClient system application we just built, but the organization is similar to other system applications. The point to note here is that system applications live under ANDROID_SRC/packages/apps and for the most part, the directory structure is the same as a normal Android application.

Our previous system service contained only Java code; however, such services may also contain calls to native code. Developers may choose to use native code for a variety of reasons. The most common being speed over interpreted code and re-use of existing libraries. In the next recipe, we will show you how to use native functions inside our PacktCrypto system service.

Also, before jumping to the next recipe, I assume that you are comfortable with JNI technology. If not, I recommend reading the excellent book: The Java Native Interface: Programmer's Guide and Specification by Sheng Liang. (http://www.amazon.com/Java-Native-Interface-Programmers-Specification/dp/0201325772).