advance android application development

43
ADVANCE ANDROID APPLICATION DEVELOPMENT Ramesh Prasad

Upload: ramesh-prasad

Post on 30-May-2015

3.885 views

Category:

Documents


3 download

DESCRIPTION

Advance Android training featuring NDK and Testing.

TRANSCRIPT

Page 1: Advance Android Application Development

ADVANCE ANDROID APPLICATION DEVELOPMENT

Ramesh Prasad

Page 2: Advance Android Application Development

INTRODUCTION TO NATIVE DEVELOPMENT

Page 3: Advance Android Application Development

Native Development

• Android applications run in the Dalvik virtual machine.

• The Native Development allows you to implement parts of your applications using native-code languages such as C and C++.

• This can provide benefits to certain classes of applications, in the form of reuse of existing code and in some cases increased speed.

Page 4: Advance Android Application Development

When to Develop in Native Code

• The Native Development will not benefit most applications.

• As a developer, you need to balance its benefits against its drawbacks; notably, using native code does not result in an automatic performance increase, but always increases application complexity.

• In general, you should only use native code if it is essential to your application, not just because you prefer to program in C/C++.

Page 5: Advance Android Application Development

When to Develop in Native Code

• Typical good candidates for the NDK are self-contained, CPU-intensive operations that don't allocate much memory, such as signal processing, physics simulation, and so on.

• Simply re-coding a method to run in C usually does not result in a large performance increase.

• When examining whether or not you should develop in native code, think about your requirements and see if the Android framework APIs provide the functionality that you need.

• The NDK can, however, can be an effective way to reuse a large corpus of existing C/C++ code.

Page 6: Advance Android Application Development

When to Develop in Native Code

• Write your application using the Android framework and use JNI to access the APIs provided by the Android NDK.

• This technique allows you to take advantage of the convenience of the Android framework, but still allows you to write native code when necessary.

• You can install applications that use native code through the JNI on devices that run Android 1.5 or later.

Page 7: Advance Android Application Development

Bionic

Page 8: Advance Android Application Development

Bionic

• Google developed a custom library for the C compiler (libc) called Bionic. This was necessary for three main reasons: – License: they wanted to keep GPL out of user-space.

Bionic code uses the BSD license. – Size: the library has to be loaded in each process, so it

needs to be small. Bionic is about 200K, or half the size of glibc (the GNU version of libc).

– Speed: limited CPU power means it needs to be fast. Bionic has a small size and fast code paths, including a very fast and small custom pthread implementation.

Page 9: Advance Android Application Development

DEVELOPING WITH NDK

Page 10: Advance Android Application Development

NDK

• The Android NDK is a toolset that lets you embed components that make use of native code in your Android applications.

• The NDK provides:– A set of tools and build files used to generate native code libraries

from C and C++ sources– A way to embed the corresponding native libraries into an

application package file (.apk) that can be deployed on Android devices

– A set of native system headers and libraries that will be supported in all future versions of the Android platform, starting from Android 1.5.

– Documentation, samples, and tutorials

Page 11: Advance Android Application Development

NDK

• The latest release of the NDK supports these ARM instruction sets:– ARMv5TE (including Thumb-1 instructions)– ARMv7-A (including Thumb-2 and VFPv3-D16

instructions, with optional support for NEON/VFPv3-D32 instructions)

– x86 instructions

Page 12: Advance Android Application Development

Libraries• It provides a set of system headers for stable native APIs that are

guaranteed to be supported in all later releases of the platform:– libc (C library) headers– libm (math library) headers– JNI interface headers– libz (Zlib compression) headers– liblog (Android logging) header– OpenGL ES 1.1 and OpenGL ES 2.0 (3D graphics libraries) headers– libjnigraphics (Pixel buffer access) header (for Android 2.2 and above).– A Minimal set of headers for C++ support– OpenSL ES native audio libraries– Android native application APIS

Page 13: Advance Android Application Development

Build System

• The NDK also provides a build system that lets you work efficiently with your sources, without having to handle the toolchain/platform/CPU/ABI details.

• You create very short build files to describe which sources to compile and which Android application will use them — the build system compiles the sources and places the shared libraries directly in your application project.

Page 14: Advance Android Application Development

Developing

• Installation• Introduction• Managing projects• Building & Running• Debugging

Page 15: Advance Android Application Development

Lab

• Hello World Tutorial– \android-ndk-r4\samples\hello-jni

Page 16: Advance Android Application Development

NATIVE APPLICATION DEVELOPMENT

Page 17: Advance Android Application Development

Application Structure

Java Code

Java Native Interface

Native Code(Shared Object)

Native Code(Static Library)

Page 18: Advance Android Application Development

Java Code• Your application's source code will declare one or more methods with the 'native'

keyword to indicate that they are implemented through native code. E.g.: native String stringFromJNI();

• You must provide a native shared library that contains the implementation of these methods, which will be packaged into your application's .apk. This library must be named according to standard Unix conventions as lib<something>.so, and shall contain a standard JNI entry point (more on this later). For example:

libhello-jni.so

• Your application must explicitely load the library. For example, to load it at application startup, simply add the following to its source code:

static { System.loadLibrary("hello-jni"); }

• Note that you should not use the 'lib' prefix and '.so' suffix here.

Page 19: Advance Android Application Development

C/C++ Code• Must implement a JNI Interface

#include <jni.h>

jstringJava_com_example_hellojni_HelloJni_stringFromJNI(

JNIEnv* env, jobject thiz ){ return (*env)->NewStringUTF(env, "Hello from JNI !");}

• JNIEnv *: A pointer to the JNI environment. This pointer is a handle to the current thread in the Java virtual machine, and contains mapping and other hosuekeeping information.

• jobject: A reference to the method that called this native code. If the calling method is static, this parameter would be type jclass instead of jobject.

• jstring: The value returned by the native method.

Package & Java FilenameFunction name

Page 20: Advance Android Application Development

Passing Strings• The String object in the Java language, which is represented as jstring in Java

Native Interface (JNI), is a 16 bit unicode string. • In C a string is by default constructed from 8 bit characters. So, to access a

Java language String object passed to a C or C++ function or return a C or C++ string to a Java language method, you need to use JNI conversion functions in your native method implementation.

Iscopy – returns the result JNI_TRUE if it made a local copy of the jstring or JNI_FALSE otherwise

The following C JNI function converts an array of C characters to a jstring: (*env)->NewStringUTF(env, lastfile)

Page 21: Advance Android Application Development

Passing Strings

• To let the Java virtual machine know you are finished with the UTF representation, call the ReleaseStringUTFChars conversion function as shown below.

• The second argument is the original jstring value used to construct the UTF representation, and the third argument is the reference to the local representation of that String. (*env)-ReleaseStringUTFChars(env, name, mfile);

Page 22: Advance Android Application Development

Passing Values/Arrays

Page 23: Advance Android Application Development

Makefile• An Android.mk file is written to describe your sources to the build

system. More specifically:– The file is really a tiny GNU Makefile fragment that will be parsed one or more

times by the build system.– The file syntax is designed to allow you to group your sources into 'modules'.

A module is one of the following:– a static library– a shared library

• Only shared libraries will be installed/copied to your application package. Static libraries can be used to generate shared libraries though. You can define one or more modules in each Android.mk file, and you can use the same source file in several modules.

• The build system handles many details for you. For example, you don't need to list header files or explicit dependencies between generated files in your Android.mk. The NDK build system will compute these automatically for you.

Page 24: Advance Android Application Development

Makefile

Page 25: Advance Android Application Development

Makefile• LOCAL_PATH := $(call my-dir)• An Android.mk file must begin with the definition of the LOCAL_PATH variable. It is used to locate source

files in the development tree. In this example, the macro function 'my-dir', provided by the build system, is used to return the path of the current directory (i.e. the directory containing the Android.mk file itself).

• include $(CLEAR_VARS)• The CLEAR_VARS variable is provided by the build system and points to a special GNU Makefile that will

clear many LOCAL_XXX variables for you (e.g. LOCAL_MODULE, LOCAL_SRC_FILES, LOCAL_STATIC_LIBRARIES, etc...), with the exception of LOCAL_PATH.

• LOCAL_MODULE := hello-jni• The LOCAL_MODULE variable must be defined to identify each module you describe in your Android.mk.

The name must be *unique* and not contain any spaces. Note that the build system will automatically add proper prefix and suffix to the corresponding generated file. In other words, a shared library module named 'foo' will generate 'libfoo.so'.

• LOCAL_SRC_FILES := hello-jni.c• The LOCAL_SRC_FILES variables must contain a list of C and/or C++ source files that will be built and

assembled into a module. Note that you should not list header and included files here, because the build system will compute dependencies automatically for you; just list the source files that will be passed directly to a compiler, and you should be good.

Page 26: Advance Android Application Development

NDK-provided function macros

• The following are GNU Make 'function' macros, and must be evaluated by using '$(call <function>)'.

• They return textual information.– my-dir

Returns the path of the current Android.mk's directory, relative to the top of the NDK build system. This is useful to define LOCAL_PATH at the start of your Android.mk as with:

LOCAL_PATH := $(call my-dir)

Page 27: Advance Android Application Development

NDK-provided function macros• all-subdir-makefiles

Returns a list of Android.mk located in all sub-directories of the current 'my-dir' path. For example, consider the following hierarchy:

• sources/foo/Android.mk• sources/foo/lib1/Android.mk• sources/foo/lib2/Android.mk

– If sources/foo/Android.mk contains the single line: include $(call all-subdir-makefiles)– Then it will include automatically sources/foo/lib1/Android.mk and

sources/foo/lib2/Android.mk– This function can be used to provide deep-nested source directory

hierarchies to the build system. Note that by default, the NDK will only look for files in sources/*/Android.mk

Page 28: Advance Android Application Development

NDK-provided function macros

• this-makefile Returns the path of the current Makefile (i.e. where the

function is called).

• parent-makefile Returns the path of the parent Makefile in the inclusion tree, i.e. the path of the Makefile that included the current one.

• grand-parent-makefile Guess what...

Page 29: Advance Android Application Development

Module-description variables

ANDROID-MK.TXT

Page 30: Advance Android Application Development

Lab

• Hello JNI– \android-ndk-r4\samples\hello-jni

Page 31: Advance Android Application Development

USING PROCESSOR FEATURES

Page 32: Advance Android Application Development

USING PROCESSOR FEATURES

• This NDK provides a small library named "cpufeatures" that can be used at runtime to detect the target device's CPU family and the optional features it supports.

Page 33: Advance Android Application Development

Usage• The library is available from sources/cpufeatures. • It provides an Android.mk build script that can be used to build it as a

static library. To use it, you must:– include '$(NDK_ROOT)/sources/cpufeatures/Android.mk' at the start or end

of your Android.mk file.

– add '$(NDK_ROOT)/sources/cpufeatures' to your LOCAL_C_INCLUDES definition.

– add 'cpufeatures' to your LOCAL_STATIC_LIBRARIES definition when building your final shared library.

– your source code can then #include <cpu-features.h> to compile against it.

Page 34: Advance Android Application Development

Usage

1

2

3

Page 35: Advance Android Application Development

Features

• Two functions are provided for now:– AndroidCpuFamily android_getCpuFamily();– uint64_t android_getCpuFeatures();

• AndroidCpuFamily– Returns the target device's CPU Family as an

enum. – For now, the only supported family is

ANDROID_CPU_FAMILY_ARM.

Page 36: Advance Android Application Development

Features• android_getCpuFeatures() Returns the set of optional features

supported by the device's CPU. The result is a set of bit-flags, each corresponding to one CPU Family-specific optional feature.

• Currently, only the following flags are defined, for the ARM CPU Family:• ANDROID_CPU_ARM_FEATURE_ARMv7

– Indicates that the device's CPU supports the ARMv7-A instruction set as supported by the "armeabi-v7a" abi

• ANDROID_CPU_ARM_FEATURE_VFPv3– Indicates that the device's CPU supports the VFPv3 hardware FPU instruction

set extension. Due to the definition of 'armeabi-v7a', this will always be the case if ANDROID_CPU_ARM_FEATURE_ARMv7 is returned.

• ANDROID_CPU_ARM_FEATURE_NEON– Indicates that the device's CPU supports the ARM Advanced SIMD (a.k.a.

NEON) vector instruction set extension.

Page 37: Advance Android Application Development

Lab

• Hello Neon– \android-ndk-r4\samples\hello-neon

Page 38: Advance Android Application Development

TESTING/INSTRUMENTATION

Page 39: Advance Android Application Development

Unit Testing

• Unit Testing Lab

Page 40: Advance Android Application Development

Monkey

• The infinite monkey theorem states that a monkey hitting keys at random on a typewriter keyboard for an infinite amount of time will almost surely type a given text, such as the complete works of William Shakespeare.

• adb shell monkey -p com.example.android.apis -v 500

Page 41: Advance Android Application Development

PROFILING

Page 42: Advance Android Application Development

PROFILING

• Profiling Lab

Page 43: Advance Android Application Development

The End

[email protected]