Android Java代码与JNI交互基础数据类型转换(三)

该文展示了如何在AndroidStudio中使用JNI进行Java与C/C++之间的基础类型数据(如int,byte,char等)转换。通过示例代码,解释了如何定义Java接口并实现Native方法,以及在C/C++侧接收和处理这些数据。
摘要由CSDN通过智能技术生成

🔥 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));
            }
        });
    }
}

 运行打印日志

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

 下载案例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

️ 邪神

你自己看着办,你喜欢打赏我就赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值