SlideShare a Scribd company logo
1 of 28
Android NDK




              by Abdun Nur Tomal
Step 1: Installing C/C++ Support on Eclipse

• Choose Eclipse Galileo to update
• Let the item preference tree load, and check
  Eclipse C/C++ Development Tools in the
  Programming Languages branch
• From -
  http://www.eclipse.org/cdt/downloads.php
Step : Installing Cygwin
• Android is Linux based, and thus it is no
  surprise that when build native code for it,
  need some Unix tools.
• On Windows, NDK supports Cygwin 1.7.x and
  above.
• It’s just a set of software that emulates Unix
  environment on Windows
• get Cygwin, go to cygwin.com
“Install or update Cygwin now!”
• Cygwin’s setup.exe will download and run




• Choose Install from Internet, then click Next, then
  choose the installation directory
(be sure to choose a directory path that contains no spaces in it) like –
                                 C:/cygwin
Base packages installed,
           Need the development packages
• After choose the mirror and click Next, Cygwin will download and
  present to you the list of available packages:

• By default, only the base packages are installed. We, however, need
  the development packages. Rather than picking the packages we
  think we need

• We install the entire Devel branch.

• Click (once) on the word “Default” next to the root Devel node and
  wait few seconds while the setup hangs.

• When it is back, you will see that “Default” changes to “Install” for
  the Devel node, just like on the screenshot above.
• Click it once, let the Cygwin console start up and initialize:
• should see the same response that tells us that GNU Make is
  present in our Unix environment that is emulated by Cygwin.
Android rich programming environment,
While the Android SDK provides a very
                                      NDK
The Android NDK broadens the horizons and can speed up the delivery
  of desired functionality by bringing in existing source code, some of
  which may be proprietary and some of which may be open source
  code.

The NDK includes all the components necessary to incorporate
  functionality written in C into an Android application.

As of r5 of the NDK application,
• authors can write a significant portion of an application directly in
   C, including user interface and event-handling capability.
As of r4b of the NDK application,
• The features enabling the image handling functionality
   demonstrated here were introduced with the r4b version of the
   NDK.
NDK Build
Two common uses of the NDK are
  - to increase application performance and
        Application performance improvements are available when
  carefully crafted functions written in C are leveraged to perform
  memory-based or computationally intensive operations.
  - to leverage existing C code by porting it to Android.
        The NDK is to port an existing body of C code written for
  another platform, such as Linux.

The NDK contains a compiler and build scripts, allowing
  - to focus on the C source files and
  - leave the build magic to the NDK installation.

The NDK build process is easily incorporated into the Eclipse
  development environment,
Step 3: Installing the Android NDK
• Android NDK itself and place it on our
  filesystem.
• Can get NDK from the official Android site
• Be sure that there are no spaces in the path.
• Extract it to C:,
• so the path is C:android-ndk-r6.
Step 4: Making a Basic NDK App
• The general idea of using NDK in apps is to put
  your native pieces of code into libraries that
  you can then consume from the Java code.
• Use the New Android Project wizard
• However, an important thing to check , it is
  spaces in the path again.
•    In the project named ... Make a folder called jni in the root of the project (right-
     click the project node, New – Folder).
•    Create a file called Android.mk (New – File) within that folder with the following
     contents:
     LOCAL_PATH := $(call my-dir)
     include $(CLEAR_VARS)
# Here we give our module name and source file(s)
     LOCAL_MODULE := ndk_test
     LOCAL_SRC_FILES := ndk_test.c
     include $(BUILD_SHARED_LIBRARY)

The Android.mk file is important for the NDK build process to recognize your NDK modules. In our case we named our
     module ndk_test and told the build tool that it consists of one source file named ndk_test.c. Let’s create it in the
     same jni folder:

#include <string.h>
#include <jni.h>
jstring Java_com_mindtherobot_samples_ndkfoo_NdkFooActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis)
       { return (*env)->NewStringUTF(env, "Hello from native code!"); }
• the C function – it matches the Java class name
• create a binary library from the C source that we
  wrote,
• use a combination of Cygwin and Android NDK
  tools.
• Launch the Cygwin console and use the cd
  command to go directly to the folder where your
  project is.
• the command line is:
  /cygdrive/c/android-ndk-r4/ndk-build
• a successful run of the ndk-build tool will
  create an .so file in a new folder called libs

• The .so file is the binary library that will be
  included into the application .apk package
  and will be available for the Java code of the
  app to link to.
Ndk_testActivity class to use the NDK code:
package com.mindtherobot.samples.ndkfoo;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;

public class Ndk_testActivity extends Activity {
     // load the library - name matches jni/Android.mk
     static {
              System.loadLibrary("ndkfoo");
              }
     // declare the native code function - must match ndk_test.c private native
     String invokeNativeFunction();
     @Override
     public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
               // this is where we call the native code
              String hello = invokeNativeFunction();
              new AlertDialog.Builder(this).setMessage(hello).show();
              }
}
How to Build FFmpeg for Android
• Download Android NDK r5b
To download android ndk revision 5b and rev.6 use the following command:
    http://dl.google.com/android/ndk/android-ndk-r5b-linux-x86.tar.bz2
    http://dl.google.com/android/ndk/android-ndk-r6-linux-x86.tar.bz2

•   Download Source Code for Ffmpeg
     - http://ffmpeg.org/download.html
           https://github.com/FFmpeg/FFmpeg
      - The FFmpeg 0.8 “Love” need to downloaded.
            http://ffmpeg.zeranoe.com/builds/

•   Build FFmpeg
    (The script is based on RockPlayer build script)
    RockPlayer open source component: http://rockplayer.freecoder.org/tech.html
           http://roman10.net/src/build_android_r5b.txt
           http://roman10.net/src/build_android_r6.txt

•   The Output of the Build
All about JNI
• Java Native Interface (JNI)
• The JNI is a part of the Java platform,
  programmers can address interoperability
  issues once, and expect their solution to work
  with all implementations of the Java platform.
“Applications written in the Java programming
  language as well as in native (C, C++, etc.)
  programming languages.”
Activity Seems
    clarify the exact scope of the programming environments for these
                                    languages.
•    Java applications are written in the Java programming
     language, and compiled into a machine-independent binary class
     format.
•    The Java API consists of a set of predefined classes.
•    Any implementation of the Java platform is guaranteed to support
     the Java programming language, virtual machine, and API.
•    The term host environment represents the host operating system, a
     set of native libraries, and the CPU instruction set.
•    Native applications are written in native programming languages
     such as C and C++, compiled into host-specific binary code, and
     linked with native libraries.
•    We know, Native applications and native libraries are typically
     dependent on a particular host environment.
•    A C application built for one operating system, for example, typically
     does not work on other operating systems.
Java Platform do
• Java platforms are commonly deployed on top of
  a host environment.
For example, the Java Runtime Environment (JRE) is
  a Sun product that supports the Java platform on
  existing operating systems such as Solaris and
  Windows.

• The Java platform offers a set of features that
  applications can rely on independent of the
  underlying host environment.
Role of the JNI
•   The JNI is a powerful feature that allows you to take advantage of the Java
    platform, but still utilize code written in other languages. As a part of the Java
    virtual machine implementation, the JNI is a two-way interface that allows Java
    applications to invoke native code and vice versa. Figure -
JNI - Two-way interface
• As a two-way interface, the JNI can support two types of native
   code:
        - Native libraries and
        - Native applications.
- Applications call native methods in the same way that they call
   methods implemented in the Java programming language.
- An invocation interface :
   Native applications can link with a native library that implements
   the Java virtual machine, and then ..
   Use the invocation interface to execute software components
   written in the Java programming language.
For example, a web browser written in C can execute downloaded
   applets in an embedded Java virtual machine implemention.
JNI - Risks
•   It risks losing two benefits of the Java platform
    - First,
    Java applications that depend on the JNI can no longer readily run on multiple host
    environments.
    It will be necessary to recompile the part of the application written in native programming
    languages.
    - Second,
    The Java programming language is type-safe and secure, native languages such as C or C++
    are not.
    while this point may be for those OS Development which might have independent hardware
    licensing issues.

When to Use the JNI ?
A number of alternative approaches also allow Java applications to interoperate with code
    written in other languages. For example:
• A Java application may communicate with a native application through a TCP/IP connection
    or through other inter-process communication (IPC) mechanisms.
• A Java application may connect to a legacy database through the JDBCTM API.
• A Java application may take advantage of distributed object technologies such as the Java IDL
    API.
When the JNI becomes useful ?
The following scenarios:
• Targeted Java API might not support certain host-dependent
  features needed by an application.
• May want to access an existing native library and are not willing to
  pay for the overhead of copying and transmitting data across
  different processes.
• Loading a native library into the existing process hosting the
  application requires less system resources than starting a new
  process and loading the library into that process.
• If a 3D-intensive application spends most of its time in graphics
  rendering, you may find it necessary to write the core portion of a
  graphics library in assembly code to achieve maximum
  performance. Like, Games, Ex-H/W ….
• Have role on the JDK
  - The JNI was first supported in JDK release 1.1. Internally.
  - Java 'jdk' is the 'Java Development Kit' and it allows you to compile
  Java programs.
Develop JNI libraries for Android phones
The steps that you have to take to develop JNI libraries for Android phones are simple
   in principle.

They are outlined below.
• Install the JNI/NDK package from Google
• Create your Android project
• Make a JNI folder in your Android project root directory (called 'jni')
• Put your JNI sources in the 'jni' folder
• Create an 'Android.mk' file, and place it in the 'jni' folder
• Optionally create an 'Application.mk' file, and place it in the 'jni' folder
• Open a command line terminal and navigate to the root directory of your Android
   project.
• Execute 'ndk-build', (if it's in your PATH variable) or execute
   '/path/to/command/ndk-build'
• The 'ndk-build' command creates the binary for your library and puts it in the
   proper folder.
• Switch to Eclipse, Refresh the 'Project Explorer View' (F5)
• Rebuild the project
• Run your project testing your JNI library.
How to code with JNI
Android.mk files
We'll leave most of the file as it is.
• LOCAL_PATH - this line should be left as it is since your source file
  ('example.c') is in the same directory as the 'Android.mk' file.
• include $(CLEAR_VARS) - this line should be left as it is. It is
  required.
• LOCAL_MODULE - this line should be changed to match your
  module name. For this tutorial we'll change it to 'example'. This
  name should not have any spaces in it as it will be made into the
  actual library's name ('libexample.so' for us).
• LOCAL_CFLAGS - This line can be left as it is. It is for compiler flags.
• LOCAL_SRC_FILES - this line should be changed to 'example.c' since
  that's our source file.
• LOCAL_LDLIBS - leave this the same.
• include $(BUILD_SHARED_LIBRARY) - leave this the same.
How to configure a script for making a
   library and an Android.mk file
• Configure script to generate the
  – config.h and config.mak files.

  – http://code.google.com/p/awesomeguy/wiki/JNITutor
    ial#Overview
  – CV Ready + Cygwin Devel Branch + NDK set
  – YA cam recorder + ffmpeg test project for making
    ffmpeg library + Color Conversion yuv2rgb
  – halfninja-android-ffmpeg-x264-04b62f2 need ffmpeg
    library
  – Test the project + ffmpeg tutorial search

More Related Content

What's hot

Using the Android Native Development Kit (NDK)
Using the Android Native Development Kit (NDK)Using the Android Native Development Kit (NDK)
Using the Android Native Development Kit (NDK)DroidConTLV
 
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 ndk - Introduction
Android ndk  - IntroductionAndroid ndk  - Introduction
Android ndk - IntroductionRakesh Jha
 
How to implement a simple dalvik virtual machine
How to implement a simple dalvik virtual machineHow to implement a simple dalvik virtual machine
How to implement a simple dalvik virtual machineChun-Yu Wang
 
Android Developer Meetup
Android Developer MeetupAndroid Developer Meetup
Android Developer MeetupMedialets
 
Understanding the Dalvik bytecode with the Dedexer tool
Understanding the Dalvik bytecode with the Dedexer toolUnderstanding the Dalvik bytecode with the Dedexer tool
Understanding the Dalvik bytecode with the Dedexer toolGabor Paller
 
LinkedIn - Disassembling Dalvik Bytecode
LinkedIn - Disassembling Dalvik BytecodeLinkedIn - Disassembling Dalvik Bytecode
LinkedIn - Disassembling Dalvik BytecodeAlain Leon
 
Toward dynamic analysis of obfuscated android malware
Toward dynamic analysis of obfuscated android malwareToward dynamic analysis of obfuscated android malware
Toward dynamic analysis of obfuscated android malwareZongXian Shen
 
ProbeDroid - Crafting Your Own Dynamic Instrument Tool on Android for App Beh...
ProbeDroid - Crafting Your Own Dynamic Instrument Tool on Android for App Beh...ProbeDroid - Crafting Your Own Dynamic Instrument Tool on Android for App Beh...
ProbeDroid - Crafting Your Own Dynamic Instrument Tool on Android for App Beh...ZongXian Shen
 
How to reverse engineer Android applications
How to reverse engineer Android applicationsHow to reverse engineer Android applications
How to reverse engineer Android applicationshubx
 
(COSCUP 2015) A Beginner's Journey to Mozilla SpiderMonkey JS Engine
(COSCUP 2015) A Beginner's Journey to Mozilla SpiderMonkey JS Engine(COSCUP 2015) A Beginner's Journey to Mozilla SpiderMonkey JS Engine
(COSCUP 2015) A Beginner's Journey to Mozilla SpiderMonkey JS EngineZongXian Shen
 
Reverse engineering android apps
Reverse engineering android appsReverse engineering android apps
Reverse engineering android appsPranay Airan
 
Advanced Evasion Techniques by Win32/Gapz
Advanced Evasion Techniques by Win32/GapzAdvanced Evasion Techniques by Win32/Gapz
Advanced Evasion Techniques by Win32/GapzAlex Matrosov
 
OpenDaylight Developer Experience 2.0
 OpenDaylight Developer Experience 2.0 OpenDaylight Developer Experience 2.0
OpenDaylight Developer Experience 2.0Michael Vorburger
 
Android is NOT just 'Java on Linux'
Android is NOT just 'Java on Linux'Android is NOT just 'Java on Linux'
Android is NOT just 'Java on Linux'Tetsuyuki Kobayashi
 

What's hot (20)

Android ndk: Entering the native world
Android ndk: Entering the native worldAndroid ndk: Entering the native world
Android ndk: Entering the native world
 
Using the Android Native Development Kit (NDK)
Using the Android Native Development Kit (NDK)Using the Android Native Development Kit (NDK)
Using the Android Native Development Kit (NDK)
 
Android ndk
Android ndkAndroid ndk
Android ndk
 
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 ndk - Introduction
Android ndk  - IntroductionAndroid ndk  - Introduction
Android ndk - Introduction
 
How to implement a simple dalvik virtual machine
How to implement a simple dalvik virtual machineHow to implement a simple dalvik virtual machine
How to implement a simple dalvik virtual machine
 
Android Developer Meetup
Android Developer MeetupAndroid Developer Meetup
Android Developer Meetup
 
Android NDK: Entrando no Mundo Nativo
Android NDK: Entrando no Mundo NativoAndroid NDK: Entrando no Mundo Nativo
Android NDK: Entrando no Mundo Nativo
 
Understanding the Dalvik bytecode with the Dedexer tool
Understanding the Dalvik bytecode with the Dedexer toolUnderstanding the Dalvik bytecode with the Dedexer tool
Understanding the Dalvik bytecode with the Dedexer tool
 
LinkedIn - Disassembling Dalvik Bytecode
LinkedIn - Disassembling Dalvik BytecodeLinkedIn - Disassembling Dalvik Bytecode
LinkedIn - Disassembling Dalvik Bytecode
 
Toward dynamic analysis of obfuscated android malware
Toward dynamic analysis of obfuscated android malwareToward dynamic analysis of obfuscated android malware
Toward dynamic analysis of obfuscated android malware
 
ProbeDroid - Crafting Your Own Dynamic Instrument Tool on Android for App Beh...
ProbeDroid - Crafting Your Own Dynamic Instrument Tool on Android for App Beh...ProbeDroid - Crafting Your Own Dynamic Instrument Tool on Android for App Beh...
ProbeDroid - Crafting Your Own Dynamic Instrument Tool on Android for App Beh...
 
How to reverse engineer Android applications
How to reverse engineer Android applicationsHow to reverse engineer Android applications
How to reverse engineer Android applications
 
(COSCUP 2015) A Beginner's Journey to Mozilla SpiderMonkey JS Engine
(COSCUP 2015) A Beginner's Journey to Mozilla SpiderMonkey JS Engine(COSCUP 2015) A Beginner's Journey to Mozilla SpiderMonkey JS Engine
(COSCUP 2015) A Beginner's Journey to Mozilla SpiderMonkey JS Engine
 
Reverse engineering android apps
Reverse engineering android appsReverse engineering android apps
Reverse engineering android apps
 
Advanced Evasion Techniques by Win32/Gapz
Advanced Evasion Techniques by Win32/GapzAdvanced Evasion Techniques by Win32/Gapz
Advanced Evasion Techniques by Win32/Gapz
 
109842496 jni
109842496 jni109842496 jni
109842496 jni
 
OpenDaylight Developer Experience 2.0
 OpenDaylight Developer Experience 2.0 OpenDaylight Developer Experience 2.0
OpenDaylight Developer Experience 2.0
 
Android is NOT just 'Java on Linux'
Android is NOT just 'Java on Linux'Android is NOT just 'Java on Linux'
Android is NOT just 'Java on Linux'
 
ISC HPCW talks
ISC HPCW talksISC HPCW talks
ISC HPCW talks
 

Similar to Android NDK

Getting started with the NDK
Getting started with the NDKGetting started with the NDK
Getting started with the NDKKirill Kounik
 
NCDevCon 2017 - Cross Platform Mobile Apps
NCDevCon 2017 - Cross Platform Mobile AppsNCDevCon 2017 - Cross Platform Mobile Apps
NCDevCon 2017 - Cross Platform Mobile AppsJohn M. Wargo
 
Native Android for Windows Developers
Native Android for Windows DevelopersNative Android for Windows Developers
Native Android for Windows DevelopersYoss Cohen
 
International Journal of Engineering Research and Development
International Journal of Engineering Research and DevelopmentInternational Journal of Engineering Research and Development
International Journal of Engineering Research and DevelopmentIJERD Editor
 
Android Architecture design programming with java
Android Architecture design programming with javaAndroid Architecture design programming with java
Android Architecture design programming with javassuser471dfb
 
Developing for Android-Types of Android Application
Developing for Android-Types of Android ApplicationDeveloping for Android-Types of Android Application
Developing for Android-Types of Android ApplicationNandini Prabhu
 
Notes of java first unit
Notes of java first unitNotes of java first unit
Notes of java first unitgowher172236
 
Introduction to android sessions new
Introduction to android   sessions newIntroduction to android   sessions new
Introduction to android sessions newJoe Jacob
 
Alternatives to Java for Android development
Alternatives to Java for Android developmentAlternatives to Java for Android development
Alternatives to Java for Android developmentttogrul
 
Toğrul Tağıyev - Müxtəlif dillərdə Android proqramlaşdırma
Toğrul Tağıyev - Müxtəlif dillərdə Android proqramlaşdırmaToğrul Tağıyev - Müxtəlif dillərdə Android proqramlaşdırma
Toğrul Tağıyev - Müxtəlif dillərdə Android proqramlaşdırmaFarhad
 
Toğrul Tağıyev - Müxtəlif dillərdə Android proqramlaşdırma
Toğrul Tağıyev - Müxtəlif dillərdə Android proqramlaşdırmaToğrul Tağıyev - Müxtəlif dillərdə Android proqramlaşdırma
Toğrul Tağıyev - Müxtəlif dillərdə Android proqramlaşdırmaFarhad
 
iOS Application Security
iOS Application SecurityiOS Application Security
iOS Application SecurityEgor Tolstoy
 
Advance Android Application Development
Advance Android Application DevelopmentAdvance Android Application Development
Advance Android Application DevelopmentRamesh Prasad
 
Enhancing and modifying_the_core_android_os
Enhancing and modifying_the_core_android_osEnhancing and modifying_the_core_android_os
Enhancing and modifying_the_core_android_osArnav Gupta
 
Building an Ionic hybrid mobile app with TypeScript
Building an Ionic hybrid mobile app with TypeScript Building an Ionic hybrid mobile app with TypeScript
Building an Ionic hybrid mobile app with TypeScript Serge van den Oever
 

Similar to Android NDK (20)

Getting started with the NDK
Getting started with the NDKGetting started with the NDK
Getting started with the NDK
 
NCDevCon 2017 - Cross Platform Mobile Apps
NCDevCon 2017 - Cross Platform Mobile AppsNCDevCon 2017 - Cross Platform Mobile Apps
NCDevCon 2017 - Cross Platform Mobile Apps
 
Native Android for Windows Developers
Native Android for Windows DevelopersNative Android for Windows Developers
Native Android for Windows Developers
 
Getting Native with NDK
Getting Native with NDKGetting Native with NDK
Getting Native with NDK
 
International Journal of Engineering Research and Development
International Journal of Engineering Research and DevelopmentInternational Journal of Engineering Research and Development
International Journal of Engineering Research and Development
 
Android Architecture design programming with java
Android Architecture design programming with javaAndroid Architecture design programming with java
Android Architecture design programming with java
 
Using the NDK and Renderscript
Using the NDK and RenderscriptUsing the NDK and Renderscript
Using the NDK and Renderscript
 
Developing for Android-Types of Android Application
Developing for Android-Types of Android ApplicationDeveloping for Android-Types of Android Application
Developing for Android-Types of Android Application
 
Notes of java first unit
Notes of java first unitNotes of java first unit
Notes of java first unit
 
JAVA First Day
JAVA First DayJAVA First Day
JAVA First Day
 
Apache cordova
Apache cordovaApache cordova
Apache cordova
 
Introduction to android
Introduction to androidIntroduction to android
Introduction to android
 
Introduction to android sessions new
Introduction to android   sessions newIntroduction to android   sessions new
Introduction to android sessions new
 
Alternatives to Java for Android development
Alternatives to Java for Android developmentAlternatives to Java for Android development
Alternatives to Java for Android development
 
Toğrul Tağıyev - Müxtəlif dillərdə Android proqramlaşdırma
Toğrul Tağıyev - Müxtəlif dillərdə Android proqramlaşdırmaToğrul Tağıyev - Müxtəlif dillərdə Android proqramlaşdırma
Toğrul Tağıyev - Müxtəlif dillərdə Android proqramlaşdırma
 
Toğrul Tağıyev - Müxtəlif dillərdə Android proqramlaşdırma
Toğrul Tağıyev - Müxtəlif dillərdə Android proqramlaşdırmaToğrul Tağıyev - Müxtəlif dillərdə Android proqramlaşdırma
Toğrul Tağıyev - Müxtəlif dillərdə Android proqramlaşdırma
 
iOS Application Security
iOS Application SecurityiOS Application Security
iOS Application Security
 
Advance Android Application Development
Advance Android Application DevelopmentAdvance Android Application Development
Advance Android Application Development
 
Enhancing and modifying_the_core_android_os
Enhancing and modifying_the_core_android_osEnhancing and modifying_the_core_android_os
Enhancing and modifying_the_core_android_os
 
Building an Ionic hybrid mobile app with TypeScript
Building an Ionic hybrid mobile app with TypeScript Building an Ionic hybrid mobile app with TypeScript
Building an Ionic hybrid mobile app with TypeScript
 

More from Sentinel Solutions Ltd

More from Sentinel Solutions Ltd (6)

Aws amazon ec2
Aws amazon ec2Aws amazon ec2
Aws amazon ec2
 
Android ndk
Android ndkAndroid ndk
Android ndk
 
GWT Widgets
GWT WidgetsGWT Widgets
GWT Widgets
 
Programming Style - Duck Type
Programming Style - Duck TypeProgramming Style - Duck Type
Programming Style - Duck Type
 
Programming style - duck type
Programming style - duck typeProgramming style - duck type
Programming style - duck type
 
Titanium Studio [Updated - 18/12/2011]
Titanium Studio [Updated - 18/12/2011]Titanium Studio [Updated - 18/12/2011]
Titanium Studio [Updated - 18/12/2011]
 

Recently uploaded

New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Visualising and forecasting stocks using Dash
Visualising and forecasting stocks using DashVisualising and forecasting stocks using Dash
Visualising and forecasting stocks using Dashnarutouzumaki53779
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 

Recently uploaded (20)

New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Visualising and forecasting stocks using Dash
Visualising and forecasting stocks using DashVisualising and forecasting stocks using Dash
Visualising and forecasting stocks using Dash
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 

Android NDK

  • 1. Android NDK by Abdun Nur Tomal
  • 2. Step 1: Installing C/C++ Support on Eclipse • Choose Eclipse Galileo to update • Let the item preference tree load, and check Eclipse C/C++ Development Tools in the Programming Languages branch • From - http://www.eclipse.org/cdt/downloads.php
  • 3. Step : Installing Cygwin • Android is Linux based, and thus it is no surprise that when build native code for it, need some Unix tools. • On Windows, NDK supports Cygwin 1.7.x and above. • It’s just a set of software that emulates Unix environment on Windows • get Cygwin, go to cygwin.com
  • 4. “Install or update Cygwin now!” • Cygwin’s setup.exe will download and run • Choose Install from Internet, then click Next, then choose the installation directory (be sure to choose a directory path that contains no spaces in it) like – C:/cygwin
  • 5. Base packages installed, Need the development packages • After choose the mirror and click Next, Cygwin will download and present to you the list of available packages: • By default, only the base packages are installed. We, however, need the development packages. Rather than picking the packages we think we need • We install the entire Devel branch. • Click (once) on the word “Default” next to the root Devel node and wait few seconds while the setup hangs. • When it is back, you will see that “Default” changes to “Install” for the Devel node, just like on the screenshot above.
  • 6.
  • 7. • Click it once, let the Cygwin console start up and initialize: • should see the same response that tells us that GNU Make is present in our Unix environment that is emulated by Cygwin.
  • 8. Android rich programming environment, While the Android SDK provides a very NDK The Android NDK broadens the horizons and can speed up the delivery of desired functionality by bringing in existing source code, some of which may be proprietary and some of which may be open source code. The NDK includes all the components necessary to incorporate functionality written in C into an Android application. As of r5 of the NDK application, • authors can write a significant portion of an application directly in C, including user interface and event-handling capability. As of r4b of the NDK application, • The features enabling the image handling functionality demonstrated here were introduced with the r4b version of the NDK.
  • 9. NDK Build Two common uses of the NDK are - to increase application performance and Application performance improvements are available when carefully crafted functions written in C are leveraged to perform memory-based or computationally intensive operations. - to leverage existing C code by porting it to Android. The NDK is to port an existing body of C code written for another platform, such as Linux. The NDK contains a compiler and build scripts, allowing - to focus on the C source files and - leave the build magic to the NDK installation. The NDK build process is easily incorporated into the Eclipse development environment,
  • 10. Step 3: Installing the Android NDK • Android NDK itself and place it on our filesystem. • Can get NDK from the official Android site • Be sure that there are no spaces in the path. • Extract it to C:, • so the path is C:android-ndk-r6.
  • 11. Step 4: Making a Basic NDK App • The general idea of using NDK in apps is to put your native pieces of code into libraries that you can then consume from the Java code. • Use the New Android Project wizard • However, an important thing to check , it is spaces in the path again.
  • 12. In the project named ... Make a folder called jni in the root of the project (right- click the project node, New – Folder). • Create a file called Android.mk (New – File) within that folder with the following contents: LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) # Here we give our module name and source file(s) LOCAL_MODULE := ndk_test LOCAL_SRC_FILES := ndk_test.c include $(BUILD_SHARED_LIBRARY) The Android.mk file is important for the NDK build process to recognize your NDK modules. In our case we named our module ndk_test and told the build tool that it consists of one source file named ndk_test.c. Let’s create it in the same jni folder: #include <string.h> #include <jni.h> jstring Java_com_mindtherobot_samples_ndkfoo_NdkFooActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) { return (*env)->NewStringUTF(env, "Hello from native code!"); }
  • 13. • the C function – it matches the Java class name • create a binary library from the C source that we wrote, • use a combination of Cygwin and Android NDK tools. • Launch the Cygwin console and use the cd command to go directly to the folder where your project is. • the command line is: /cygdrive/c/android-ndk-r4/ndk-build
  • 14.
  • 15. • a successful run of the ndk-build tool will create an .so file in a new folder called libs • The .so file is the binary library that will be included into the application .apk package and will be available for the Java code of the app to link to.
  • 16. Ndk_testActivity class to use the NDK code: package com.mindtherobot.samples.ndkfoo; import android.app.Activity; import android.app.AlertDialog; import android.os.Bundle; public class Ndk_testActivity extends Activity { // load the library - name matches jni/Android.mk static { System.loadLibrary("ndkfoo"); } // declare the native code function - must match ndk_test.c private native String invokeNativeFunction(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // this is where we call the native code String hello = invokeNativeFunction(); new AlertDialog.Builder(this).setMessage(hello).show(); } }
  • 17. How to Build FFmpeg for Android • Download Android NDK r5b To download android ndk revision 5b and rev.6 use the following command: http://dl.google.com/android/ndk/android-ndk-r5b-linux-x86.tar.bz2 http://dl.google.com/android/ndk/android-ndk-r6-linux-x86.tar.bz2 • Download Source Code for Ffmpeg - http://ffmpeg.org/download.html https://github.com/FFmpeg/FFmpeg - The FFmpeg 0.8 “Love” need to downloaded. http://ffmpeg.zeranoe.com/builds/ • Build FFmpeg (The script is based on RockPlayer build script) RockPlayer open source component: http://rockplayer.freecoder.org/tech.html http://roman10.net/src/build_android_r5b.txt http://roman10.net/src/build_android_r6.txt • The Output of the Build
  • 18. All about JNI • Java Native Interface (JNI) • The JNI is a part of the Java platform, programmers can address interoperability issues once, and expect their solution to work with all implementations of the Java platform. “Applications written in the Java programming language as well as in native (C, C++, etc.) programming languages.”
  • 19. Activity Seems clarify the exact scope of the programming environments for these languages. • Java applications are written in the Java programming language, and compiled into a machine-independent binary class format. • The Java API consists of a set of predefined classes. • Any implementation of the Java platform is guaranteed to support the Java programming language, virtual machine, and API. • The term host environment represents the host operating system, a set of native libraries, and the CPU instruction set. • Native applications are written in native programming languages such as C and C++, compiled into host-specific binary code, and linked with native libraries. • We know, Native applications and native libraries are typically dependent on a particular host environment. • A C application built for one operating system, for example, typically does not work on other operating systems.
  • 20. Java Platform do • Java platforms are commonly deployed on top of a host environment. For example, the Java Runtime Environment (JRE) is a Sun product that supports the Java platform on existing operating systems such as Solaris and Windows. • The Java platform offers a set of features that applications can rely on independent of the underlying host environment.
  • 21. Role of the JNI • The JNI is a powerful feature that allows you to take advantage of the Java platform, but still utilize code written in other languages. As a part of the Java virtual machine implementation, the JNI is a two-way interface that allows Java applications to invoke native code and vice versa. Figure -
  • 22. JNI - Two-way interface • As a two-way interface, the JNI can support two types of native code: - Native libraries and - Native applications. - Applications call native methods in the same way that they call methods implemented in the Java programming language. - An invocation interface : Native applications can link with a native library that implements the Java virtual machine, and then .. Use the invocation interface to execute software components written in the Java programming language. For example, a web browser written in C can execute downloaded applets in an embedded Java virtual machine implemention.
  • 23. JNI - Risks • It risks losing two benefits of the Java platform - First, Java applications that depend on the JNI can no longer readily run on multiple host environments. It will be necessary to recompile the part of the application written in native programming languages. - Second, The Java programming language is type-safe and secure, native languages such as C or C++ are not. while this point may be for those OS Development which might have independent hardware licensing issues. When to Use the JNI ? A number of alternative approaches also allow Java applications to interoperate with code written in other languages. For example: • A Java application may communicate with a native application through a TCP/IP connection or through other inter-process communication (IPC) mechanisms. • A Java application may connect to a legacy database through the JDBCTM API. • A Java application may take advantage of distributed object technologies such as the Java IDL API.
  • 24. When the JNI becomes useful ? The following scenarios: • Targeted Java API might not support certain host-dependent features needed by an application. • May want to access an existing native library and are not willing to pay for the overhead of copying and transmitting data across different processes. • Loading a native library into the existing process hosting the application requires less system resources than starting a new process and loading the library into that process. • If a 3D-intensive application spends most of its time in graphics rendering, you may find it necessary to write the core portion of a graphics library in assembly code to achieve maximum performance. Like, Games, Ex-H/W …. • Have role on the JDK - The JNI was first supported in JDK release 1.1. Internally. - Java 'jdk' is the 'Java Development Kit' and it allows you to compile Java programs.
  • 25. Develop JNI libraries for Android phones The steps that you have to take to develop JNI libraries for Android phones are simple in principle. They are outlined below. • Install the JNI/NDK package from Google • Create your Android project • Make a JNI folder in your Android project root directory (called 'jni') • Put your JNI sources in the 'jni' folder • Create an 'Android.mk' file, and place it in the 'jni' folder • Optionally create an 'Application.mk' file, and place it in the 'jni' folder • Open a command line terminal and navigate to the root directory of your Android project. • Execute 'ndk-build', (if it's in your PATH variable) or execute '/path/to/command/ndk-build' • The 'ndk-build' command creates the binary for your library and puts it in the proper folder. • Switch to Eclipse, Refresh the 'Project Explorer View' (F5) • Rebuild the project • Run your project testing your JNI library.
  • 26. How to code with JNI
  • 27. Android.mk files We'll leave most of the file as it is. • LOCAL_PATH - this line should be left as it is since your source file ('example.c') is in the same directory as the 'Android.mk' file. • include $(CLEAR_VARS) - this line should be left as it is. It is required. • LOCAL_MODULE - this line should be changed to match your module name. For this tutorial we'll change it to 'example'. This name should not have any spaces in it as it will be made into the actual library's name ('libexample.so' for us). • LOCAL_CFLAGS - This line can be left as it is. It is for compiler flags. • LOCAL_SRC_FILES - this line should be changed to 'example.c' since that's our source file. • LOCAL_LDLIBS - leave this the same. • include $(BUILD_SHARED_LIBRARY) - leave this the same.
  • 28. How to configure a script for making a library and an Android.mk file • Configure script to generate the – config.h and config.mak files. – http://code.google.com/p/awesomeguy/wiki/JNITutor ial#Overview – CV Ready + Cygwin Devel Branch + NDK set – YA cam recorder + ffmpeg test project for making ffmpeg library + Color Conversion yuv2rgb – halfninja-android-ffmpeg-x264-04b62f2 need ffmpeg library – Test the project + ffmpeg tutorial search