我需要在我的Qt for 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());
}
据我了解,当一切正常时,我应该得到&#34;这是一个测试字符串&#34;打印。但是test.toString()函数似乎返回一个空字符串。
所以有些东西要么编译错误(这就是为什么我添加了所有关于路径的信息,我认为是有针对性的)或者我错误地调用了这个函数而且我不知道我在做什么错。
目前正在使用Qt 5.7.0,以防重要。
答案 0 :(得分:0)
由于您正在调用不带参数的Java函数,因此签名应为:
"()Ljava/lang/String;"
V
void
仅用于函数不返回任何内容时。