简体   繁体   中英

Calling java function from c++ using jni: Failed to find static method id

I'm creating cocos2d-x game (v. 3.4) on mac os using android sdk 4.2.2, but I tried other too. I want to make NativeHelper class to call some native android stuff from c++. I used this tutorial as the base: http://www.cocos2d-x.org/wiki/User_Tutorial-Integrate_AdMob It's working, but I want use a java function with parameters. And there comes errors:

02-13 09:33:20.690: W/dalvikvm(28873): Bogus method descriptor: (I;)V 02-13 09:33:20.690: E/JniHelper(28873): Failed to find static method id of showBanner

Here's java implementation:

public static void showBanner(int position) {
        final int _position = position;
        _appActivity.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                if(_appActivity != null){
                    if (!_appActivity.adView.isEnabled()){
                        _appActivity.adView.setEnabled(true);
                    }
                    if (_appActivity.adView.getVisibility() == View.INVISIBLE){
                        _appActivity.adView.setVisibility(View.VISIBLE);
                        RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                        if(_position == 0){
                            adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                        }
                        else{
                            adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);                           
                        }
                        _appActivity.adView.setLayoutParams(adParams);
                    }
                }
            }
        });
    }

c++ implementation:

void NativeHelper::showBanner(int position){
    cocos2d::JniMethodInfo t;
    if (cocos2d::JniHelper::getStaticMethodInfo(t, AppActivityClassName, "showBanner", "(I;)V")){

        t.env->CallStaticVoidMethod(t.classID, t.methodID, position);

        t.env->DeleteLocalRef(t.classID);

        isBannerShowing = true;
    }
}

If I just remove position parameter from both function (and change (I;)V to ()V ) it's working like a charm. I tried other parameter types like bool and it also doesn't work.

I thought I maybe did something wrong so I found this tutorial: http://stnguyen.com/cocos2d-x/call-java-functions-from-cpp.html

And calling for example sayHello also doesn't work too:

02-13 09:33:16.955: W/dalvikvm(28873): Bogus method descriptor: (Ljava/lang/String;I;)V 02-13 09:33:16.955: E/JniHelper(28873): Failed to find static method id of sayHello

I'm using ndk r9, but tried r10 too. I'm basically out of ideas...

You mistyped. It should be "(I)V", not "(I;)V"

You can just open the compiled .class file, and search the method name. The compiled file will be a hex file, but method name and parameter is text. see Sample

public void InitUserData(int sdk_app_id, String account_type, String 
app_id_3rd, String identifier, String user_sig)

=======>

InitUserDataL(ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V

Use javap is also ok, but open the .class file is more convenient.

For me a double myfunction(java.lang.String astringparam) {} needed (Ljava/lang/String;)D . (Ljava/lang/String)D without the semicolon did not work.

BTW I used "javap -s -classpath bin/classes/org/cocos2dx/javascript/ AppActivity" on the command line to get the signature. (Thanks to @EJP)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM