Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cann't call Java method from Qt side...
Qt 6.11 is out! See what's new in the release blog

Cann't call Java method from Qt side...

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 5.5k Views 2 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.
  • Taz742T Offline
    Taz742T Offline
    Taz742
    wrote on last edited by Taz742
    #1

    Hi Guys!!
    I wrote some java method, which always returns number 7, but when I call this method from c++ it returns 0..
    where I have to create this file can you tell me??
    because I think that QAndroidJniObject::callstaticmethod<jint>(Java_File_Path, "MethodName", "Signature");
    here Java_File_Path is incorrect.

    raven-worxR 1 Reply Last reply
    -1
    • Taz742T Taz742

      Hi Guys!!
      I wrote some java method, which always returns number 7, but when I call this method from c++ it returns 0..
      where I have to create this file can you tell me??
      because I think that QAndroidJniObject::callstaticmethod<jint>(Java_File_Path, "MethodName", "Signature");
      here Java_File_Path is incorrect.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Taz742
      you need to be a little more specific.
      Show the Java code and the exact C++ code.
      Are you really using the filepath on the disk in Java_File_Path ?!?!

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      Taz742T 2 Replies Last reply
      0
      • raven-worxR raven-worx

        @Taz742
        you need to be a little more specific.
        Show the Java code and the exact C++ code.
        Are you really using the filepath on the disk in Java_File_Path ?!?!

        Taz742T Offline
        Taz742T Offline
        Taz742
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • raven-worxR raven-worx

          @Taz742
          you need to be a little more specific.
          Show the Java code and the exact C++ code.
          Are you really using the filepath on the disk in Java_File_Path ?!?!

          Taz742T Offline
          Taz742T Offline
          Taz742
          wrote on last edited by
          #4

          @raven-worx
          alt text
          alt text

          raven-worxR 1 Reply Last reply
          -1
          • Taz742T Taz742

            @raven-worx
            alt text
            alt text

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by raven-worx
            #5

            @Taz742

            1. is this really valid Java code?
            2. in your QAnadroidJNI call you specified to call a method with int parameter and int return value. Your java code doesn't have any parameters?!
            3. are you running this code on desktop machine or in an Android environment?!
            4. The java file needs to be bundled into your Android APK. Thus it needs to be compiled and be able to be found in the classpath to call it via the JVM runtime. But i don't see any android related files in your project?

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            2
            • Pablo J. RoginaP Offline
              Pablo J. RoginaP Offline
              Pablo J. Rogina
              wrote on last edited by
              #6

              @Taz742 to add to @raven-worx reply, it looks like your Java code won't compile, you're missing (); and the path to the Java class in C++ code shouldn't include "src"

              Please take a look at this presentation from the creator of Qt's port to Android, the use case 1 is all you need.

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • I Offline
                I Offline
                Ibrahim
                wrote on last edited by
                #7

                @Taz742
                Qt Android Extras module provides QAndroidJniObject class. Also Qt Android Extras module for just Android applications, not desktop applications.
                You add this command into app.pro file:

                android {
                  QT += androidextras
                }
                

                You must to add android template. So, you go to Projects -> choose Android x86 or Armv7 -> Build Android APK -> Click Create Templates button. You must to add java file int src like this:
                alt text
                You can try this code (I wrote this code in mainwindow.cpp):

                #include "mainwindow.h"
                #include "ui_mainwindow.h"
                
                #include <QAndroidJniObject>
                #include <QDebug>
                
                MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow)
                {
                  ui->setupUi(this);
                
                  qDebug() << QAndroidJniObject::callStaticMethod<jint>(
                                  "com/example/Example",
                                  "fooMethod",
                                  "()I");
                }
                
                MainWindow::~MainWindow()
                {
                  delete ui;
                }
                
                Taz742T 3 Replies Last reply
                0
                • I Ibrahim

                  @Taz742
                  Qt Android Extras module provides QAndroidJniObject class. Also Qt Android Extras module for just Android applications, not desktop applications.
                  You add this command into app.pro file:

                  android {
                    QT += androidextras
                  }
                  

                  You must to add android template. So, you go to Projects -> choose Android x86 or Armv7 -> Build Android APK -> Click Create Templates button. You must to add java file int src like this:
                  alt text
                  You can try this code (I wrote this code in mainwindow.cpp):

                  #include "mainwindow.h"
                  #include "ui_mainwindow.h"
                  
                  #include <QAndroidJniObject>
                  #include <QDebug>
                  
                  MainWindow::MainWindow(QWidget *parent) :
                    QMainWindow(parent),
                    ui(new Ui::MainWindow)
                  {
                    ui->setupUi(this);
                  
                    qDebug() << QAndroidJniObject::callStaticMethod<jint>(
                                    "com/example/Example",
                                    "fooMethod",
                                    "()I");
                  }
                  
                  MainWindow::~MainWindow()
                  {
                    delete ui;
                  }
                  
                  Taz742T Offline
                  Taz742T Offline
                  Taz742
                  wrote on last edited by
                  #8

                  @Ibrahim
                  your comment looks like helpful. Can you give me your project please?

                  1 Reply Last reply
                  -1
                  • I Ibrahim

                    @Taz742
                    Qt Android Extras module provides QAndroidJniObject class. Also Qt Android Extras module for just Android applications, not desktop applications.
                    You add this command into app.pro file:

                    android {
                      QT += androidextras
                    }
                    

                    You must to add android template. So, you go to Projects -> choose Android x86 or Armv7 -> Build Android APK -> Click Create Templates button. You must to add java file int src like this:
                    alt text
                    You can try this code (I wrote this code in mainwindow.cpp):

                    #include "mainwindow.h"
                    #include "ui_mainwindow.h"
                    
                    #include <QAndroidJniObject>
                    #include <QDebug>
                    
                    MainWindow::MainWindow(QWidget *parent) :
                      QMainWindow(parent),
                      ui(new Ui::MainWindow)
                    {
                      ui->setupUi(this);
                    
                      qDebug() << QAndroidJniObject::callStaticMethod<jint>(
                                      "com/example/Example",
                                      "fooMethod",
                                      "()I");
                    }
                    
                    MainWindow::~MainWindow()
                    {
                      delete ui;
                    }
                    
                    Taz742T Offline
                    Taz742T Offline
                    Taz742
                    wrote on last edited by Taz742
                    #9

                    @Ibrahim
                    Did your program print 7?

                    1 Reply Last reply
                    -1
                    • I Ibrahim

                      @Taz742
                      Qt Android Extras module provides QAndroidJniObject class. Also Qt Android Extras module for just Android applications, not desktop applications.
                      You add this command into app.pro file:

                      android {
                        QT += androidextras
                      }
                      

                      You must to add android template. So, you go to Projects -> choose Android x86 or Armv7 -> Build Android APK -> Click Create Templates button. You must to add java file int src like this:
                      alt text
                      You can try this code (I wrote this code in mainwindow.cpp):

                      #include "mainwindow.h"
                      #include "ui_mainwindow.h"
                      
                      #include <QAndroidJniObject>
                      #include <QDebug>
                      
                      MainWindow::MainWindow(QWidget *parent) :
                        QMainWindow(parent),
                        ui(new Ui::MainWindow)
                      {
                        ui->setupUi(this);
                      
                        qDebug() << QAndroidJniObject::callStaticMethod<jint>(
                                        "com/example/Example",
                                        "fooMethod",
                                        "()I");
                      }
                      
                      MainWindow::~MainWindow()
                      {
                        delete ui;
                      }
                      
                      Taz742T Offline
                      Taz742T Offline
                      Taz742
                      wrote on last edited by
                      #10
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • Taz742T Offline
                        Taz742T Offline
                        Taz742
                        wrote on last edited by Taz742
                        #11

                        Answer is:
                        I wrote into AndroidManifest.xml <manifest package="com.leri" and also I added in java file -> package com.leri;
                        Thanks..

                        1 Reply Last reply
                        -1

                        • Login

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