介绍

The NDK is a toolset that allows you to implement parts of your app using native-code languages such as C and C++. For certain types of apps, this can be helpful so you can reuse existing code libraries written in these languages, but most apps do not need the Android NDK. Before downloading the NDK, you should understand that the NDK will not benefit most apps. As a developer, you need to balance its benefits against its drawbacks. Notably, using native code on Android generally does not result in a noticable performance improvement, but it always increases your app complexity. In general, you should only use the NDK if it is essential to your app—never because you simply prefer to program in C/C++.

配置

  1. SDK Tool中安装Ndk
    这里写图片描述
  2. 设置"Android NDK ocation",若为自动下载,应该就是在SDK目录下
    这里写图片描述
  3. 新建JNI目录(src/main/jni),并且在build.gradle下添加ndk内容
defaultConfig {  
      applicationId “mraz.com.jnidemo”  
      minSdkVersion 15  
      targetSdkVersion 24  
      versionCode 1  
      versionName “1.0”  
      ndk {  
      moduleName ‘jniTest’//这里就是生成so的模块名,[也就是libjniTest.so](http://xn--libjniTest-k92py04kef1a.so)  
      }  
      }

这里写图片描述

  1. Rebuild Project之后出现如下错误,则需要在gradle.properties中设置"android.useDeprecatedNdk=true"
    这里写图片描述
  2. 新建一个Java类
    这里写图片描述

实际使用

经过上面的步骤,我们创建一个java类,其中定义两个native方法
JniTest.java

package mraz.com.jnidemo;
public class JniTest {
    static {
        System.loadLibrary("jniTest");
    }
    public native String getHello();
    public native int getSum(int a , int b);
}

首先,我们需要生成这个类对应的native代码中的头文件,使用javah
这里写图片描述
在Android Studio中使用Terminal,具有tab键联想功能
以Demo为例,生成对应的头文件到jni目录下:
javah -d app\src\main\jni
-classpath app\build\intermediates\classes\debug mraz.com.jnidemo.JniTest
生成头文件
mraz_com_jnidemo_JniTest.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class mraz_com_jnidemo_JniTest */

#ifndef _Included_mraz_com_jnidemo_JniTest
#define _Included_mraz_com_jnidemo_JniTest
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     mraz_com_jnidemo_JniTest
 * Method:    getHello
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_mraz_com_jnidemo_JniTest_getHello
  (JNIEnv *env, jobject object);

/*
 * Class:     mraz_com_jnidemo_JniTest
 * Method:    getSum
 * Signature: (II)I
 */
JNIEXPORT jint JNICALL Java_mraz_com_jnidemo_JniTest_getSum
  (JNIEnv *env, jobject object, jint a, jint b);

#ifdef __cplusplus
}
#endif
#endif

然后在JNI目录下创建对应的源文件mraz_com_jnidemo_JniTest.c
具体实现如下:

#include "mraz_com_jnidemo_JniTest.h"
#include<jni.h>
//
// Created by Mraz on 2016/6/28.
//
JNIEXPORT jstring JNICALL Java_mraz_com_jnidemo_JniTest_getHello(JNIEnv *env, jobject object)
{
    return (*env)->NewStringUTF(env, "hello");
}

/*
 * Class:     mraz_com_jnidemo_JniTest
 * Method:    getSum
 * Signature: (II)I
 */
JNIEXPORT jint JNICALL Java_mraz_com_jnidemo_JniTest_getSum
        (JNIEnv *env, jobject object, jint a, jint b) {
    return (a + b);
}

对应java类JniTest.java,加载so,定义native方法

package mraz.com.jnidemo;
/**
 * Created by Mraz on 2016/6/28.
 */
public class JniTest {
    static {
        System.loadLibrary("jniTest");
    }
    public native String getHello();
    public native int getSum(int a , int b);
}

MainActivity.java中使用JniTest.java

package mraz.com.jnidemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        JniTest jniTest = new JniTest();
        Log.e("yidong", " hello = " + jniTest.getHello());
        Log.e("yidong", " sum = " + jniTest.getSum(4, 6));
        TextView textView = (TextView) findViewById(R.id.tv_jni);
        textView.setText(String.valueOf(jniTest.getSum(5, 6)));
    }
}

结果

这里写图片描述

这里写图片描述

results matching ""

    No results matching ""