NDK Work notes (test environment)

Source: Internet
Author: User

relationship between 1.JNI and NDK:the role of 1.1:jni

There will be close communication between the Java layer and the C layer. At this point, the JNI (Java Native Interface) acts as an interface between the two sides.

With the JNI interface, you can move the function implementation part of the Java layer class to the local C function.

1.2: Reasons to use JNI

The reason: Java code execution is slow, and C code execution speed. And more commercially valuable,
There are many existing C (or C + +) program modules that can easily connect Java programs to C modules through the JNI interface.

However, Java code can cross the platform, and C code is closely related to the local platform device, so it is called a local (Native) program code.

Therefore, in the design of the entire application software, often from the execution speed, cross-platform capabilities and other aspects of evaluation, in order to achieve the most desirable planning.



2. How to install the NDK development tools:2.1. Domestic website Image:

Http://wear.techbrood.com/tools/sdk/ndk/index.html

2.2. Features and background introduction

As early as 2014 in June, Mountain View City announced that the upcoming Android L will be supported by 64-bit processors.

A few weeks later, version 10 of the revised NDK (Native development Kit, native Development Kit) was released, supporting three 64-bit architectures of chips,

namely arm64-v8a, x86_64 and Mips64. I believe that developers know that when arm64-v8a, x86_64 and MIPS64 three kinds of 64-bit chips are supported,

In fact, you can think of this, with the support of these three new ABI application binary interfaces,

Even all 64-bit cores will be able to run the new version of Android.

as the official release of Android L became more and more close, Google recently updated the NDK to version 10b,

The new NDK r10b adds a new Intel 64-bit chip to the emulator emulator image,

From this point on, application developers can prepare or simulate applications for Android devices that use Intel 64-bit chips.

As Intel's handheld mobile platform chips have been tepid, the company has been aggressively laying out their architecture for Android developers.

The launch of the new ndk in Mountain View clearly helped Intel's own architectural layout. Thanks to the power of Intel chip performance,

Developers may like or have some interest in developing some good applications on this platform,

Especially the game. And, on the other hand, it's also a sign that Intel and Google are fostering stronger and closer partnerships.

As with this relationship, we can expect that in the near future there will be more Android devices using the Intel Atom/bay Trail chip.

Note that the NDK serves only local applications, not standard Android SDK services built around Java.

If you're a developer, and you're always looking to run your application in a 64-bit environment,

Or if you need to update to the latest version of NDK,NDK r10b is your coup

You can go to the source of the Android NDK Developer Center to download, or you can start the download directly from the link we provide.


2.3 Installation environment dependent.

The default compiler for ANDROID-NDK64-R10B64-bit ABI (Application Binary Interface) is GCC 4.9,

