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.

Platform library project organization

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.

System application project organization

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).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.108.236