Qt6 CMake install java files
-
I am currently in the process of porting my app from Qt5.15.17 to Qt6.9.
My project setup contains a root project with the standard android files like the AndroidManifest.xml, build.gradle and some .java sources for Activity and Application
but also multiple subproject that are separate git submodules with their own .java files.Previously in QMake I was able to collect the needed files and using
INSTALL
to deploy them to my build folder like# add java files to file explorer JAVA_FILES = $$files($$system_path($$PWD/android/*.java)) nativeJavaFilesCopy.files = $$JAVA_FILES nativeJavaFilesCopy.path = /java/com/donkeycat/dcnative INSTALLS += nativeJavaFilesCopy
but now after switching to CMake I am having troubles integrating the .java files of my subprojects in my build :(
I am aware of the
QT_ANDROID_PACKAGE_SOURCE_DIR
property which I set to my root projects android folderset_property(TARGET ${PROJECT_NAME} PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR ${CMAKE_SOURCE_DIR}/android )
but as I understand this only supports one location.
How can I tell CMake to also include .java files from other directories?I have tried just adding them as sources:
file(GLOB_RECURSE JAVA_SOURCES "${SUBMODULE_JAVA_DIR}/*.java") # add as sources target_sources(${PROJECT_NAME} PUBLIC ${JAVA_SOURCES})
but also more complex commands that should copy those files in the build folder:
# Source and destination paths set(SUBMODULE_JAVA_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android") set(BUILD_ANDROID_JAVA_DIR "${CMAKE_BINARY_DIR}/android-build-${ROOT_PROJECT}/src/com/donkeycat/dcnative") file(GLOB_RECURSE JAVA_SOURCES "${SUBMODULE_JAVA_DIR}/*.java") # Create a custom command to copy them add_custom_command( OUTPUT ${BUILD_ANDROID_JAVA_DIR}/.copied # Dummy output marker COMMAND ${CMAKE_COMMAND} -E make_directory ${BUILD_ANDROID_JAVA_DIR} COMMAND ${CMAKE_COMMAND} -E copy_if_different ${JAVA_SOURCES} ${BUILD_ANDROID_JAVA_DIR} COMMAND ${CMAKE_COMMAND} -E touch ${BUILD_ANDROID_JAVA_DIR}/.copied DEPENDS ${JAVA_SOURCES} COMMENT "Copying .java files from native plugin to android build dir" ) # Add a target that depends on it add_custom_target(copy_java ALL DEPENDS ${BUILD_ANDROID_JAVA_DIR}/.copied ) # Ensure your app depends on the copied Java files add_dependencies(${PROJECT_NAME} copy_java)
which essentially does what it should and copies the files to the build folder, but as soon as compilation starts the whole "${CMAKE_BINARY_DIR}/android-build-${ROOT_PROJECT}/src/" folder gets purged and only the jav source files from QT_ANDROID_PACKAGE_SOURCE_DIR directory are available :(
So I am searching for another way of including more .java source files in my build,
is there a "follow-up" for INSTALL in CMake? Or other ways to include my files? -
I managed to come up with a workaround (which I would like to have only temporary xD)
Instead of trying to compile my additional subproject .java files directly to the build folder,
I can move it to QT_ANDROID_PACKAGE_SOURCE_DIR directory -> where they then get copied to the build folder autmatically.This essentially works but I do not want those files to be present in my root project dir so I
- added those to my .gitignore
- tried to add a custom command after build to delete those files:
For each folder I add a variable appends the given path and in my root project's CMake I have
if (ANDROID) # Collect all registered plugin folders and clean after build after linking subdirectories get_property(ALL_JAVA_PLUGIN_DIRS GLOBAL PROPERTY JAVA_CLEANUP_DIRS) message(STATUS "ALL_JAVA_PLUGIN_DIRS: ${ALL_JAVA_PLUGIN_DIRS}") foreach(DIR_TO_DELETE ${ALL_JAVA_PLUGIN_DIRS}) message(STATUS "Adding custom command for deleting: ${DIR_TO_DELETE}") add_custom_command( TARGET ${ROOT_PROJECT} POST_BUILD COMMAND ${CMAKE_COMMAND} -E echo "Deleting: ${DIR_TO_DELETE}" COMMAND ${CMAKE_COMMAND} -E remove_directory ${DIR_TO_DELETE} COMMENT "Cleaning up plugin java folder after build: ${DIR_TO_DELETE}" ) endforeach() endif ()
Sadly this does not really work reliable :/
Every time after a successful build (when I think this should be triggered) I see neither the echo command, nor the comment anywhere in the logs and the files still remain,
but on the other hand, sometimes in between it seems that the files get purged thus making my build fail xDIs anyone aware of another approach / solution to this?
Am I the only one having this problem, maybe I have something wonky with my project setup? :-sEssentially I am just searching for successor of "INSTALLS" for QMake which I can apply to CMake