RC1 JNI call C++ error

RC1 JNI call C++ error
0

#1

hello.
in ver RC1,
i try call c++ function from Java.

but it is error,
java.lang.UnsatisfiedLinkError: Native method not found: org.cocos2dx.cpp.AppActivity.CppFunc:()V
pease help this problem.

AppActivity.java

package org.cocos2dx.cpp;
import org.cocos2dx.lib.Cocos2dxActivity;
public class AppActivity extends Cocos2dxActivity
{
    public static native void CppFunc();
}

hellowolrd.cpp

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    #include "platform/android/jni/JniHelper.h"
#endif

extern "C" {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    JNIEXPORT void JNICALL  Java_org_cocos2dx_cpp_AppActivity_CppFunc(JNIEnv *env, jobject obj)
    {
       
    }
#endif

#2

I think your method signature is incorrect.
It’s declared as having no parameter but implemented as having 1 parameter.

Try it like this:

JNIEXPORT void JNICALL  Java_org_cocos2dx_cpp_AppActivity_CppFunc(JNIEnv *env)
{
...
}

#3

devnoob
thanks for help
its not work. :frowning:


#4

Are you missing a curly closing bracket?

extern "C" {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    JNIEXPORT void JNICALL  Java_org_cocos2dx_cpp_AppActivity_CppFunc(JNIEnv *env)
    {
     ...
    }
#endif
}

#5

its not work. :frowning:
i think RC1 is different pre ver.


#6

Did you add helloworld.cpp in the proj.android/jni/Android.mk file?


#7

lance_gray
Yes. everything is fine.
i can’t call c++ in RC1 ver.


#8

@jamesYou Have you succeeded in calling C++ from java? I have the same issue.

UPDATE:
Solution found. Just in case somebody will have same problem.
I have named the methods in this way

void Java_org_cocos2dx_cpp_AppActivity_methodName

also, in a .h file I have put the method in extern “C” in the file of the class, but outside of the class declaration:

extern "C"
{
    void Java_org_cocos2dx_cpp_AppActivity_methodName(JNIEnv* env, jobject thiz);
};

and in .cpp file just like this:

using namespace cocos2d;

void Java_org_cocos2dx_cpp_AppActivity_methodName(JNIEnv * env, jobject thiz)
{
	CCLOG("Called C++ method!");
}