facebook twitter hatena line email

「Cocos2dx/AndroidNative連携/引数&戻り値あり」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ページの作成:「 ==E/JniHelper: Failed to find static method id ofエラーが出る場合== *引数がintの場合は"()V"の部分を"(I)V"に書き換えてあげる *引数がintで...」)
 
行1: 行1:
 +
==サンプルコード==
 +
-Classes/NativeLauncher.h
 +
#ifndef Helloworld_NativeLauncher_h
 +
#define Helloworld_NativeLauncher_h
 +
class NativeLauncher
 +
{
 +
public:
 +
    static void launchNative();
 +
};
 +
#endif
 +
 +
-Classes/NativeLauncher.cpp
 +
#include "NativeLauncher.h"
 +
#include <jni.h>
 +
#include "platform/android/jni/JniHelper.h"
 +
#define CLASS_NAME "org/cocos2dx/cpp/AppActivity"
 +
void NativeLauncher::launchNative()
 +
{
 +
    cocos2d::JniMethodInfo t;
 +
    if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "launchNative", "()V")) {
 +
        t.env->CallStaticVoidMethod(t.classID, t.methodID);
 +
        t.env->DeleteLocalRef(t.classID);
 +
    }
 +
}
 +
int NativeLauncher::tasizan(int value, int value2)
 +
{
 +
    int ret = 0;
 +
    cocos2d::JniMethodInfo t;
 +
    if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "tasizan", "(II)I")) {
 +
        ret = t.env->CallStaticIntMethod(t.classID, t.methodID, value, value2);
 +
        t.env->DeleteLocalRef(t.classID);
 +
    }
 +
    return ret;
 +
}
 +
 +
-Classes/HelloWorldScene.h
 +
class HelloWorld : public cocos2d::Layer
 +
{
 +
public:
 +
    static cocos2d::Scene* createScene();
 +
    virtual bool init();
 +
    // a selector callback
 +
    void menuCloseCallback(cocos2d::Ref* pSender);
 +
    void menuLogCallback(cocos2d::Ref* pSender);
 +
    // implement the "static create()" method manually
 +
    CREATE_FUNC(HelloWorld);
 +
};
 +
 +
-Classes/HelloWorldScene.cpp
 +
#include "NativeLauncher.h"
 +
bool HelloWorld::init()
 +
{
 +
// 略
 +
    auto logItem = MenuItemImage::create(
 +
                                          "CloseNormal.png",
 +
                                          "CloseSelected.png",
 +
                                          CC_CALLBACK_1(HelloWorld::menuLogCallback, this));
 +
    logItem->setPosition(Vec2(origin.x + visibleSize.width - logItem->getContentSize().width/2 ,
 +
                                origin.y + visibleSize.height - logItem->getContentSize().height/2));
 +
    auto logmenu = Menu::create(logItem, NULL);
 +
    logmenu->setPosition(Vec2::ZERO);
 +
    this->addChild(logmenu, 1);
 +
// 略
 +
}
 +
void HelloWorld::menuLogCallback(Ref* pSender)
 +
{
 +
    NativeLauncher::launchNative();
 +
}
 +
 +
-proj.android-studio/app/src/org/cocos2dx/cpp/AppActivity.java
 +
public class AppActivity extends Cocos2dxActivity {
 +
    public static void launchNative()
 +
    {
 +
        System.out.println("launchNative()");
 +
    }
 +
}
 +
 +
-proj.android/jni/Android.mk
 +
LOCAL_SRC_FILES := hellocpp/main.cpp \
 +
                    ../../Classes/AppDelegate.cpp \
 +
                    ../../Classes/HelloWorldScene.cpp \
 +
                    ../../Classes/NativeLauncher.cpp
 +
NativeLauncher.cppを追加
 +
 +
-proj.android-studio/jni/Android.mk
 +
LOCAL_SRC_FILES := hellocpp/main.cpp \
 +
                    ../../Classes/AppDelegate.cpp \
 +
                    ../../Classes/HelloWorldScene.cpp \
 +
                    ../../Classes/NativeLauncher.cpp
 +
