Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to call a static method of java from c++ side?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Mobile and Embedded
3 Posts 2 Posters 3.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    stereomatching
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      stereomatching
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mobi
        wrote on last edited by
        #3

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

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

        return str.toString();
        

        }@

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved