我需要在我的Qt中为Android项目使用Java库和函数,所以我已经开始很简单了。
这是我现在的AESCrypt.java
package org.qcolocrypt;
public class AESCrypt extends org.qtproject.qt5.android.bindings.QtActivity{
public static String getTestString() { return "This is a Test String"; }
}现在,我的Qt项目驻留在Apps/QColoCrypt以及我的cpp和h文件中。在该目录中,我有以下结构:
Apps/QColoCrypt/java/AndroidManifest.xml
Apps/QColoCrypt/java/src/org/qcolocrypt/AESCrypt.java我的.pro文件如下所示:
#-------------------------------------------------
#
# Project created by QtCreator 2017-11-23T06:31:24
#
#-------------------------------------------------
QT += core gui androidextras
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QColoCrypt
TEMPLATE = app
ANDROID_PACKAGE_SOURCE_DIR = $$PWD/java
SOURCES += main.cpp\
mainview.cpp
HEADERS += mainview.h
FORMS += mainview.ui
MOC_DIR = MOCS
OBJECTS_DIR = OBJS
CONFIG += mobility
MOBILITY =
OTHER_FILES += \
java/AndroidManifest.xml \
java/src/org/qcolocrypt/AESCrypt.java以防我的AndroidManifest看起来像这样:
<?xml version="1.0"?>
<manifest package="org.qtproject.example.QColoCrypt" xmlns:android="http://schemas.android.com/apk/res/android" android:versionName="1.0" android:versionCode="1" android:installLocation="auto">
<application android:hardwareAccelerated="true" android:name="org.qtproject.qt5.android.bindings.QtApplication" android:label="QColoCrypt">
<activity android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|layoutDirection|locale|fontScale|keyboard|keyboardHidden|navigation" android:name="org.qtproject.qt5.android.bindings.QtActivity" android:label="QColoCrypt" android:screenOrientation="unspecified" android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<!-- Application arguments -->
<!-- meta-data android:name="android.app.arguments" android:value="arg1 arg2 arg3"/ -->
<!-- Application arguments -->
<meta-data android:name="android.app.lib_name" android:value="QColoCrypt"/>
<meta-data android:name="android.app.qt_sources_resource_id" android:resource="@array/qt_sources"/>
<meta-data android:name="android.app.repository" android:value="default"/>
<meta-data android:name="android.app.qt_libs_resource_id" android:resource="@array/qt_libs"/>
<meta-data android:name="android.app.bundled_libs_resource_id" android:resource="@array/bundled_libs"/>
<!-- Deploy Qt libs as part of package -->
<meta-data android:name="android.app.bundle_local_qt_libs" android:value="1"/>
<meta-data android:name="android.app.bundled_in_lib_resource_id" android:resource="@array/bundled_in_lib"/>
<meta-data android:name="android.app.bundled_in_assets_resource_id" android:resource="@array/bundled_in_assets"/>
<!-- Run with local libs -->
<meta-data android:name="android.app.use_local_qt_libs" android:value="1"/>
<meta-data android:name="android.app.libs_prefix" android:value="/data/local/tmp/qt/"/>
<meta-data android:name="android.app.load_local_libs" android:value="plugins/platforms/android/libqtforandroid.so"/>
<meta-data android:name="android.app.load_local_jars" android:value="jar/QtAndroid.jar:jar/QtAndroid-bundled.jar"/>
<meta-data android:name="android.app.static_init_classes" android:value=""/>
<!-- Messages maps -->
<meta-data android:value="@string/ministro_not_found_msg" android:name="android.app.ministro_not_found_msg"/>
<meta-data android:value="@string/ministro_needed_msg" android:name="android.app.ministro_needed_msg"/>
<meta-data android:value="@string/fatal_error_msg" android:name="android.app.fatal_error_msg"/>
<!-- Messages maps -->
<!-- Splash screen -->
<!-- meta-data android:name="android.app.splash_screen_drawable" android:resource="@drawable/logo"/ -->
<!-- meta-data android:name="android.app.splash_screen_sticky" android:value="true"/ -->
<!-- Splash screen -->
<!-- Background running -->
<!-- Warning: changing this value to true may cause unexpected crashes if the
application still try to draw after
"applicationStateChanged(Qt::ApplicationSuspended)"
signal is sent! -->
<meta-data android:name="android.app.background_running" android:value="false"/>
<!-- Background running -->
<!-- auto screen scale factor -->
<meta-data android:name="android.app.auto_screen_scale_factor" android:value="false"/>
<!-- auto screen scale factor -->
<!-- extract android style -->
<!-- available android:values :
* full - useful QWidget & Quick Controls 1 apps
* minimal - useful for Quick Controls 2 apps, it is much faster than "full"
* none - useful for apps that don't use any of the above Qt modules
-->
<meta-data android:name="android.app.extract_android_style" android:value="full"/>
<!-- extract android style -->
</activity>
<!-- For adding service(s) please check: https://wiki.qt.io/AndroidServices -->
</application>
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="16"/>
<supports-screens android:largeScreens="true" android:normalScreens="true" android:anyDensity="true" android:smallScreens="true"/>
<!-- The following comment will be replaced upon deployment with default permissions based on the dependencies of the application.
Remove the comment if you do not require these default permissions. -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- The following comment will be replaced upon deployment with default features based on the dependencies of the application.
Remove the comment if you do not require these default features. -->
</manifest>编译项目时,生成的所有内容都会进入目录。应用程序/00AndroidBuilds/QColoCrypt
最后,在我的mainview.cpp (只有cpp文件,Qt的main.cpp除外)中,我在一个按钮单击下面的代码:
void MainView::on_pushButton_clicked()
{
QAndroidJniObject test = QAndroidJniObject::callStaticObjectMethod("org/qcolocrypt/AESCrypt",
"getTestString",
"(V)Ljava/lang/String;");
ui->pteLog->appendPlainText("==========TEST STRING================");
ui->pteLog->appendPlainText(test.toString());
}据我所知,当一切正常工作时,我应该打印“这是一个测试字符串”。但是,test.toString()函数似乎返回一个空字符串。
所以,要么是编译错误(这就是为什么我添加了所有我认为有针对性的路径信息),要么是我不正确地调用函数,而我不知道自己做错了什么。
目前使用QT5.7.0,以防万一这很重要。
发布于 2017-11-24 10:35:11
由于您正在调用不接受参数的Java函数,因此签名应该是:
"()Ljava/lang/String;"V for void仅用于当函数不返回任何内容时。
https://stackoverflow.com/questions/47470884
复制相似问题