[cocos2dx]在cocos2dx中通过Jni实现Java与C++的互相调用(二)
发布时间:2020-12-14 17:22:01 所属栏目:百科 来源:网络整理
导读:jni详解介绍 JNI是JVM实现中的一部分,因此Native语言和Java代码都运行在JVM的宿主环境。JNI的出现使得开发者既可以利用Java语言跨平台、类库丰 富、开发便捷等特点,又可以利用Native语言的高效。 JNI是一个双向的接口:开发者不仅可以通过JNI在Java代码中
jni详解介绍JNI是JVM实现中的一部分,因此Native语言和Java代码都运行在JVM的宿主环境。JNI的出现使得开发者既可以利用Java语言跨平台、类库丰 富、开发便捷等特点,又可以利用Native语言的高效。JNI是一个双向的接口:开发者不仅可以通过JNI在Java代码中访问Native模块,还可以在 Native代码中嵌入一个JVM,并通过JNI访问运行于其中的Java模块。可见,JNI担任了一个桥梁的角色,它将JVM与Native模块联系起来,从而实现了Java代码与Native代码的互访。如下图: 缺点:由于Native模块的使用,Java代码会丧失其原有的跨平台性和类型安全等特性。但是这不是我们应该担心的,不是吗?哈哈。也就是说,JNI是帮助游戏在Java代码中调用Native接口和在Native代码中调用Java接口。 方法1. 基本使用c++接口, 一般来说,要在Native代码中访问Java对象,有如下几个步骤:
方法2. jnihelper2dx里面为我们提供了一个JniHelper类,来满足与Java层的数据交互,JniHelper可以很方便的调用java层的动静态方法。C++调用JavaJniUtil.h
#pragma once #include <string> using namespace std; namespace JniUtil { string callJava_getAppVersion(); bool callJava_copyText(string copyText); string callJava_getTestAllString(bool b,int i,float f,double d,string s); void callJava_callNativeFunShowText(bool b,string s); } JniUtil.cpp
#include "JniUtil.h" #include "cocos2d.h" #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID #include <Jni.h> #include "platform/android/jni/JniHelper.h" #endif #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS #include "IosHelper.h" #endif #define JAVA_CLASSNAME "org/cocos2dx/cpp/AppActivity" using namespace cocos2d; namespace JniUtil { string callJava_getAppVersion() { string str = ""; #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID JniMethodInfo minfo; //在org.cocos2dx.cpp.AppActivity文件中查找static String getAppVersion();这静态方法是否存在。 bool isHave = JniHelper::getStaticMethodInfo(minfo,JAVA_CLASSNAME,"getAppVersion","()Ljava/lang/String;"); if (isHave) { jstring jVersion = (jstring)minfo.env->CallStaticObjectMethod(minfo.classID,minfo.methodID); //使用jstring2string函数将返回的jstring类型的值转化为c++中的string类型 //string text = JniHelper::jstring2string(jVersion); const char* version = minfo.env->GetStringUTFChars(jVersion,0); str = version; minfo.env->ReleaseStringUTFChars(jVersion,version); minfo.env->DeleteLocalRef(minfo.classID); cocos2d::log("JniFun call callJava_getAppVersion over!===%s",str.c_str()); } else { cocos2d::log("JniFun call callJava_getAppVersion error!"); } #endif #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS //IosHelper::HuiPay(kStr.c_str()); #endif return str; } bool callJava_copyText(string copyText) { bool isSuccess = false; #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID JniMethodInfo minfo; bool isHave = JniHelper::getStaticMethodInfo(minfo,"copyText","(Ljava/lang/String;)Z"); if (isHave) { jstring jCopyText = minfo.env->NewStringUTF(copyText.c_str()); jboolean jIsSuccess= minfo.env->CallStaticBooleanMethod(minfo.classID,minfo.methodID,jCopyText); isSuccess = jIsSuccess; minfo.env->DeleteLocalRef(minfo.classID); cocos2d::log("JniFun call callJava_copyText over!"); } else { cocos2d::log("JniFun call callJava_copyText error!"); } #endif #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS //IosHelper::HuiPay(kStr.c_str()); #endif return isSuccess; } string callJava_getTestAllString(bool b,string s) { string str = ""; #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID JniMethodInfo minfo; bool isHave = JniHelper::getStaticMethodInfo(minfo,"getTestAllString","(ZIFDLjava/lang/String;)Ljava/lang/String;"); if (isHave) { jstring js = minfo.env->NewStringUTF(s.c_str()); jstring jRes = (jstring)minfo.env->CallStaticObjectMethod(minfo.classID,b,i,f,d,js); const char* res = minfo.env->GetStringUTFChars(jRes,0); str = res; minfo.env->ReleaseStringUTFChars(jRes,res); minfo.env->DeleteLocalRef(js); minfo.env->DeleteLocalRef(minfo.classID); cocos2d::log("JniFun call callJava_getTestAllString over!==%s",str.c_str()); } else { cocos2d::log("JniFun call callJava_getTestAllString error!"); } #endif #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS //IosHelper::HuiPay(kStr.c_str()); #endif return str; } void callJava_callNativeFunShowText(bool b,string s) { #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID JniMethodInfo minfo; bool isHave = JniHelper::getStaticMethodInfo(minfo,"callNativeFunShowText","(ZIFDLjava/lang/String;)V"); if (isHave) { jstring js = minfo.env->NewStringUTF(s.c_str()); jstring jRes = (jstring)minfo.env->CallStaticObjectMethod(minfo.classID,0); string str = res; minfo.env->ReleaseStringUTFChars(jRes,res); minfo.env->DeleteLocalRef(js); minfo.env->DeleteLocalRef(minfo.classID); cocos2d::log("JniFun call callJava_callNativeFunShowText over!==%s",str.c_str()); } else { cocos2d::log("JniFun call callJava_callNativeFunShowText error!"); } #endif #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS //IosHelper::HuiPay(kStr.c_str()); #endif } } Java调用C++
AppActivity.java
package org.cocos2dx.cpp; import org.cocos2dx.lib.Cocos2dxActivity; import org.cocos2dx.lib.Cocos2dxHelper; import android.content.ClipData; import android.content.ClipboardManager; import android.content.pm.PackageInfo; import android.os.Bundle; import android.util.AndroidException; import android.view.WindowManager; public class AppActivity extends Cocos2dxActivity //public class AppActivity extends Cocos2dxHelper { //在java类中定义一个方法,用于提供给java调用C++ public static native void NativeFunShowText(String text); private static AppActivity appActivity = null; //剪切板管理工具类 private static ClipboardManager mClipboardManager; //剪切板Data对象 private static ClipData mClipData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); appActivity = this; getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); mClipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); } public static String getAppVersion() throws AndroidException { PackageInfo pInfo = getContext().getPackageManager().getPackageInfo(getContext().getPackageName(),0); String version = pInfo.versionName +" "+ pInfo.versionCode; return version; } public static boolean copyText(String copyTxt) { //创建一个新的文本clip对象 mClipData = ClipData.newPlainText("Simple test",copyTxt); //把clip对象放在剪贴板中 mClipboardManager.setPrimaryClip(mClipData); return true; } public static String getTestAllString(boolean b,String s) { final String str = "bool:"+ b + " int:" + i + " float:" + f + " double:" + d + " String:" + s; System.out.println("----getTestAll----out runOnUiThread-----"+str); //添加到主线程 appActivity.runOnUiThread(new Runnable(){ public void run(){ System.out.println("----getTestAll----in runOnUiThread-----"+str); } }); return str; } public static void callNativeFunShowText(boolean b,String s) { final String str = "bool:"+ b + " int:" + i + " float:" + f + " double:" + d + " String:" + s; System.out.println("----callNativeFunShowText----out runOnGLThread-----"+str); //想从java代码来改变cocos2dxUI界面,需要在GL线程中运行,否则会崩溃 appActivity.runOnGLThread(new Runnable(){ public void run(){ System.out.println("----callNativeFunShowText----in runOnGLThread-----"+str); NativeFunShowText(str); } }); } }
JniCallback.h
#pragma once namespace JniCallback { }
JniCallback.cpp
#include "JniCallback.h" #include "cocos2d.h" #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID #include <Jni.h> #include "platform/android/jni/JniHelper.h" #endif using namespace cocos2d; namespace JniCallback { extern "C" { #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID //Java_:是格式,必须加的 //org_cocos2dx_cpp_AppActivity_NativeFunShowText:是包名+类名+方法名 JNIEXPORT void JNICALL Java_org_cocos2dx_cpp_AppActivity_NativeFunShowText (JNIEnv* env,jclass method,jstring param) { const char* data = env->GetStringUTFChars(param,0); cocos2d::log("Java_org_cocos2dx_cpp_AppActivity_NativeFunShowText---- :%s",data); //do cocosUI something env->ReleaseStringUTFChars(param,data); } #endif } } LOCAL_SRC_FILES := hellocpp/main.cpp hellocpp/test.cpp <--为新添加的可参考 [cocos2dx]Android.mk学习 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |