Android JNI Programming (vii)--writing the first JNI program using Androidstudio

Source: Internet
Author: User

Copyright notice: This article from Administration Blog, reproduced please indicate the source: http://blog.csdn.net/a_zhon/.

Directory (?) [+]

1. A brief introduction to the NDK and JNI
    1. NDK:NDK is the abbreviation for Native Development Kit, a set of tools provided by Google that allows you to develop JNI for Android in other languages (c, C + +, or assembler). The NDK can compile multi-platform so, developers simply need to modify the Mk file to explain the required platform, no need to change any code, the NDK can help you compile the necessary so library.
    2. JNI:JNI is the abbreviation for Java Native Interface, which provides a number of APIs that enable communication between Java and other languages (mainly c&c++)
      NDK Network Disk
2. Open Android Studio to configure the NDK path (or download NDK)

3. Create a class and define two nativeMethods such as:

4. Use the Javac command to HelloWorld.javaCompile and then use the JAVAH-JNI command to compile the header file needed to get JNI into the location where the file is located and execute the Javac.

It is important to note that we need to step back to the command like directory execution javah -jni . Command javah -jni 包名.类名

Here to get the header file there will be a lot of pits if prompted 错误: 找不到‘com.zsy.hellojni.HelloWorld‘ 的类文件。 You can try this command.Javah-classpath.-jni package name. Class name. If the compilation succeeds without encountering a pit, you can ···\src\main\javadirectory to see a. h file 5. Next, create a JNI directory in the project and cut the newly generated. h file to this directory

6. Let's look at the contents of this. h file first. In this case, the Java concept is equivalent to the abstract method in the interface, we need to create a. c file to implement these methods, and we will implement the native method of our definition.
/* Don't EDIT this file-it are machine generated */#include <jni.h>/* Header for class Com_zsy_hellojni_helloword */#ifndef _included_com_zsy_hellojni_helloword#define _included_com_zsy_hellojni_helloword#ifdef __cplusplusextern "C" {#endif/* * class:com_ Zsy_hellojni_helloworld * Method:helloworld * Signature: () ljava/lang/string; * TODO: Returns a String * ///equivalent to the abstract method in interface Jniexport jstring jnicall Java_com_zsy_hellojni_helloworld_helloworld ( JNIENV * env, Jclass); /* * Class:com_zsy_hellojni_helloworld * Method:add * Signature: (II) I * TODO: Make an addition */jniexport jint Jnicall Ja Va_com_zsy_hellojni_helloworld_add (jnienv * env,jclass, Jint, Jint); #ifdef __cplusplus} #endif#endif           

7. In the JNI directory, we create a hello_jni.cFile to implement the abstract method in the. h file
//Create a C file yourself, implement your own definition of the native method, that is, the method in the. h file //introduce your own generated. h header file # include <com_zsy_hellojni_HelloWord.h>/ /Returns a string Jniexport jstring jnicall java_com_zsy_hellojni_helloworld_helloworld (jnienv *env, Jclass Jobj) {  Return (*ENV)->newstringutf (env,"HelloWorld I'm a string called by Jni");} //Return a+b results jniexport jint jnicall java_com_zsy_hellojni_helloworld_add (jnienv *env, Jclass Jobj,  Jint A, jint b) { return a+b;}             

8. Next we are build.gradleAdd NDK configuration in
defaultConfig {            //...            ndk {                 moduleName "Hello"//指定生成的so文件名                abiFilters "armeabi", "armeabi-v7a", "x86"//cpu的类型 } }

9. After rebuild the project we will be able to see the resulting so file in./build

10. Now we can invoke the two native methods in the mainactivity.
static { //名字必须和build.gradle中的moduleName一致 System.loadLibrary("hello"); } TextView textView = (TextView) findViewById(R.id.tv); String s = HelloWorld.helloWorld(); int add = HelloWorld.add(3, 66); textView.setText(s+"\n"+"我是一个加法: "+add); 

Here a simple JNI program is written, demo portal

Android JNI Programming (vii)--writing the first JNI program using Androidstudio

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.