任何人都可以帮助解决这个问题吗,我正在为 android 创建一个应用程序,我需要从 Qt C++ 调用 java 函数,我正在使用以下信息进行测试。
请参阅下面的 CMakeLists.txt 文件。
cmake_minimum_required(VERSION 3.16)
project(testing VERSION 0.1 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt6 6.5 REQUIRED COMPONENTS Quick Widgets Core)
qt_standard_project_setup(REQUIRES 6.5)
qt_add_executable(apptesting
main.cpp
)
qt_add_qml_module(apptesting
URI testing
VERSION 1.0
QML_FILES
Main.qml
RESOURCES
android/AndroidManifest.xml
android/build.gradle
android/res/values/libs.xml
android/res/xml/qtprovider_paths.xml
android/gradle/wrapper/gradle-wrapper.jar
android/gradle/wrapper/gradle-wrapper.properties
android/gradle.properties
android/gradlew
android/gradlew.bat
android/src/org/qtproject/example/file/MathOperations.java
SOURCES
mathbridge.h mathbridge.cpp
)
# Set Android source directory
set(android_package_dir "${CMAKE_SOURCE_DIR}/android")
# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
set_target_properties(apptesting PROPERTIES
# MACOSX_BUNDLE_GUI_IDENTIFIER com.example.apptesting
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
)
target_link_libraries(apptesting
PRIVATE Qt6::Quick
PRIVATE Qt6::Widgets
PRIVATE Qt6::Core
)
include(GNUInstallDirs)
install(TARGETS apptesting
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
Java 文件
package org.qtproject.example.file;
import android.app.Activity;
import android.widget.Toast;
import org.qtproject.qt.android.QtNative;
public class MathOperations {
public static String add() {
return "This is a test for addition";
}
public static String multiply() {
return "This is a test for multiplication";
}
}
下面是我的Qt C++实现代码
#include "mathbridge.h"
#include <QJniObject>
#include <QDebug>
QString MathBridge::addNumbers() {
QJniObject result = QJniObject::callStaticObjectMethod(
"org/qtproject/example/file/MathOperations", // Fully qualified Java class name
"add", // Java method name
"()Ljava/lang/String;" // JNI method signature
);
if (result.isValid()) {
QString value = result.toString();
qDebug() << "Addition Result:" << value;
return value;
} else {
qDebug() << "Failed to call add() in Java";
return "Error";
}
}
QString MathBridge::multiplyNumbers() {
QJniObject result = QJniObject::callStaticObjectMethod(
"org/qtproject/example/file/MathOperations", // Fully qualified Java class name
"multiply", // Java method name
"()Ljava/lang/String;" // JNI method signature
);
if (result.isValid()) {
QString value = result.toString();
qDebug() << "Multiplication Result:" << value;
return value;
} else {
qDebug() << "Failed to call multiply() in Java";
return "Error";
}
}
我收到错误消息说没有找到类“org.qtproject.example.file.MathOperations”
请查看下面的错误
W libapptesting_x86_64.so: java.lang.ClassNotFoundException: Didn't find class "org.qtproject.example.file.MathOperations" on path: DexPathList[[zip file "/data/app/~~8CDaDhFP1VCP5LeUpwjwoQ==/org.qtproject.example.apptesting-Ltr2ET7MHeEnfYKGtla3Zw==/base.apk"],nativeLibraryDirectories=[/system/lib64, /system_ext/lib64]]
W libapptesting_x86_64.so: at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:259)
W libapptesting_x86_64.so: at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
W libapptesting_x86_64.so: at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
W libapptesting_x86_64.so: at org.qtproject.qt.android.QtNative.startQtApplication(Native Method)
W libapptesting_x86_64.so: at org.qtproject.qt.android.QtNative$$ExternalSyntheticLambda4.run(D8$$SyntheticClass:0)
W libapptesting_x86_64.so: at org.qtproject.qt.android.QtThread$1.run(QtThread.java:25)
W libapptesting_x86_64.so: at java.lang.Thread.run(Thread.java:1012)
11-25 20:46:31.682 12976 13041 W libapptesting_x86_64.so:
D libapptesting_x86_64.so: Failed to call add() in Java
W libapptesting_x86_64.so: java.lang.ClassNotFoundException: Didn't find class "org.qtproject.example.file.MathOperations" on path: DexPathList[[zip file "/data/app/~~8CDaDhFP1VCP5LeUpwjwoQ==/org.qtproject.example.apptesting-Ltr2ET7MHeEnfYKGtla3Zw==/base.apk"],nativeLibraryDirectories=[/system/lib64, /system_ext/lib64]]
W libapptesting_x86_64.so: at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:259)
W libapptesting_x86_64.so: at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
W libapptesting_x86_64.so: at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
W libapptesting_x86_64.so: at org.qtproject.qt.android.QtNative.startQtApplication(Native Method)
W libapptesting_x86_64.so: at org.qtproject.qt.android.QtNative$$ExternalSyntheticLambda4.run(D8$$SyntheticClass:0)
W libapptesting_x86_64.so: at org.qtproject.qt.android.QtThread$1.run(QtThread.java:25)
W libapptesting_x86_64.so: at java.lang.Thread.run(Thread.java:1012)
11-25 20:46:31.713 12976 13041 W libapptesting_x86_64.so:
D libapptesting_x86_64.so: Failed to call multiply() in Java
阅读了一些帖子后,我知道 android/src/org/qtproject/example/file/MathOperations.java 目录不应该位于 CMakeLists.txt 中的 RESOURCES 中。
但是我没有将此目录添加到资源中,当我使用 QT Creator 中的“添加现有目录..”将子目录添加到 android 内部时,该子目录会自行添加到资源中。
当我尝试从 CMakeLists.txt 中的资源中删除时,该子目录将从 Qt Creator 中的 android 文件夹中删除,拥有自己的子目录。
有人可以就我的以下疑问提出建议吗?
应该将android/src/org/qtproject/example/file/MathOperations.java添加到QT Creator的android目录中吗?
如果要添加,那么如何避免它被添加到CMakeLists.txt中的RESOURCES中?
如果不应该添加它,那么我如何以及在哪里指定 CMakeLists.txt 或 build.gradle 来编译并添加到 Android 应用程序?
我的CMakeLists.txt是错误的吗,这就是为什么java文件没有被编译并且没有添加到android应用程序中的原因?
我们将非常感谢您的帮助。
谢谢,