A brief introduction to Android SDK, NDK, and JNI

Source: Internet
Author: User
Tags win32

Android SDK:

SDK (software Development Kit) software Development Kit. A collection of development tools that are used by software development engineers to build applications for specific software packages, software frameworks, hardware platforms, and operating systems. So. The Android SDk refers to Android's proprietary software development Kit

JNI:

The Java Native Interface (JNI) standard is part of the Java platform that allows Java code to interact with code written in other languages. JNI is a local programming interface that enables Java code that runs within a Java Virtual machine (VM) to interoperate with applications and libraries written in other programming languages such as C, C + +, and assembly language

Of course, the following procedures are generally required:

1 Write Java program: Here take HelloWorld for example. In order to implement the call C function printf in Java code.

Code 1:

Class HelloWorld {

public native void Testhelloworld ();

static {

System.loadlibrary ("Hello");

}

public static void Main (string[] args) {

New HelloWorld (). Testhelloworld ();

}

}

Declaration Native method: If you want to use a method as a local method, then you must declare that the change method is native and cannot be implemented.

Load Dynamic Library: system.loadlibrary ("Hello");

This is typically loaded with a static block. Also note that the system.loadlibrary () parameter "Hello" is the name of the dynamic library.

2) compiling

Javac Helloworld.java

3 Generate a header file with an extension of H Javah?

Jniexport void Jnicall Java_helloworld_testhelloworld (jnienv *, jobject);

This h file corresponds to our interface in Java, where we declare a java_helloworld_testhelloworld (JNIENV *, Jobject) method, and then implement this method in our local method, which means that we are writing c/c+ + The method name used in the program must be the same as here.

4 Write Local method implementation and the same method name declared in the header file generated by the Javah command

Code 2:

#include "jni.h"

#include "HelloWorld.h"

#include Other headers

Jniexport void Jnicall Java_helloworld_testhelloworld (jnienv *env, Jobject obj)

{

printf ("Hello world!/n");

Return

}

Note the 1th line in code 2, which requires that the jni.h (which can be found under the%java_home%/include folder) be introduced, because the types of jnienv, Jobject, and so on in the program are defined in the header file. , and the HelloWorld.h header file needs to be introduced in line 2nd. And then save it for helloworldimpl.c OK.

5 Generating dynamic libraries

Here, for example in Windows, you need to generate a DLL file. Under Save the HELLOWORLDIMPL.C folder, use the VC compiler CL. Cl-i%java_home%/include-i%java_home%/include/win32-ld Helloworldimp.c-fehello.dll Note: The generated DLL file name is configured after the option-fe, Here is hello, because the name we used when we loadlibary in the Helloworld.java file is hello.

Additional-i%java_home%/include-i%java_home%/include/win32 parameters need to be added because the jni.h file was introduced when the local method was written in step fourth.

NDK:

NDK Full Name: Native Development Kit.
NDK is a collection of tools.
* NDK provides a range of tools to help developers quickly develop a C (or C + +) dynamic library and automatically package so and Java applications as APK. The tools that help developers are huge.
* NDK integrates the cross compiler, and provides the corresponding MK file isolation CPU, platform, ABI and other differences, developers only need to simply modify the Mk file (indicating "which files need to compile", "compile feature requirements", etc.), you can create so.
* NDK can automatically package so and Java applications, greatly reducing the developer's packaging work.

In fact:

NDK is a tool that can quickly and easily develop. so files. The JNI process is complex, generating. So requires a lot of operations, and NDK is simplifying the process.

An inappropriate analogy: JNI needs to buy their own vegetables, wash vegetables, stir fry, cooking, very troublesome; NDK is a house rush, a phone call.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.