🔥 Android Studio 版本 🔥
🔥 创建包含JNI相关函数类 JNIConstructorClass.java 🔥
package com.cmake.ndk1.jni; import com.cmake.ndk1.model.Animal; public class JNIConstructorClass { static { System.loadLibrary("constructor-class-lib"); } public native Animal invokeAnimalConstructors(); public native Animal allocObjectConstructor(); }
🔥 配置动态库名称 🔥
add_library( #动态库名称 constructor-class-lib constructor-class-lib SHARED jni/jni_constructor_class.cpp )
🔥 生成可关联的库链接 🔥
为了让Java能够调用 constructor-class-lib 库中的函数,您需要使用 CMake 构建脚本中的 target_link_libraries() 命令来关联constructor-class-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 invoke-method-lib constructor-class-lib # Links the target library to the log library # included in the NDK. ) )
🔥 提供给Native层调用的日志库链接 🔥
target_link_libraries( # Specifies the target library. constructor-class-lib # Links the target library to the log library # included in the NDK. ${log-lib})
🔥 Native层实现 JNIConstructorClass类下面的JNI接口函数 🔥
#include <base.h> #include <jvm.h> extern "C" JNIEXPORT jobject JNICALL Java_com_cmake_ndk1_jni_JNIConstructorClass_invokeAnimalConstructors(JNIEnv *env, jobject thiz) { //调用java中某个类的方法和调用某个类的构造方法没有什么差别 //查找Animal类,将包名中的点(.)替换成斜杠(/) (com.cmake.ndk1.model) jclass cls=env->FindClass("com/cmake/ndk1/model/Animal"); //这里构造函数没有具体的名字,就用 "<init>" 代替 jmethodID mid=env->GetMethodID(cls,"<init>","(Ljava/lang/String;)V"); //构造一个传递给构造函数的参数 jstring str = env->NewStringUTF("this is animal name 1"); //获取带有构造函数的Animal jobject animal = env->NewObject(cls,mid,str); return animal; } extern "C" JNIEXPORT jobject JNICALL Java_com_cmake_ndk1_jni_JNIConstructorClass_allocObjectConstructor(JNIEnv *env, jobject thiz) { //查找Animal类,将包名中的点(.)替换成斜杠(/) (com.cmake.ndk1.model) jclass cls=env->FindClass("com/cmake/ndk1/model/Animal"); //这里构造函数没有具体的名字,就用 "<init>" 代替 jmethodID mid=env->GetMethodID(cls,"<init>","(Ljava/lang/String;)V"); //获取带有构造函数的Animal jobject animal=env->AllocObject(cls); //构造一个传递给构造函数的参数 jstring str = env->NewStringUTF("this is animal name 2"); env->CallNonvirtualVoidMethod(animal,cls,mid,str); return 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.base.ICallbackMethod; import com.cmake.ndk1.base.IThreadCallback; import com.cmake.ndk1.jni.JNIConstructorClass; import com.cmake.ndk1.jni.JNIInvokeMethod; public class MainActivity9 extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final JNIConstructorClass constructorClass = new JNIConstructorClass(); final TextView textView = findViewById(R.id.sample_text); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.i("MainActivity9", "name is" + constructorClass.invokeAnimalConstructors().getName()); Log.i("MainActivity9", "name is" + constructorClass.allocObjectConstructor().getName()); } }); } }
I/MainActivity9: name isthis is animal name 2 D/Animal: call getName method I/MainActivity9: name isthis is animal name 1
下载案例文章来源:https://www.toymoban.com/news/detail-556813.html
Android-Jni中使用多线程(pthread) 文章来源地址https://www.toymoban.com/news/detail-556813.html
到了这里,关于Android Java代码与JNI交互 JNI访问Java构造方法(九)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!