Minimum supported API levels (API level) is the update level for Android L (Android 4.4 's API is 19 and the new version should be 20).

In addition, the new CPU feature library adds the ARM V8 architecture machine Core support,

To fix an issue where an existing library might not be able to detect the ARM V8 NEON decoding chip. Other specific updates and see official release notes.


2.4: Installation Environment Introduction:

ubuntu14.04 64bit,

Decompression: TAR-XVF android-ndk64-r10b-linux-x86_64.tar.bz2
Modify the ~/.BASHRC file: Add the following statement at the end of the file:

Export path=/home/zshh/home/android-ndk-r10b: $PATH

2.5 Verify that the NDK is installed successfully.

Into the/android-ndk-r10b/samples/hello-jni/jni$ inside.

android-ndk-r10b/samples/hello-jni/jni$ Ndk-buildIf you generate the following information, indicate the
[ARM64-V8A] Compile:hello-jni <= HELLO-JNI.C
[ARM64-V8A] Sharedlibrary:libhello-jni.so
[ARM64-V8A] install:libhello-jni.so = libs/arm64-v8a/libhello-jni.so
[x86_64] Compile:hello-jni <= HELLO-JNI.C
[x86_64] Sharedlibrary:libhello-jni.so
[x86_64] install:libhello-jni.so = libs/x86_64/libhello-jni.so
[MIPS64] Compile:hello-jni <= HELLO-JNI.C
[MIPS64] Sharedlibrary:libhello-jni.so
[MIPS64] install:libhello-jni.so = libs/mips64/libhello-jni.so
[ARMEABI-V7A] Compile Thumb:hello-jni <= HELLO-JNI.C
[ARMEABI-V7A] Sharedlibrary:libhello-jni.so
[ARMEABI-V7A] install:libhello-jni.so = libs/armeabi-v7a/libhello-jni.so
[Armeabi] Compile Thumb:hello-jni <= HELLO-JNI.C
[Armeabi] Sharedlibrary:libhello-jni.so
[Armeabi] install:libhello-jni.so = libs/armeabi/libhello-jni.so
[x86] Compile:hello-jni <= HELLO-JNI.C
[x86] Sharedlibrary:libhello-jni.so
[x86] install:libhello-jni.so = libs/x86/libhello-jni.so
[MIPS] Compile:hello-jni <= HELLO-JNI.C
[MIPS] Sharedlibrary:libhello-jni.so
[MIPS] install:libhello-jni.so = libs/mips/libhello-jni.so

2.6 Generating specific platform-related code

The information above outputs the dynamic library files that are required for each platform.

If you need to modify it, you may not need to generate so much in the real world. What we can need is just one of them.

such as x86_64, or armeabi-v7a, then I need to modify the file

View the files under the file.

[Email protected]:~/downloads/android-ndk-r10b/samples/hello-jni/jni$ ls

Android.mk application.mk HELLO-JNI.C

We need to open the Application.mk file.

[Email protected]:~/downloads/android-ndk-r10b/samples/hello-jni/jni$ vim application.mk

Change App_abi: = All to App_abi: = armeabi-v7a

This generates only the code in the ARMEABI-V7A schema.


3. How to use the NDK
3.1. Create a test project. Create a project named: Hellondk that contains the files Hellondk.java and Mainactivity.java programs. where Hellondk.java

/***hellondk.java*****/

Package Com.octopus.com;public class Hellondk {        static{        system.loadlibrary ("Hellondk");    }    Public native String SayHello ();}

/****mainactivity*****/
Package Com.octopus.com;import Android.os.bundle;import Android.app.activity;import Android.view.menu;public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); HELLONDK obj  = new Hellondk (); String str = Obj.sayhello (); Settitle ("[" +str+ "]");} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}

3.2. Writing C module Code

Generate C header files using the Javah tool

[Email protected]:~/work/android/workspace/ndk/jni$ javah-classpath. /bin/classes COM.OCTOPUS.COM.HELLONDK

1.jni Catalog Description: We need to re-catalog the JNI in Eclipse Project and how to switch to the directory using the CD command.

2. Use the Javah-classpath command to locate the directory where our Hellondk.class files are located and generate the corresponding header files.

    

3. When execution completes Javah-classpath. /bin/classes after the COM.OCTOPUS.COM.HELLONDK command. Refresh this NDK project,

The com_octopus_com_hellondk.h is generated under this directory.

    

/* Don't EDIT this file-it are machine generated */#include <jni.h>/* Header for class COM_OCTOPUS_COM_HELLONDK */ #ifndef _included_com_octopus_com_hellondk#define _included_com_octopus_com_hellondk#ifdef __cplusplusextern "C" {# endif/* * Class:     com_octopus_com_hellondk * Method:    SayHello * Signature: () ljava/lang/string; */jniexport Jstring jnicall Java_com_octopus_com_hellondk_sayhello  (jnienv *, jobject); #ifdef __cplusplus} #endif #endif
4. The header file must contain the #include<jni.h> header file.

5. The function of the comment section is as follows: class: Is the completion of the package path and class name composition. That's the way we/com/octopus/com/hellondk this class.

But here is an underscore separating: COM_OCTOPUS_COM_HELLONDK

6. Method: Methods name. SayHello

7. Method signature: () ljava/lang/string; From this we can see that the method has no parameters. The type of the return value is a string type.

(********* Note: This section involves the specification of JNI, which we'll be introducing later.) )

8. Implementing COM_OCTOPUS_COM_HELLONDK.C under the JNI directory

#include <jni.h>/* * Class:     com_octopus_com_hellondk * Method:    SayHello * Signature: () ljava/lang/string ; */jniexport jstring jnicall Java_com_octopus_com_hellondk_sayhello  (jnienv *env, Jobject obj) {return (*env), Newstringutf (env, "Hello, Ndk ... \ n");
9. Compile and build the. so file.

Write the Android.mk file as follows:

Local_path: = $ (call My-dir) include $ (clear_vars) local_module    : = hellondklocal_src_files: = Com_octopus_com_ Hellondk.cinclude $ (build_shared_library)


10. Deployment test, Effect:






NDK Work notes (test environment)

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.