SlideShare a Scribd company logo
1 of 43
ADVANCE ANDROID APPLICATION DEVELOPMENT Ramesh Prasad
INTRODUCTION TO NATIVE 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.
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++.
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.
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.
Bionic
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.
Bionic Bionic has built-in support for important Android-specific services such as system properties and logging.  It doesn’t support certain POSIX features, like C++ exceptions and wide chars, which were not needed on Android.  Thus it’s not quite compatible with the gnu libc.  All native code must be compiled against bionic, not glibc.
Architecture Support The library is written to support ARM CPUs, though some x86 support is also present. There is no support for other CPU architectures
C++ Support So what is different about the Bionic libc versus glibc?  The most striking differences are in the C++ support C++ No C++ exceptions No STL Pthread
C++ exceptions The Bionic libc routines do not handle C++ exceptions. They neither throw exceptions themselves, nor will they pass exceptions from a called function back through to their caller.  So for example, if the cmp() routine passed to qsort() throws an exception the caller of qsort() will not see it. Support for C++ exceptions adds significant overhead to function calls, even just to pass thrown exceptions back to the caller.  As Android's primary programming language is Java, which handles exceptions entirely within the runtime package, the designers chose to omit the lower level exception support.
STL There is no C++ Standard Template Library included.  Developers are free supply their own, such as the free SGI implementation.
Pthread The pthread implementation appears to be completely new and developed by Google specifically for Android.  It is, quite deliberately, not a complete implementation of POSIX pthreads.  It implements those features necessary to support threads in the Dalvik JVM, and only selectively thereafter.
Pthread Mutexes, rwlocks, condvars, etc are all implemented using kernel futexes, which makes the user space implementation impressively simple. There is no pthread_cancel(). Threads can exit, but can not be killed by another thread. There is no pthread_atfork(). This routine is useful if you're going to fork from a threaded process, allowing cleanups of resources which should not be held in the child. Thread local storage is implemented, with up to 64 keys handled. Android reserves several of these for its own use: the per-thread id and errno, as well as two variables related to OpenGL POSIX realtime thread extensions like pthread_attr_{set,get}inheritsched and pthread_attr_{set,get}scope are not implemented.
DEVELOPING WITH NDK
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
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
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
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.
Developing Installation Introduction Managing projects Building & Running Debugging
Lab Hello World Tutorial ndroid-ndk-r4amplesello-jni
NATIVE APPLICATION DEVELOPMENT
Application Structure  Java Code Java Native Interface Native Code (Shared Object) Native Code (Static Library)
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.
C/C++ Code Must implement a JNI Interface #include <jni.h> jstring Java_com_example_hellojni_HelloJni_stringFromJNI(  JNIEnv* env, jobjectthiz ) {     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 Filename Function name
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.  The following C JNI function converts an array of C characters to a jstring: (*env)->NewStringUTF(env, lastfile)  Iscopy – returns the result JNI_TRUE if it made a local copy of the jstring or JNI_FALSE otherwise
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);
Passing Values/Arrays
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.
Makefile
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.
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)
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
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...
Module-description variables
Lab Hello JNI ndroid-ndk-r4amplesello-jni
USING PROCESSOR FEATURES
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.
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.
Usage 2 3 1
Features Two functions are provided for now: AndroidCpuFamilyandroid_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.
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.

More Related Content

What's hot

2018 top ide's for andriod development
2018 top ide's for andriod development2018 top ide's for andriod development
2018 top ide's for andriod developmentQamar Abbas
 
Android study jams 1
Android study jams 1Android study jams 1
Android study jams 1NancyMariaAS
 
Python Integrated Development Environment
Python Integrated Development EnvironmentPython Integrated Development Environment
Python Integrated Development EnvironmentTikendraPandey
 
Android Project Presentation
Android Project PresentationAndroid Project Presentation
Android Project PresentationLaxmi Kant Yadav
 
Selenium web driver_2.0_presentation
Selenium web driver_2.0_presentationSelenium web driver_2.0_presentation
Selenium web driver_2.0_presentationsayhi2sudarshan
 
The Ring programming language version 1.3 book - Part 4 of 88
The Ring programming language version 1.3 book - Part 4 of 88The Ring programming language version 1.3 book - Part 4 of 88
The Ring programming language version 1.3 book - Part 4 of 88Mahmoud Samir Fayed
 
Android software stack
Android software stackAndroid software stack
Android software stackSoba Arjun
 
Swift vs flutter pixel values technolabs
Swift vs flutter pixel values technolabsSwift vs flutter pixel values technolabs
Swift vs flutter pixel values technolabsPixel Values Technolabs
 
Android Workshop Day 1 Part 2
Android Workshop Day 1 Part 2Android Workshop Day 1 Part 2
Android Workshop Day 1 Part 2Ahsanul Karim
 
Java Programming (M&M)
Java Programming (M&M)Java Programming (M&M)
Java Programming (M&M)mafffffe19
 
Introduction to flutter's basic concepts
Introduction to flutter's basic conceptsIntroduction to flutter's basic concepts
Introduction to flutter's basic conceptsKumaresh Chandra Baruri
 
Browser core red bus presentation
Browser core red bus presentation Browser core red bus presentation
Browser core red bus presentation redBus India
 
Dload mobile development
Dload mobile developmentDload mobile development
Dload mobile developmentSayed Ahmed
 
iOS Development, with Swift and XCode
iOS Development, with Swift and XCodeiOS Development, with Swift and XCode
iOS Development, with Swift and XCodeWan Leung Wong
 
Cross platform app development with flutter
Cross platform app development with flutterCross platform app development with flutter
Cross platform app development with flutterHwan Jo
 

What's hot (20)

2018 top ide's for andriod development
2018 top ide's for andriod development2018 top ide's for andriod development
2018 top ide's for andriod development
 
Android study jams 1
Android study jams 1Android study jams 1
Android study jams 1
 
Python Integrated Development Environment
Python Integrated Development EnvironmentPython Integrated Development Environment
Python Integrated Development Environment
 
JyothishNewResume5exp
JyothishNewResume5expJyothishNewResume5exp
JyothishNewResume5exp
 
Android Project Presentation
Android Project PresentationAndroid Project Presentation
Android Project Presentation
 
Ide description
Ide descriptionIde description
Ide description
 
Selenium web driver_2.0_presentation
Selenium web driver_2.0_presentationSelenium web driver_2.0_presentation
Selenium web driver_2.0_presentation
 
Choose flutter
Choose flutterChoose flutter
Choose flutter
 
The Ring programming language version 1.3 book - Part 4 of 88
The Ring programming language version 1.3 book - Part 4 of 88The Ring programming language version 1.3 book - Part 4 of 88
The Ring programming language version 1.3 book - Part 4 of 88
 
Android software stack
Android software stackAndroid software stack
Android software stack
 
Swift vs flutter pixel values technolabs
Swift vs flutter pixel values technolabsSwift vs flutter pixel values technolabs
Swift vs flutter pixel values technolabs
 
Android Workshop Day 1 Part 2
Android Workshop Day 1 Part 2Android Workshop Day 1 Part 2
Android Workshop Day 1 Part 2
 
Java Programming (M&M)
Java Programming (M&M)Java Programming (M&M)
Java Programming (M&M)
 
Introduction to flutter's basic concepts
Introduction to flutter's basic conceptsIntroduction to flutter's basic concepts
Introduction to flutter's basic concepts
 
Ide
IdeIde
Ide
 
Android overview
Android overviewAndroid overview
Android overview
 
Browser core red bus presentation
Browser core red bus presentation Browser core red bus presentation
Browser core red bus presentation
 
Dload mobile development
Dload mobile developmentDload mobile development
Dload mobile development
 
iOS Development, with Swift and XCode
iOS Development, with Swift and XCodeiOS Development, with Swift and XCode
iOS Development, with Swift and XCode
 
Cross platform app development with flutter
Cross platform app development with flutterCross platform app development with flutter
Cross platform app development with flutter
 

Viewers also liked

Android Protips: Advanced Topics for Expert Android App Developers
Android Protips: Advanced Topics for Expert Android App DevelopersAndroid Protips: Advanced Topics for Expert Android App Developers
Android Protips: Advanced Topics for Expert Android App DevelopersReto Meier
 
Presentation on Android operating system
Presentation on Android operating systemPresentation on Android operating system
Presentation on Android operating systemSalma Begum
 
New features in android m upload
New features in android m   uploadNew features in android m   upload
New features in android m uploadBin Yang
 
Android OS and its Features
Android OS and its FeaturesAndroid OS and its Features
Android OS and its FeaturesHarshad Lokhande
 
Application component
Application componentApplication component
Application componenthome
 
Android vs i os features
Android vs i os featuresAndroid vs i os features
Android vs i os featuresGuang Ying Yuan
 
Android Development: Build Android App from Scratch
Android Development: Build Android App from ScratchAndroid Development: Build Android App from Scratch
Android Development: Build Android App from ScratchTaufan Erfiyanto
 
Advance Android Programming - learning beyond basics
Advance Android Programming - learning beyond basicsAdvance Android Programming - learning beyond basics
Advance Android Programming - learning beyond basicsayman diab
 
Non Conventional Android Programming (English)
Non Conventional Android Programming (English)Non Conventional Android Programming (English)
Non Conventional Android Programming (English)Davide Cerbo
 
Advance Android application development workshop day 1
Advance Android application development workshop day 1Advance Android application development workshop day 1
Advance Android application development workshop day 1cresco
 
Android Workshop 2013
Android Workshop 2013Android Workshop 2013
Android Workshop 2013Junda Ong
 
Memory problems in android programming
Memory problems in android programmingMemory problems in android programming
Memory problems in android programmingAiTi Education
 
Software proposal on android
Software proposal on androidSoftware proposal on android
Software proposal on androidKamrul Chowdhury
 
Asynchronous Programming in Android
Asynchronous Programming in AndroidAsynchronous Programming in Android
Asynchronous Programming in AndroidJohn Pendexter
 
Android UI Reference
Android UI ReferenceAndroid UI Reference
Android UI ReferenceGauntFace
 
Advance Android application development workshop day 3
Advance Android application development workshop day 3Advance Android application development workshop day 3
Advance Android application development workshop day 3cresco
 

Viewers also liked (20)

Android Protips: Advanced Topics for Expert Android App Developers
Android Protips: Advanced Topics for Expert Android App DevelopersAndroid Protips: Advanced Topics for Expert Android App Developers
Android Protips: Advanced Topics for Expert Android App Developers
 
Presentation on Android operating system
Presentation on Android operating systemPresentation on Android operating system
Presentation on Android operating system
 
Android ppt
Android ppt Android ppt
Android ppt
 
New features in android m upload
New features in android m   uploadNew features in android m   upload
New features in android m upload
 
Android OS and its Features
Android OS and its FeaturesAndroid OS and its Features
Android OS and its Features
 
Application component
Application componentApplication component
Application component
 
Android vs i os features
Android vs i os featuresAndroid vs i os features
Android vs i os features
 
Api List September
Api List SeptemberApi List September
Api List September
 
Android Development: Build Android App from Scratch
Android Development: Build Android App from ScratchAndroid Development: Build Android App from Scratch
Android Development: Build Android App from Scratch
 
Android ppt
Android pptAndroid ppt
Android ppt
 
Advance Android Programming - learning beyond basics
Advance Android Programming - learning beyond basicsAdvance Android Programming - learning beyond basics
Advance Android Programming - learning beyond basics
 
Non Conventional Android Programming (English)
Non Conventional Android Programming (English)Non Conventional Android Programming (English)
Non Conventional Android Programming (English)
 
Advance Android application development workshop day 1
Advance Android application development workshop day 1Advance Android application development workshop day 1
Advance Android application development workshop day 1
 
Android Workshop 2013
Android Workshop 2013Android Workshop 2013
Android Workshop 2013
 
Memory problems in android programming
Memory problems in android programmingMemory problems in android programming
Memory problems in android programming
 
Software proposal on android
Software proposal on androidSoftware proposal on android
Software proposal on android
 
Asynchronous Programming in Android
Asynchronous Programming in AndroidAsynchronous Programming in Android
Asynchronous Programming in Android
 
Android UI Reference
Android UI ReferenceAndroid UI Reference
Android UI Reference
 
Advance Android application development workshop day 3
Advance Android application development workshop day 3Advance Android application development workshop day 3
Advance Android application development workshop day 3
 
Android Components
Android ComponentsAndroid Components
Android Components
 

Similar to Advance Android Application Development

Android on IA devices and Intel Tools
Android on IA devices and Intel ToolsAndroid on IA devices and Intel Tools
Android on IA devices and Intel ToolsXavier Hallade
 
Using the android ndk - DroidCon Paris 2014
Using the android ndk - DroidCon Paris 2014Using the android ndk - DroidCon Paris 2014
Using the android ndk - DroidCon Paris 2014Paris Android User Group
 
Android Native Development Kit
Android Native Development KitAndroid Native Development Kit
Android Native Development KitPeter R. Egli
 
tybsc it asp.net full unit 1,2,3,4,5,6 notes
tybsc it asp.net full unit 1,2,3,4,5,6 notestybsc it asp.net full unit 1,2,3,4,5,6 notes
tybsc it asp.net full unit 1,2,3,4,5,6 notesWE-IT TUTORIALS
 
Getting started with the NDK
Getting started with the NDKGetting started with the NDK
Getting started with the NDKKirill Kounik
 
01 Introduction to programming
01 Introduction to programming01 Introduction to programming
01 Introduction to programmingmaznabili
 
LinkedIn - Disassembling Dalvik Bytecode
LinkedIn - Disassembling Dalvik BytecodeLinkedIn - Disassembling Dalvik Bytecode
LinkedIn - Disassembling Dalvik BytecodeAlain Leon
 
Build Great Networked APIs with Swift, OpenAPI, and gRPC
Build Great Networked APIs with Swift, OpenAPI, and gRPCBuild Great Networked APIs with Swift, OpenAPI, and gRPC
Build Great Networked APIs with Swift, OpenAPI, and gRPCTim Burks
 
.NET Core, ASP.NET Core Course, Session 3
.NET Core, ASP.NET Core Course, Session 3.NET Core, ASP.NET Core Course, Session 3
.NET Core, ASP.NET Core Course, Session 3aminmesbahi
 
(1) c sharp introduction_basics_dot_net
(1) c sharp introduction_basics_dot_net(1) c sharp introduction_basics_dot_net
(1) c sharp introduction_basics_dot_netNico Ludwig
 
The seven pillars of aspnet
The seven pillars of aspnetThe seven pillars of aspnet
The seven pillars of aspnetNethaji Naidu
 
Android ndk - Introduction
Android ndk  - IntroductionAndroid ndk  - Introduction
Android ndk - IntroductionRakesh Jha
 

Similar to Advance Android Application Development (20)

Android on IA devices and Intel Tools
Android on IA devices and Intel ToolsAndroid on IA devices and Intel Tools
Android on IA devices and Intel Tools
 
Using the android ndk - DroidCon Paris 2014
Using the android ndk - DroidCon Paris 2014Using the android ndk - DroidCon Paris 2014
Using the android ndk - DroidCon Paris 2014
 
Android Native Development Kit
Android Native Development KitAndroid Native Development Kit
Android Native Development Kit
 
Introduction to .net
Introduction to .netIntroduction to .net
Introduction to .net
 
tybsc it asp.net full unit 1,2,3,4,5,6 notes
tybsc it asp.net full unit 1,2,3,4,5,6 notestybsc it asp.net full unit 1,2,3,4,5,6 notes
tybsc it asp.net full unit 1,2,3,4,5,6 notes
 
Getting Native with NDK
Getting Native with NDKGetting Native with NDK
Getting Native with NDK
 
Getting started with the NDK
Getting started with the NDKGetting started with the NDK
Getting started with the NDK
 
01 Introduction to programming
01 Introduction to programming01 Introduction to programming
01 Introduction to programming
 
C# tutorial
C# tutorialC# tutorial
C# tutorial
 
LinkedIn - Disassembling Dalvik Bytecode
LinkedIn - Disassembling Dalvik BytecodeLinkedIn - Disassembling Dalvik Bytecode
LinkedIn - Disassembling Dalvik Bytecode
 
Build Great Networked APIs with Swift, OpenAPI, and gRPC
Build Great Networked APIs with Swift, OpenAPI, and gRPCBuild Great Networked APIs with Swift, OpenAPI, and gRPC
Build Great Networked APIs with Swift, OpenAPI, and gRPC
 
Introduction to Programming Lesson 01
Introduction to Programming Lesson 01Introduction to Programming Lesson 01
Introduction to Programming Lesson 01
 
.NET Core, ASP.NET Core Course, Session 3
.NET Core, ASP.NET Core Course, Session 3.NET Core, ASP.NET Core Course, Session 3
.NET Core, ASP.NET Core Course, Session 3
 
Dotnet basics
Dotnet basicsDotnet basics
Dotnet basics
 
Programming
ProgrammingProgramming
Programming
 
(1) c sharp introduction_basics_dot_net
(1) c sharp introduction_basics_dot_net(1) c sharp introduction_basics_dot_net
(1) c sharp introduction_basics_dot_net
 
Android NDK
Android NDKAndroid NDK
Android NDK
 
The seven pillars of aspnet
The seven pillars of aspnetThe seven pillars of aspnet
The seven pillars of aspnet
 
Android develop guideline
Android develop guidelineAndroid develop guideline
Android develop guideline
 
Android ndk - Introduction
Android ndk  - IntroductionAndroid ndk  - Introduction
Android ndk - Introduction
 

More from Ramesh Prasad

Multimedia on android
Multimedia on androidMultimedia on android
Multimedia on androidRamesh Prasad
 
Managing sales using CRM
Managing sales using CRMManaging sales using CRM
Managing sales using CRMRamesh Prasad
 
Signal processing in smartphones - 4G perspective
Signal processing in smartphones - 4G perspectiveSignal processing in smartphones - 4G perspective
Signal processing in smartphones - 4G perspectiveRamesh Prasad
 
Android Application Development
Android Application DevelopmentAndroid Application Development
Android Application DevelopmentRamesh Prasad
 
Sdk For Firmware Development
Sdk For Firmware DevelopmentSdk For Firmware Development
Sdk For Firmware DevelopmentRamesh Prasad
 
De Interlacing Techniques
De Interlacing TechniquesDe Interlacing Techniques
De Interlacing TechniquesRamesh Prasad
 

More from Ramesh Prasad (6)

Multimedia on android
Multimedia on androidMultimedia on android
Multimedia on android
 
Managing sales using CRM
Managing sales using CRMManaging sales using CRM
Managing sales using CRM
 
Signal processing in smartphones - 4G perspective
Signal processing in smartphones - 4G perspectiveSignal processing in smartphones - 4G perspective
Signal processing in smartphones - 4G perspective
 
Android Application Development
Android Application DevelopmentAndroid Application Development
Android Application Development
 
Sdk For Firmware Development
Sdk For Firmware DevelopmentSdk For Firmware Development
Sdk For Firmware Development
 
De Interlacing Techniques
De Interlacing TechniquesDe Interlacing Techniques
De Interlacing Techniques
 

Advance Android Application Development

  • 1. ADVANCE ANDROID APPLICATION DEVELOPMENT Ramesh Prasad
  • 3. 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.
  • 4. 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++.
  • 5. 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.
  • 6. 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.
  • 8. 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.
  • 9. Bionic Bionic has built-in support for important Android-specific services such as system properties and logging. It doesn’t support certain POSIX features, like C++ exceptions and wide chars, which were not needed on Android. Thus it’s not quite compatible with the gnu libc. All native code must be compiled against bionic, not glibc.
  • 10. Architecture Support The library is written to support ARM CPUs, though some x86 support is also present. There is no support for other CPU architectures
  • 11. C++ Support So what is different about the Bionic libc versus glibc? The most striking differences are in the C++ support C++ No C++ exceptions No STL Pthread
  • 12. C++ exceptions The Bionic libc routines do not handle C++ exceptions. They neither throw exceptions themselves, nor will they pass exceptions from a called function back through to their caller. So for example, if the cmp() routine passed to qsort() throws an exception the caller of qsort() will not see it. Support for C++ exceptions adds significant overhead to function calls, even just to pass thrown exceptions back to the caller. As Android's primary programming language is Java, which handles exceptions entirely within the runtime package, the designers chose to omit the lower level exception support.
  • 13. STL There is no C++ Standard Template Library included. Developers are free supply their own, such as the free SGI implementation.
  • 14. Pthread The pthread implementation appears to be completely new and developed by Google specifically for Android. It is, quite deliberately, not a complete implementation of POSIX pthreads. It implements those features necessary to support threads in the Dalvik JVM, and only selectively thereafter.
  • 15. Pthread Mutexes, rwlocks, condvars, etc are all implemented using kernel futexes, which makes the user space implementation impressively simple. There is no pthread_cancel(). Threads can exit, but can not be killed by another thread. There is no pthread_atfork(). This routine is useful if you're going to fork from a threaded process, allowing cleanups of resources which should not be held in the child. Thread local storage is implemented, with up to 64 keys handled. Android reserves several of these for its own use: the per-thread id and errno, as well as two variables related to OpenGL POSIX realtime thread extensions like pthread_attr_{set,get}inheritsched and pthread_attr_{set,get}scope are not implemented.
  • 17. 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
  • 18. 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
  • 19. 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
  • 20. 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.
  • 21. Developing Installation Introduction Managing projects Building & Running Debugging
  • 22. Lab Hello World Tutorial ndroid-ndk-r4amplesello-jni
  • 24. Application Structure Java Code Java Native Interface Native Code (Shared Object) Native Code (Static Library)
  • 25. 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.
  • 26. C/C++ Code Must implement a JNI Interface #include <jni.h> jstring Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobjectthiz ) { 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 Filename Function name
  • 27. 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. The following C JNI function converts an array of C characters to a jstring: (*env)->NewStringUTF(env, lastfile) Iscopy – returns the result JNI_TRUE if it made a local copy of the jstring or JNI_FALSE otherwise
  • 28. 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);
  • 30. 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.
  • 32. 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.
  • 33. 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)
  • 34. 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
  • 35. 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...
  • 37. Lab Hello JNI ndroid-ndk-r4amplesello-jni
  • 39. 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.
  • 40. 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.
  • 42. Features Two functions are provided for now: AndroidCpuFamilyandroid_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.
  • 43. 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.
  • 44. Lab Hello Neon ndroid-ndk-r4amplesello-neon
  • 46. Unit Testing Unit Testing Lab
  • 47. 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