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

  • 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.

  • Moderators

    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 ?!?!

  • This post is deleted!

  • Moderators


    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?

  • @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.

  • @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 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) :
      ui(new Ui::MainWindow)
      qDebug() << QAndroidJniObject::callStaticMethod<jint>(
      delete ui;

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

  • @Ibrahim
    Did your program print 7?

  • This post is deleted!

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

Log in to reply

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