Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java JNI vs. Android NDK

Could someone explain how the Java JNI and the Android NDK differ, how they are the same and how they fit together? I haven't found anything that gives a good explanation of the differences between the two and I'm a little confused.

Thanks!

like image 685
Z. Adam Avatar asked Mar 26 '16 04:03

Z. Adam


People also ask

Is NDK faster than Java?

the native version will usually be much faster.

Is JNI an Android?

JNI is the Java Native Interface. It defines a way for the bytecode that Android compiles from managed code (written in the Java or Kotlin programming languages) to interact with native code (written in C/C++).

Is JNI fast?

When talking about JNI, there are two directions: java calling C++, and C++ calling java. Java calling C++ (or C) via the "native" keyword is very fast, around 50 clock cycles. However, C++ calling Java is somewhat slow.

What is Android NDK used for?

The Native Development Kit (NDK) is a set of tools that allows you to use C and C++ code with Android, and provides platform libraries you can use to manage native activities and access physical device components, such as sensors and touch input.


2 Answers

JNI is just the way that Java handles calling into native/C++ code, and calling back into Java from there. It has nothing to say about Android - it is a Java language feature.

The Android NDK is a way to write Android applications using code called by JNI. It's specific to Android and gives native code access to Android APIs at that level.

like image 179
Doug Stevenson Avatar answered Sep 21 '22 14:09

Doug Stevenson


Android NDK(Native Development Kit) is basically a toolchain to reuse code written in C/C++(native code). It compiles the native code into native library. NDK is similar to a Android SDK, with primary difference of using SDK only for java codes. NDK is used in application developed for multiple platforms like iOS, Windows. Apps like Whatsapp, Instagram were developed using NDK.

Java code use JNI(Java Native Interface)to call functions from native library, like accessing the objects, methods, etc. Also the native code can access the java environment.

like image 35
Sathiya Avatar answered Sep 21 '22 14:09

Sathiya