🔥 Android Studio 版本 🔥
🔥 基础类型数据的转换 🔥
定义传递基础数据类型到Native的JNI接口函数
package com.cmake.ndk1.jni; public class JNIBasicType{ static{ System.loadLibrary("native-lib"); } public native int callNativeInt(int num); public native byte callNativeByte(byte b); public native char callNativeChar(char ch); public native short callNativeShort(short sh); public native long callNativeLong(long l); public native float callNativeFloat(float f); public native double callNativeDouble(double d); public native boolean callNativeBool(boolean b); };
Native层创建callNativeInt 提供给Java调用
#include <base.h> #include <jni.h> #include <string> extern "C" JNIEXPORT jint JNICALL Java_com_cmake_ndk1_jni_JNIBasicType_callNativeInt(JNIEnv *env, jobject thiz, jint num) { LOGD("java int value is %d",num); int c_num = num * 2; return c_num; }
CMakeLists.txt 文件下配置动态库名称
添加 target_link_libraries 配置
target_link_libraries( # Specifies the target library. ndk1 person-lib dynamic-lib basic-type-lib # Links the target library to the log library # included in the NDK. ) )
target_link_libraries( # Specifies the target library. basic-type-lib # Links the target library to the log library # included in the NDK. ${log-lib})
Java调用Native层函数
package com.cmake.ndk1; import android.os.Bundle; import android.view.View; import android.widget.TextView; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import com.cmake.ndk1.jni.JNIBasicType; public class MainActivity2 extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); JNIBasicType jniBasicType=new JNIBasicType(); final TextView textView=findViewById(R.id.sample_text); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int nativeInt=jniBasicType.callNativeInt(2); textView.setText(String.valueOf(nativeInt)); } }); } }
运行打印日志
文章来源:https://www.toymoban.com/news/detail-548206.html
Java调用Native分别打印基础类型数据
#include <base.h> #include <jni.h> #include <string> #include <jni.h> extern "C" JNIEXPORT jint JNICALL Java_com_cmake_ndk1_jni_JNIBasicType_callNativeInt(JNIEnv *env, jobject thiz, jint num) { LOGD("java int value is %d",num); int c_num = num * 2; return c_num; } extern "C" JNIEXPORT jbyte JNICALL Java_com_cmake_ndk1_jni_JNIBasicType_callNativeByte(JNIEnv *env, jobject thiz, jbyte b) { // TODO: implement callNativeByte() LOGD("java byte value is %d",b); jbyte c_byte=b+(jbyte)10; return c_byte; } extern "C" JNIEXPORT jchar JNICALL Java_com_cmake_ndk1_jni_JNIBasicType_callNativeChar(JNIEnv *env, jobject thiz, jchar ch) { // TODO: implement callNativeChar() LOGD("java char value is %c",ch); jchar c_char=ch+(jchar)3; return c_char; } extern "C" JNIEXPORT jshort JNICALL Java_com_cmake_ndk1_jni_JNIBasicType_callNativeShort(JNIEnv *env, jobject thiz, jshort sh) { // TODO: implement callNativeShort() LOGD("java short value is %d",sh); jshort c_short=sh+(jshort)1000; return c_short; } extern "C" JNIEXPORT jlong JNICALL Java_com_cmake_ndk1_jni_JNIBasicType_callNativeLong(JNIEnv *env, jobject thiz, jlong l) { // TODO: implement callNativeLong() LOGD("java long value is %d",l); jlong c_long=l+(jlong)1000; return c_long; } extern "C" JNIEXPORT jfloat JNICALL Java_com_cmake_ndk1_jni_JNIBasicType_callNativeFloat(JNIEnv *env, jobject thiz, jfloat f) { // TODO: implement callNativeFloat() LOGD("java float value is %f",f); jfloat c_float=f+(jfloat)10.1; return c_float; } extern "C" JNIEXPORT jdouble JNICALL Java_com_cmake_ndk1_jni_JNIBasicType_callNativeDouble(JNIEnv *env, jobject thiz, jdouble d) { // TODO: implement callNativeDouble() LOGD("java double value is %f",d); jdouble c_double=d+(jdouble)10.1; return c_double; } extern "C" JNIEXPORT jboolean JNICALL Java_com_cmake_ndk1_jni_JNIBasicType_callNativeBool(JNIEnv *env, jobject thiz, jboolean b) { // TODO: implement callNativeBool() LOGD("java boolean value is %d",b); jboolean c_boolean=(jboolean)b; return c_boolean; }
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.JNIBasicType; public class MainActivity3 extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); JNIBasicType jniBasicType=new JNIBasicType(); final TextView textView=findViewById(R.id.sample_text); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.d("MainActivity3",String.valueOf(jniBasicType.callNativeInt(2))); Log.d("MainActivity3",String.valueOf( jniBasicType.callNativeByte((byte) 4))); Log.d("MainActivity3",String.valueOf(jniBasicType.callNativeChar('A'))); Log.d("MainActivity3",String.valueOf(jniBasicType.callNativeShort((short) 6))); Log.d("MainActivity3",String.valueOf(jniBasicType.callNativeLong(8))); Log.d("MainActivity3",String.valueOf(jniBasicType.callNativeFloat(2.1f))); Log.d("MainActivity3",String.valueOf(jniBasicType.callNativeDouble(2.10))); Log.d("MainActivity3",String.valueOf(jniBasicType.callNativeBool(true))); } }); } }
D/LOG_JNI: java int value is 2 D/MainActivity3: 4 D/LOG_JNI: java byte value is 4 D/MainActivity3: 14 D/LOG_JNI: java char value is A D/MainActivity3: D D/LOG_JNI: java short value is 6 D/MainActivity3: 1006 D/LOG_JNI: java long value is 8 D/MainActivity3: 1008 D/LOG_JNI: java float value is 2.100000 D/MainActivity3: 12.200001 D/LOG_JNI: java double value is 2.100000 D/MainActivity3: 12.2 D/LOG_JNI: java boolean value is 1 D/MainActivity3: true
下载案例文章来源地址https://www.toymoban.com/news/detail-548206.html
到了这里,关于Android Java代码与JNI交互基础数据类型转换(三)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!