如何使用QAndroidJniObject从内部的Qt调用Java code? [英] How to use QAndroidJniObject to call Java code from inside Qt?

查看:552
本文介绍了如何使用QAndroidJniObject从内部的Qt调用Java code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个空的Qt为Android项目,Qt Creator的创建开始,我怎么执行本地Java code?

Starting from an empty Qt for Android project, created with Qt Creator, how do I execute native Java code?

例如,如果我有这样的Java方法:

For example, if I have this java method:

class HelloJava {
    public static String sayHello(int a, int b) {
        return "Hello from Java, a+b=" + (a+b);
    }
}

和我有这个Qt的方法,应该调用 HelloJava.sayHello()方法:

And I have this Qt method which should call the HelloJava.sayHello() method:

QString call_HelloJava_sayHello(int a, int b) {
    // What goes in here?
}

我在哪里可以把Java的code在我的项目,而所发生的 call_HelloJava_sayHello()方法内?

推荐答案

借助的Qt DOC 是pretty关于这个问题的详细:

The Qt doc is pretty verbose on the subject:

// Java class
package org.qtproject.qt5;
class TestClass
{
   static String fromNumber(int x) { ... }
   static String[] stringArray(String s1, String s2) { ... }
}


// C++ code

// The signature for the first function is "(I)Ljava/lang/String;"
QAndroidJniObject stringNumber = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/TestClass",
                                                                           "fromNumber"
                                                                           "(I)Ljava/lang/String;",
                                                                           10);

// the signature for the second function is "(Ljava/lang/String;Ljava/lang/String;)[Ljava/lang/String;"
QAndroidJniObject string1 = QAndroidJniObject::fromString("String1");
QAndroidJniObject string2 = QAndroidJniObject::fromString("String2");
QAndroidJniObject stringArray = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/TestClass",
                                                                          "stringArray"
                                                                          "(Ljava/lang/String;Ljava/lang/String;)[Ljava/lang/String;"
                                                                           string1.object<jstring>(),
                                                                           string2.object<jstring>());

所以你的函数的签名应(我;我;)Ljava /朗/字符串;

和也是这个:

ANDROID_PACKAGE_SOURCE_DIR:此变量可用于指定一个
  目录,其中添加和修改可以对默认制成
  Android包模板。该androiddeployqt工具将复制
  从Qt应用程序模板到生成目录,然后将其
  将复制的顶部上的ANDROID_PACKAGE_SOURCE_DIR的内容
  对此,覆盖所有现有文件。更新步骤中的一部分
  源文件会自动修改,以反映你的其他
  设置然后对合并后的包上运行。如果,对于
  例如,要打一个自定义的Andr​​oidManifest.xml为您
  应用程序,然后把这个直接进入指定的文件夹
  此变量。您还可以添加自定义的Java文件
  ANDROID_PACKAGE_SOURCE_DIR / src目录。

ANDROID_PACKAGE_SOURCE_DIR: This variable can be used to specify a directory where additions and modifications can be made to the default Android package template. The androiddeployqt tool will copy the application template from Qt into the build directory, and then it will copy the contents of the ANDROID_PACKAGE_SOURCE_DIR on top of this, overwriting any existing files. The update step where parts of the source files are modified automatically to reflect your other settings is then run on the resulting merged package. If you, for instance, want to make a custom AndroidManifest.xml for your application, then place this directly into the folder specified in this variable. You can also add custom Java files in ANDROID_PACKAGE_SOURCE_DIR/src.

请注意:当添加构建文件的自定义版本(如
  strings.xml中,libs.xml,AndroidManifest.xml中,等)到您的项目,
  确保你从包模板,它位于它们复制
  $ QT / src目录/安卓/ JAVA。你不应该从构建复制任何文件
  目录,这些文件已被修改,以符合当前的构建
  设置。

Note: When adding custom versions of the build files (like strings.xml, libs.xml, AndroidManifest.xml, etc.) to your project, make sure you copy them from the package template, which is located in $QT/src/android/java. You should never copy any files from the build directory, as these files have been altered to match the current build settings.

因此​​,看来你需要指定和Java文件会自动部署。

So it seems you need to specify that and the java files are automatically deployed.

只需添加到您的.pro文件(假设你的Java源代码放置在 /路径/要/项目/米亚瓦/ src目录

Just add this to your .pro file (assuming your java sources are placed at /path/to/project/myjava/src:

android {
    ANDROID_PACKAGE_SOURCE_DIR=$$_PRO_FILE_PWD_/android
    QT += androidextras
}

_PRO_FILE_PWD _ 是一个qmake的变量,将解决项目目录。

_PRO_FILE_PWD_ is a qmake variable that will resolve to the project directory.

这篇关于如何使用QAndroidJniObject从内部的Qt调用Java code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