NDK Introduction
-
Upload
rahul-tripathi -
Category
Mobile
-
view
109 -
download
3
Transcript of NDK Introduction
![Page 1: NDK Introduction](https://reader034.fdocuments.net/reader034/viewer/2022042602/55a685801a28abe7088b458b/html5/thumbnails/1.jpg)
Android NDK Intoduction
Rahul Tripathi
![Page 2: NDK Introduction](https://reader034.fdocuments.net/reader034/viewer/2022042602/55a685801a28abe7088b458b/html5/thumbnails/2.jpg)
Agenda
The Android Native Development Kit (NDK)
NDK Info
What , Why , Why not ?
Requirement Tools & Installation of NDK
Developing an ADD application that uses the NDK
Final Output
![Page 3: NDK Introduction](https://reader034.fdocuments.net/reader034/viewer/2022042602/55a685801a28abe7088b458b/html5/thumbnails/3.jpg)
NDK info Android Native Development Kit (Android NDK)
Developer(s) - Google
Initial release - June 2009; 5 years ago
Stable release -
10d / December 2014; 2 months ago
Written in- C and C++
Operating system- Cross-platform
Available in- English
Type- IDE, SDK
Website-
developer.android.com/tools/sdk/ndk/index.ht
ml
![Page 4: NDK Introduction](https://reader034.fdocuments.net/reader034/viewer/2022042602/55a685801a28abe7088b458b/html5/thumbnails/4.jpg)
Native Development Kit What , Why ,Why Not?
The NDK is a toolset that allows you to implement parts of your app using native-code languages such as C and C++.
What is it?
Build scripts/toolkit to incorporate native code in Android * apps via the Java NativeInterface (JNI)
Why use it?
Performancee.g., complex algorithms, multimedia applications, games
Why not use it?Performance improvement isn’t alwaysguaranteed, in contrary to the addedcomplexity
![Page 5: NDK Introduction](https://reader034.fdocuments.net/reader034/viewer/2022042602/55a685801a28abe7088b458b/html5/thumbnails/5.jpg)
NDK Build Process
1. 3.
2.
Dalvik Application
Android NDK Application
![Page 6: NDK Introduction](https://reader034.fdocuments.net/reader034/viewer/2022042602/55a685801a28abe7088b458b/html5/thumbnails/6.jpg)
What is JNI ?
Android is put together of about equal part Java and C.
we need an easy way to bridge between these two totally
different worlds.
Java offers Java Native Interface (JNI) as a framework
connecting the world of Java to the native code.
![Page 7: NDK Introduction](https://reader034.fdocuments.net/reader034/viewer/2022042602/55a685801a28abe7088b458b/html5/thumbnails/7.jpg)
NDK - System and Software Requirements
The Android SDK
A complete Android SDK installation (including all dependencies) is required.
Android 1.5 SDK or later version is required.
Supported operating systems
Windows XP (32-bit) or Vista (32- or 64-bit)
Mac OS X 10.4.8 or later (x86 only)
Linux (32 or 64-bit; Ubuntu 8.04, or other Linux distributions)
Required development tools
For all development platforms, GNU Make 3.81 or later is required. Earlier versions of GNU Make might work but have not been tested.
A recent version of awk (either GNU Awk or Nawk) is also required.
For Windows, Cygwin 1.7 or higher is required. The NDK will not work with Cygwin 1.5 installations.
![Page 8: NDK Introduction](https://reader034.fdocuments.net/reader034/viewer/2022042602/55a685801a28abe7088b458b/html5/thumbnails/8.jpg)
How to Install NDK
On Linux and Mac OS X (Darwin):
Download the appropriate package from this page.
Open a terminal window.
Go to the directory to which you downloaded the package.
Run chmod a+x on the downloaded package.
Execute the package. For example:
ndk$ chmod a+x android-ndk-r10c-darwin-x86_64.binndk$ ./android-ndk-r10c-darwin-x86_64.bin
The folder containing the NDK extracts itself.
Note that you can also use a program like 7z to extract the package.
On Windows:
Download the appropriate package from this page.
Navigate to the folder to which you downloaded the package.
Double-click the downloaded file. The folder containing the NDK extracts itself.
![Page 9: NDK Introduction](https://reader034.fdocuments.net/reader034/viewer/2022042602/55a685801a28abe7088b458b/html5/thumbnails/9.jpg)
Configure NDKNow Open Eclipse and Configure NDK .
Go to window Option on the top.
Click Preference :
In Android ----> Select NDK ----> Give the path of your
NDK Which you have Extracted by Terminal .
(Note : Please Don't Make any Space in Contains Path Folder)
You have to download For NDK ..
Click Help : Install New Software ----> Select Release / Juno Link ..
Now Select
C/C++ GCC Cross Compiler Support
C/C++ GDB Hardware Debugging
Now Finish ....
![Page 10: NDK Introduction](https://reader034.fdocuments.net/reader034/viewer/2022042602/55a685801a28abe7088b458b/html5/thumbnails/10.jpg)
Now Start With Project
Create a Android Application Project -->
Right Click on project -->
Select Android tool -->
Add Native Support -->
![Page 11: NDK Introduction](https://reader034.fdocuments.net/reader034/viewer/2022042602/55a685801a28abe7088b458b/html5/thumbnails/11.jpg)
NDK Project Explorer in Eclipse
Jni FolderC/C++ File
Android.mk file
Application.mk file
After do that, your project has a folder called “jni”. Inside this folder, you have the
Android.mk file, which goes your library configuration for the compiler. And there is a
another file with .cpp (C++) extension, this is where your native code goes.You have to
create A file in side jni folder Application.mk which describe which native 'modules' (i.e.
static/shared libraries) are needed by your application.
![Page 12: NDK Introduction](https://reader034.fdocuments.net/reader034/viewer/2022042602/55a685801a28abe7088b458b/html5/thumbnails/12.jpg)
Now Here is .Java , .MK
![Page 13: NDK Introduction](https://reader034.fdocuments.net/reader034/viewer/2022042602/55a685801a28abe7088b458b/html5/thumbnails/13.jpg)
Android.MK & Application.MK
Android.mk :- sub-makefile that describes the C++
static libraries and *.so files you want to build.
Application.mk :- contains settings that apply to
all the C/C++ code (processor type, API level,
standard library, project-wide compiler options)
![Page 14: NDK Introduction](https://reader034.fdocuments.net/reader034/viewer/2022042602/55a685801a28abe7088b458b/html5/thumbnails/14.jpg)
Calling Native From Java
• Create an activity .JAVA .
• Write A native method in side that Activity .
• JNI program uses a static initializer to load a shared library
("myjni.dll" in Windows or "libmyjni.so" in Ubuntu). It
declares a native method.
![Page 15: NDK Introduction](https://reader034.fdocuments.net/reader034/viewer/2022042602/55a685801a28abe7088b458b/html5/thumbnails/15.jpg)
Create A JNI .h Header File Using "Javah"Generating C/C++ Header File using "javah" Utility
Now We Have to Create Header File (.h) under JNI Folder .
Using Terminal/Cygwin
Go to your bin directory using cd command .. On Terminal/Cygwin .
No.....
You Can Create Manual Javah Builder ...
![Page 16: NDK Introduction](https://reader034.fdocuments.net/reader034/viewer/2022042602/55a685801a28abe7088b458b/html5/thumbnails/16.jpg)
Header file for C/C++ Programs
Run javah utility on the class file to create a header file for C/C++ programs.
![Page 17: NDK Introduction](https://reader034.fdocuments.net/reader034/viewer/2022042602/55a685801a28abe7088b458b/html5/thumbnails/17.jpg)
C/C++ Implementation Write Your C/C++ Code
JNI environment interface function
![Page 18: NDK Introduction](https://reader034.fdocuments.net/reader034/viewer/2022042602/55a685801a28abe7088b458b/html5/thumbnails/18.jpg)
What is Javah ?
Java is bundled with JDK .it Has command to generate c header file from a
java class. this file provide connective glue that allow your java and c code to
intract.
How to find your Javah from Terminal :
Which Javah
![Page 19: NDK Introduction](https://reader034.fdocuments.net/reader034/viewer/2022042602/55a685801a28abe7088b458b/html5/thumbnails/19.jpg)
Setting up Automatic NDK Builds in
Eclipse Start by right clicking on your android project and select Properties. In the resulting
dialog, choose the Builders entry in the list to the left and press the New... button:
![Page 20: NDK Introduction](https://reader034.fdocuments.net/reader034/viewer/2022042602/55a685801a28abe7088b458b/html5/thumbnails/20.jpg)
Here We Go .......... NDK Build
![Page 21: NDK Introduction](https://reader034.fdocuments.net/reader034/viewer/2022042602/55a685801a28abe7088b458b/html5/thumbnails/21.jpg)
NDK Build Finish ....
![Page 22: NDK Introduction](https://reader034.fdocuments.net/reader034/viewer/2022042602/55a685801a28abe7088b458b/html5/thumbnails/22.jpg)
Generated
Shared Library .So File
1. The Android OS loads the Java code and starts
executing it
2. The Java code calls System.loadLibrary() to load
a native library.
3. The Java code starts calling functions from the
native code.
![Page 23: NDK Introduction](https://reader034.fdocuments.net/reader034/viewer/2022042602/55a685801a28abe7088b458b/html5/thumbnails/23.jpg)
Final Output
![Page 24: NDK Introduction](https://reader034.fdocuments.net/reader034/viewer/2022042602/55a685801a28abe7088b458b/html5/thumbnails/24.jpg)
Thank You