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

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

问题描述

从使用Qt Creator创建的Android项目的空Qt开始,如何执行本机Java代码?



例如,如果我有这个java方法:

  class HelloJava {
public static String sayHello(int a,int b){
returnHello来自Java,a + b =+(a + b);
}
}



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

  QString call_HelloJava_sayHello b){
//这里有什么?
}

我将Java代码放在我的项目中, call_HelloJava_sayHello()方法?

解决方案

Qt doc 对主题非常详细:

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


// C ++代码

//第一个函数的签名是(I)Ljava / lang / String;
QAndroidJniObject stringNumber = QAndroidJniObject :: callStaticObjectMethod(org / qtproject / qt5 / TestClass,
fromNumber
(I)Ljava / lang / String;,
10 );

//第二个函数的签名是(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>());

所以你的函数的sig应该是(I; I; Ljava / lang / String;



还有:


ANDROID_PACKAGE_SOURCE_DIR:此变量可用于指定
目录,其中可以对默认的
Android软件包模板进行添加和修改。 androiddeployqt工具将
应用程序模板从Qt复制到构建目录,然后
将复制ANDROID_PACKAGE_SOURCE_DIR的内容在
之上,覆盖任何现有的文件。更新步骤,其中部分
源文件被自动修改以反映您的其他
设置然后在生成的合并包上运行。如果你为
实例,想为你的
应用程序做一个自定义的AndroidManifest.xml,然后将它直接放入
这个变量中指定的文件夹。您还可以在
ANDROID_PACKAGE_SOURCE_DIR / src中添加自定义Java文件。



注意:添加自定义版本的构建文件(如
strings.xml ,libs.xml,AndroidManifest.xml等)到你的项目,
确保你从包模板,它位于
$ QT / src / android / java中复制他们。您不应该从build
目录复制任何文件,因为这些文件已经更改为与当前版本
设置相匹配。


所以看来你需要指定并且自动部署java文件。



只需将它添加到你的.pro文件位于 / path / to / project / myjava / src

  android {
ANDROID_PACKAGE_SOURCE_DIR = $$ _ PRO_FILE_PWD_ / android
QT + = androidextras
}

_PRO_FILE_PWD _ 是将解析到项目目录的qmake变量。


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

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);
    }
}

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

Where do I put the Java code in my project, and what goes inside the call_HelloJava_sayHello() method?

解决方案

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>());

So the sig of your function should be "(I;I;)Ljava/lang/String;"

And also this:

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.

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.

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

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_ is a qmake variable that will resolve to the project directory.

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

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