首先在Classes文件夹下新建C++文件
InterfaceJNI.cpp
InterfaceJNI.h
InterfaceJNI.h
#include <string.h>
#include "cocos2d.h"
using namespace std;
using namespace cocos2d;
class InterfaceJNI
{
public:
static void func1();
};
InterfaceJNI.cpp
#include "InterfaceJNI.h"
#include "platform/android/jni/JniHelper.h"
#include <jni.h>
#include <android/log.h>
// Android那边的文件的包
#define CLASS_NAME "com/china/jniTest/jniTest"
void InterfaceJNI::func1()
{
JniMethodInfo t;
// Class名和方法名的指定。func1是在Android代码那边定义的方法名。
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "func1", "()V")) {
// 由于是void、所以使用CallStaticVoidMethod方法。
t.env->CallStaticVoidMethod(t.classID, t.methodID);
// 释放
t.env->DeleteLocalRef(t.classID);
}
}
InterfaceJNI.cpp
#include "InterfaceJNI.h"
#include "platform/android/jni/JniHelper.h"
#include <jni.h>
#include <android/log.h>
// Android那边的文件的包
#define CLASS_NAME "com/china/jniTest/jniTest"
voidInterfaceJNI::func1()
{
JniMethodInfo t;
// Class名和方法名的指定。func1是在Android代码那边定义的方法名。
if(JniHelper::getStaticMethodInfo(t, CLASS_NAME, "func1", "()V")) {
// 由于是void、所以使用CallStaticVoidMethod方法。
t.env->CallStaticVoidMethod(t.classID, t.methodID);
// 释放
t.env->DeleteLocalRef(t.classID);
}
}
下面在Cocos2dx中调用刚才定义好的JNI方法吧。
在HelloWorldScene.cpp中调用吧。
HelloWorldScene.cpp
#include "HelloWorldScene.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "JNICalls/InterfaceJNI.h"
#endif
USING_NS_CC;
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
this->jniLoad();
}
void HelloWorld::jniLoad()
{
// 记得要加上ANDROID的判定。
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
InterfaceJNI::func1();
#endif
}
这样一来Cocos2dx方的方法都定义好了。
接下来该做Android的方法了。
jniTest.java
package com.marnishi.jniTest;
import org.cocos2dx.lib.Cocos2dxActivity;
import android.os.Bundle;
public class jniTest extends Cocos2dxActivity{
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
public static void func1() {
System.out.println("func1 call");
}
static {
System.loadLibrary("game");
}
}
------------------------------------------------------------
----------------------华丽的分割线--------------------------
------------------------------------------------------------
上面只做了个无返回值,无参数的例子。
下面整几个有返回值,有参数的例子吧。
cocos2d-x
InterfaceJNI.h
#include <string.h>
#include "cocos2d.h"
using namespace std;
using namespace cocos2d;
class InterfaceJNI
{
public:
static void func1();
static void func2(int value);
static void func3(bool value);
static void func4(const char *value);
static void func5(const char *value1, int value2, bool value3);
static int func6();
static std::string func7();
};
InterfaceJNI.cpp
#include "InterfaceJNI.h"
#include "platform/android/jni/JniHelper.h"
#include <jni.h>
#include <android/log.h>
// Android的Package名和java Class名
#define CLASS_NAME "com/china/jniTest/jniTest"
void InterfaceJNI::func1()
{
JniMethodInfo t;
// Class名和方法名的指定。
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "func1", "()V")) {
// 因为是void、所以用CallStaticVoidMethod
t.env->CallStaticVoidMethod(t.classID, t.methodID);
// 释放
t.env->DeleteLocalRef(t.classID);
}
}
/*
// Android代码这样写即可
public static void func1() {
System.out.println("func1 call");
}
*/
void InterfaceJNI::func2(int value)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "func2", "(I)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, value);
t.env->DeleteLocalRef(t.classID);
}
}
/*
public static void func2(int value) {
System.out.println("func2 val=" + value);
}
*/
void InterfaceJNI::func3(bool value)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "func3", "(Z)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, value);
t.env->DeleteLocalRef(t.classID);
}
}
/*
public static void func3(boolean value) {
System.out.println("func3 val=" + value);
}
*/
void InterfaceJNI::func4(const char *value)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "func4", "(Ljava/lang/String;)V")) {
jstring stringArg1 = t.env->NewStringUTF(value);
t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg1);
t.env->DeleteLocalRef(stringArg1);
t.env->DeleteLocalRef(t.classID);
}
}
/*
public static void func4(String value) {
System.out.println("func4 val=" + value);
}
*/
void InterfaceJNI::func5(const char *value1, int value2, bool value3)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "func5", "(Ljava/lang/String;IZ)V")) {
jstring stringArg1 = t.env->NewStringUTF(value1);
t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg1, value2, value3);
t.env->DeleteLocalRef(stringArg1);
t.env->DeleteLocalRef(t.classID);
}
}
/*
public static void func5(String value1, int value2, boolean value3) {
System.out.println("func5 val=" + value1 + " int=" + value2 + " bool=" + value3);
}
*/
int InterfaceJNI::func6()
{
int ret = 0;
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "func6", "()I")) {
ret = t.env->CallStaticIntMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
}
return ret;
}
/*
public static int func6() {
return 12345;
}
*/
std::string InterfaceJNI::func7()
{
std::string ret;
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "func7", "()Ljava/lang/String;")) {
jstring jStr = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
const char* str = t.env->GetStringUTFChars(jStr, NULL);
ret = str;
t.env->ReleaseStringUTFChars(jStr,str);
t.env->DeleteLocalRef(t.classID);
}
return ret;
}
/*
public static String func7() {
return "禁止撸管";
}
*/
完事儿
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。