android jni介绍

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android jni介绍相关的知识,希望对你有一定的参考价值。

Jni API:https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/jniTOC.html
JNIEnv、jobject和jclass这三种基本类型

操作API都在JNIEnv中,JNIEnv为Java与C/C++通信桥梁
jobject:Java层传递的对象(普通native方法传递)
jclass:Java层对应的Class类(静态native方法传递)

native普通方法与静态方法区别:Jni传递参数是jobject和jclass区别

android studio ndk api自动补齐
安装插件:
Android NDK Support
NDK WorkspaceManager Support

属性,方法,数组例子:
Java层
public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";
//测试属性
public String strName ="test";

//排序数组
private int[] array = {89,2,4,34,88,100,1};
// Used to load the ‘native-lib‘ library on application startup.
static {
    System.loadLibrary("native-lib");
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Example of a call to a native method
    TextView tv = (TextView) findViewById(R.id.sample_text);

    //测试属性操作
    tv.setText(updateStringFromC());
    //测试方法调用
    tv.setText(getJavaMethod());

    //测试数组操作
    this.operateArraySort(array);
    for (int i = 0; i < array.length; i++){
        Log.e(TAG, "onCreate: " + array[i]);
    }

    //异常处理 C++的异常 JAVA是无法try catch的,如果jni层出现了异常,那么Java的代码调用中止
    try {
        exception();
    }catch (Exception e){
        Log.e(TAG, "onCreate: " + e.getMessage());
    }
    Log.e(TAG, "onCreate: " + "end");

}
//测试方法
public String getMethod(int a){
    return "method" + a;
}

/**
 * A native method that is implemented by the ‘native-lib‘ native library,
 * which is packaged with this application.
 */
public native String stringFromJNI();
//从C返回字符串
public native String updateStringFromC();
//调用Java方法
public native String getJavaMethod();
//对array数组进行排序操作
public native void operateArraySort(int[] array);
//异常处理
public native void exception();

}

Jni层:

#include <jni.h>
#include <string>
#include "stdlib.h"

//C C++混编

int compare(const void* lhs, const void* rhs){
    return (*(int*)lhs - *(int*)rhs);
}

//异常处理
extern "C"
JNIEXPORT void JNICALL
Java_com_home_adproj_androidndktest_MainActivity_exception(JNIEnv *env, jobject instance) {
    jclass  _jclass = env->GetObjectClass(instance);
    jfieldID _jfieldID = env->GetFieldID(_jclass,"strName2","Ljava/lang/String;");//strName2不存在有异常
    if(env->ExceptionCheck()){//检测异常
        env->ExceptionClear();//清除异常 不会使Java层崩溃
        //抛异常给Java层
        jclass exceptionClass = env->FindClass("java/lang/IllegalArgumentException");
        env->ThrowNew(exceptionClass,"strName2 不存在");//抛异常到Java层,Java层进行捕获
    }
}

//对Java数组进行操作
extern "C"
JNIEXPORT void JNICALL
Java_com_home_adproj_androidndktest_MainActivity_operateArraySort(JNIEnv *env, jobject instance,
                                                                  jintArray array_) {
    jint *arrays = env->GetIntArrayElements(array_, NULL);
    int _len = env->GetArrayLength(array_);//获取数组长度
    //qsort(void* __base, size_t __nmemb, size_t __size, int (*__comparator)(const void* __lhs, const void* __rhs));
    qsort(arrays,_len, sizeof(int),compare);//数组排序
    env->ReleaseIntArrayElements(array_, arrays, 0);//释放
}

//调用Java层方法
extern "C"
JNIEXPORT jstring JNICALL
Java_com_home_adproj_androidndktest_MainActivity_getJavaMethod(JNIEnv *env, jobject instance) {
    jclass _jclass =env->GetObjectClass(instance);
    //(jclass clazz, const char* name, const char* sig)
    jmethodID _jmethodID = env->GetMethodID(_jclass,"getMethod","(I)Ljava/lang/String;");//(参数类型)Ljava/lang/String返回值类型
    jstring result = (jstring) env->CallObjectMethod(instance, _jmethodID,100);
    char* str = (char *) env->GetStringUTFChars(result, NULL);//string 转char
    return env->NewStringUTF(str);
}

//测试Java属性操作
extern "C"
JNIEXPORT jstring JNICALL
Java_com_home_adproj_androidndktest_MainActivity_updateStringFromC(JNIEnv *env, jobject instance) {
    // TODO
    jclass _jclass = env->GetObjectClass(instance);
    //(jclass clazz, const char* name, const char* sig)
    //name:属性名  sig:属性类型
    jfieldID _jfieldID = env->GetFieldID(_jclass,"strName","Ljava/lang/String;");
    jstring _result = (jstring) env->GetObjectField(instance, _jfieldID);

    //如何转化成java string
    char* str = (char *) env->GetStringUTFChars(_result, NULL);

    char text[20] = "success";
    return env->NewStringUTF(strcat(str,text));
}

extern "C"
JNIEXPORT jstring JNICALL
Java_com_home_adproj_androidndktest_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

lldb:jni调试工具
官网:http://lldb.llvm.org/tutorial.html

以上是关于android jni介绍的主要内容,如果未能解决你的问题,请参考以下文章

Android 动态注册JNI函数

Android开发实践:Java层与Jni层的数组传递

Android JNI&NDK编程小结及建议

Android JNI 学习:JNI 设计概述

Android JNI开发

深入理解JNI