How to call a static method of java from c++ side?



  • simpleJNI.hpp

    @#include <QObject>

    class simpleJNI : public QObject
    {
    Q_OBJECT
    public:
    explicit simpleJNI(QObject *parent = nullptr);

    Q_INVOKABLE QString printHelloWorld();
    

    };@

    simpleJNI.cpp

    @
    #include "simpleJNI.hpp"

    #include <QDebug>
    #include <QtAndroidExtras/QAndroidJniObject>

    simpleJNI::simpleJNI(QObject *parent) :
    QObject(parent)
    {
    }

    QString simpleJNI::printHelloWorld()
    {
    QAndroidJniObject str = QAndroidJniObject::callStaticObjectMethod("adv/appBucksAdv",
    "getString",
    "(V)Ljava/lang/String;"
    );

    return str.toString();
    

    }
    @

    appBucksAdv.java
    @package adv;

    import org.qtproject.qt5.android.bindings.QtApplication;
    import org.qtproject.qt5.android.bindings.QtActivity;
    import android.content.Context;

    public class appBucksAdv extends QtActivity
    {
    public static String getString()
    {
    return "hello world";
    }
    }
    @

    main.qml

    @
    import QtQuick 2.2
    import QtQuick.Controls 1.1

    Rectangle {
    width: 100
    height: 62

    color: "red"
    
    Text{
        id: text
        height: 30
        width: parent.width
        anchors.centerIn: parent
    }
    
    MouseArea{
        anchors.fill: parent
    
        onClicked: {
            text.text = text.text + simpleJNI.printHelloWorld() + "\n"
        }
    }
    

    }
    @

    But the program do not print "helloWorld\n"

    message :
    W/dalvikvm( 6727): Bogus method descriptor: (V)Ljava/lang/String;

    "Layout of the projects":https://github.com/stereomatchingkiss/blogCodes2/tree/master/androidTest



  • If I change the signature of the function to

    @public static String printHelloWorld(int a)
    {
    return "hello world";
    }@

    and simpleJNI.cpp to

    @QString simpleJNI::printHelloWorld()
    {
    QAndroidJniObject str = QAndroidJniObject::callStaticObjectMethod("adv/appBucksAdv",
    "printHelloWorld",
    //change (V) to (I)
    "(I)Ljava/lang/String;",
    23);

    return str.toString();
    

    }
    @

    The codes work



  • try "()Ljava/lang/String;" or

    @QString simpleJNI::printHelloWorld()
    {
    QAndroidJniObject str = QAndroidJniObject::callStaticObjectMethod<jstring>("adv/appBucksAdv",
    "printHelloWorld");

    return str.toString();
    

    }@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.