Qt + Android:不同项目共有的java文件的位置

时间:2014-09-19 08:25:00

标签: android qt

在Qt for Android中,您的项目中包含了java文件。该位置使用项目文件中的变量ANDROID_PACKAGE_SOURCE_DIR进行配置。

该位置还包含特定于项目的其他文件(资源等)。

但是如果这些java文件对于不同的项目是通用的,那么你应该有单独的副本,每个项目中有一个副本在ANDROID_PACKAGE_SOURCE_DIR中

我的问题是,是否有人知道如何指定独立于项目位置的java文件目录。

1 个答案:

答案 0 :(得分:1)

我仍然相当擅长在Qt中开发Android应用程序,但最近我开始了这个旅程并遇到了你遇到的完全相同的问题;我写了一些常见的C ++ / Java代码,我想在几个不同的应用程序之间共享。 C ++代码很简单(共享库)但是当你说Java代码时,我不想在每个项目目录中都有相同Java代码的副本。

花了一点时间,但我找到了一种方法,这很容易做到。

在编译/链接C ++代码之后编译Qt Android项目时,会运行编写Java代码的ANT脚本(因此您只需要在src文件夹中粘贴Java源文件而不是编译JAR文件)。

ANT脚本是Android SDK的一部分,默认情况下会在src文件夹中查找并编译在那里找到的任何Java文件。因此,您真正需要确保的是,在运行ANT脚本之前,您要用作应用程序一部分的所有Java文件都位于该文件夹中。

ANDROID_PACKAGE_SOURCE_DIR变量告诉qMake在哪里找到需要复制到android-build文件夹的文件(包括任何项目特定的Java代码),但我们想要的是在执行ANT脚本之前运行一段时间的自定义目标手动将我们的常见Java文件复制到src文件夹,以便它们也可以编译到应用程序中。

要在我的* .pro文件中执行此操作,我添加了以下内容:

# This line makes sure my custom manifest file and project specific java code is copied to the android-build folder
ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android

# This is a custom variable which holds the path to my common Java code
# I use the $$system_path() qMake function to make sure that my directory separators are correct for the platform as you need to use the correct separator in the Make file (i.e. \ for Windows and / for Linux)
commonAndroidFilesPath = $$system_path( $$PWD/../CommonLib/android-sources/src )

# This is a custom variable which holds the path to the src folder in the output directory. That is where they need to go for the ANT script to compile them.
androidBuildOutputDir = $$system_path( $$OUT_PWD/../android-build/src )

# Here is the magic, this is the actual copy command I want to run.
# Make has a platform agnostic copy command macro you can use which substitutes the correct copy command for the platform you are on: $(COPY_DIR)
copyCommonJavaFiles.commands = $(COPY_DIR) $${commonAndroidFilesPath} $${androidBuildOutputDir}

# I tack it on to the first target which exists by default just because I know this will happen before the ANT script gets run.
first.depends = $(first) copyCommonJavaFiles
export(first.depends)
export(copyCommonJavaFiles.commands)
QMAKE_EXTRA_TARGETS += first copyCommonJavaFiles

当您运行qMake时,生成的Make文件将具有以下内容:

first: $(first) copyCommonJavaFiles

copyCommonJavaFiles:
    $(COPY_DIR) C:\Users\bvanderlaan\Documents\GitHub\MyProject\MyProjectApp\..\CommonLib\android-sources\src C:\Users\bvanderlaan\Documents\GitHub\build-MyProject-Android_for_armeabi_v7a_GCC_4_9_Qt_5_4_1-Debug\MyProjectApp\..\android-build\src

所以现在当我构建我的公共C ++作为共享库链接时,我的常见Java代码被复制到src目录,然后ANT脚本编译所有Java代码并使用任何项目特定的Java代码复制到我的应用程序中我可能有。这样就不需要在我的源代码树中将Java文件的副本保存在多个位置,或者需要外部构建脚本/工具来编译我的工作空间。

我希望这能为你提出问题并为你效劳。

直到下一次富有想象力地思考并创造性地设计

相关问题