NativeLauncher.cppを追加
  
 
==E/JniHelper: Failed to find static method id ofエラーが出る場合==
 
==E/JniHelper: Failed to find static method id ofエラーが出る場合==

2017年2月21日 (火) 16:37時点における版

サンプルコード

-Classes/NativeLauncher.h

#ifndef Helloworld_NativeLauncher_h
#define Helloworld_NativeLauncher_h
class NativeLauncher
{
public:
    static void launchNative();
};
#endif

-Classes/NativeLauncher.cpp

#include "NativeLauncher.h"
#include <jni.h>
#include "platform/android/jni/JniHelper.h"
#define CLASS_NAME "org/cocos2dx/cpp/AppActivity"
void NativeLauncher::launchNative()
{
    cocos2d::JniMethodInfo t;
    if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "launchNative", "()V")) {
        t.env->CallStaticVoidMethod(t.classID, t.methodID);
        t.env->DeleteLocalRef(t.classID);
    }
}
int NativeLauncher::tasizan(int value, int value2)
{
    int ret = 0;
    cocos2d::JniMethodInfo t;
    if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "tasizan", "(II)I")) {
        ret = t.env->CallStaticIntMethod(t.classID, t.methodID, value, value2);
        t.env->DeleteLocalRef(t.classID);
    }
    return ret;
}

-Classes/HelloWorldScene.h

class HelloWorld : public cocos2d::Layer
{
public:
    static cocos2d::Scene* createScene();
    virtual bool init();
    // a selector callback
    void menuCloseCallback(cocos2d::Ref* pSender);
    void menuLogCallback(cocos2d::Ref* pSender);
    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
};

-Classes/HelloWorldScene.cpp

#include "NativeLauncher.h"
bool HelloWorld::init()
{
// 略
   auto logItem = MenuItemImage::create(
                                          "CloseNormal.png",
                                          "CloseSelected.png",
                                          CC_CALLBACK_1(HelloWorld::menuLogCallback, this));
   logItem->setPosition(Vec2(origin.x + visibleSize.width - logItem->getContentSize().width/2 ,
                                origin.y + visibleSize.height - logItem->getContentSize().height/2));
   auto logmenu = Menu::create(logItem, NULL);
   logmenu->setPosition(Vec2::ZERO);
   this->addChild(logmenu, 1);
// 略
}
void HelloWorld::menuLogCallback(Ref* pSender)
{
    NativeLauncher::launchNative();
}

-proj.android-studio/app/src/org/cocos2dx/cpp/AppActivity.java

public class AppActivity extends Cocos2dxActivity {
    public static void launchNative()
    {
        System.out.println("launchNative()");
    }
}

-proj.android/jni/Android.mk

LOCAL_SRC_FILES := hellocpp/main.cpp \
                   ../../Classes/AppDelegate.cpp \
                   ../../Classes/HelloWorldScene.cpp \
                   ../../Classes/NativeLauncher.cpp

NativeLauncher.cppを追加

-proj.android-studio/jni/Android.mk

LOCAL_SRC_FILES := hellocpp/main.cpp \
                   ../../Classes/AppDelegate.cpp \
                   ../../Classes/HelloWorldScene.cpp \
                   ../../Classes/NativeLauncher.cpp

NativeLauncher.cppを追加

E/JniHelper: Failed to find static method id ofエラーが出る場合

  • 引数がintの場合は"()V"の部分を"(I)V"に書き換えてあげる
  • 引数がintで戻りもintの場合は"()V"の部分を"(I)I"に書き換えてあげる
  • 引数が2つあり両方intの場合は"()V"の部分を"(II)I"に書き換えてあげる

参考:http://qiita.com/isaoeka/items/9cabb349cd18319fd239

  • 文字列が引数の場合は"()V"の部分に;があるか確認する
if(JniHelper::getStaticMethodInfo(t, CLASS_NAME, "function_name", "( Ljava/lang/String; Ljava/lang/String;)V")

参考:http://r2274.blog9.fc2.com/blog-entry-1658.html