🔥 Android Studio 版本 🔥
🔥 创建包含JNI的类 JNIAccessMethod.java 🔥
package com.cmake.ndk1.jni; import com.cmake.ndk1.model.Animal; public class JNIAccessMethod { static { System.loadLibrary("access-method-lib"); } public native void accessInstanceMethod(Animal animal); public native void accessStaticMethod(Animal animal); }
🔥 Java方法对应Native层方法名称 🔥
* |Java |JNI | * |String fun() |()Ljva/lang/String; | * |long f(int i,Class c); |(ILjava/lang/Class;)J | * |String(byte[] bytes); |([B)V |
🔥 配置动态库名称 🔥
add_library( #动态库名称 access-method-lib access-method-lib SHARED jni/jni_access_method.cpp )
🔥 生成可关联的库链接 🔥
为了让Java能够调用 access-method-lib 库中的函数,您需要使用 CMake 构建脚本中的 target_link_libraries() 命令来关联 access-method-lib 库
target_link_libraries( # Specifies the target library. ndk1 person-lib dynamic-lib basic-type-lib string-lib reference-type-lib access-field-lib access-method-lib # Links the target library to the log library # included in the NDK. ) )
🔥 提供给 jni 调用的日志库链接 🔥
target_link_libraries( # Specifies the target library. access-method-lib # Links the target library to the log library # included in the NDK. ${log-lib})
🔥 Native层实现 JNIAccessMethod 类下面的JNI接口 🔥
实现调用 callInstanceMethod() 函数
extern "C" JNIEXPORT void JNICALL Java_com_cmake_ndk1_jni_JNIAccessMethod_accessInstanceMethod(JNIEnv *env,jobject instance, jobject animal) { jclass cls = env->GetObjectClass(animal); //查找Animal对象中的callInstanceMethod()函数,函数需要传递的参数是int类型对应I, //返回返回为void对应V jmethodID mid = env->GetMethodID(cls,"callInstanceMethod","(I)V"); env->CallVoidMethod(animal,mid,2); }
实现调用Animal中 callStaticMethod() 函数并传递字符串参数
extern "C" JNIEXPORT void JNICALL Java_com_cmake_ndk1_jni_JNIAccessMethod_accessStaticMethod(JNIEnv *env,jobject instance, jobject animal) { jclass cls = env->GetObjectClass(animal); //查找Animal对象中的callStaticMethod()函数,函数需要传递的参数是int类型对应I, //返回返回为void对应V jmethodID mid = env->GetStaticMethodID(cls,"callStaticMethod", "(Ljava/lang/String;)Ljava/lang/String;"); //构造一个调用callStaticMethod()函数需要传递的参数 jstring parJString=env->NewStringUTF("jstrinig"); env->CallStaticObjectMethod(cls,mid,parJString); }
实现调用Animal中 callStaticMethod() 函数并 传递集合和集合长度到参数文章来源:https://www.toymoban.com/news/detail-600770.html
extern "C" JNIEXPORT void JNICALL Java_com_cmake_ndk1_jni_JNIAccessMethod_accessStaticMethod(JNIEnv *env,jobject instance, jobject animal) { jclass cls = env->GetObjectClass(animal); //查找Animal对象中的callStaticMethod()函数,函数需要传递的参数是int类型对应I, //返回返回为void对应V jmethodID mid = env->GetStaticMethodID(cls,"callStaticMethod", "(Ljava/lang/String;)Ljava/lang/String;"); //构造一个调用callStaticMethod()函数需要传递的参数 jstring parJString=env->NewStringUTF("jstrinig"); env->CallStaticObjectMethod(cls,mid,parJString); mid = env->GetStaticMethodID(cls,"callStaticMethod", "([Ljava/lang/String;I)Ljava/lang/String;"); //在Native构建Java的字符串数组 jclass strClass = env->FindClass("java/lang/String"); int size=2; jobjectArray strArray = env->NewObjectArray(size,strClass, nullptr); jstring strItem; //循环添加字符串到strArray集合中 for (int i = 0; i < size; ++i) { strItem = env->NewStringUTF("string in native"); env->SetObjectArrayElement(strArray,i,strItem); } //调用静态方法 env->CallStaticObjectMethod(cls,mid,strArray,size); }
🔥 调用Native层函数操作Animal 中的函数 🔥
package com.cmake.ndk1; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.TextView; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import com.cmake.ndk1.jni.JNIAccessField; import com.cmake.ndk1.jni.JNIAccessMethod; import com.cmake.ndk1.model.Animal; public class MainActivity7 extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); JNIAccessMethod jniAccessMethod=new JNIAccessMethod(); Animal animal=new Animal("animal"); final TextView textView=findViewById(R.id.sample_text); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { jniAccessMethod.accessInstanceMethod(animal); jniAccessMethod.accessStaticMethod(animal); } }); } }
D/Animal: call instance method and num is 2 D/Animal: call static method with jstrinig D/Animal: call static method with string array D/Animal: str in array is string in native D/Animal: str in array is string in native
下载案例文章来源地址https://www.toymoban.com/news/detail-600770.html
到了这里,关于Android Java代码与JNI交互 JNI访问Java类方法 (七)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!