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
.
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
.
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); } }
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.
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)
make PacktLibraryClient
make
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
PlatformLibraryClient
application. Logcat should output something similar to the following:I/PacktDESTest( 425): DES bytes: fd068bdc755be524
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.
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.
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 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).
3.12.108.236