Add proper dependencies to apk targets

Before, building ${target}_make_apk always re-built the apk, instead of
rebuilding the apk only when inputs changed. This patch fixes that by
moving the creation code from a custom target to a custom command with
proper dependencies.

The androidtestrunner tool now does not check for the existence of an
apk anymore and always runs the make command that is supposed to build
the apk.

The ${target}_prepare_apk_dir target is not needed anymore by the Qt
build but is still used by Qt Creator's Android support. Add a
clarifying comment.

Fixes: QTBUG-93431
Change-Id: I00d65d616fef9511b03b65f879c4bc6cb92dfc30
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
This commit is contained in:
Joerg Bornemann 2021-05-11 13:49:08 +02:00
parent 50c838d4b5
commit 76eefab088
2 changed files with 44 additions and 34 deletions

View File

@ -288,35 +288,48 @@ function(qt6_android_add_apk_target target)
if(NOT QT_NO_GLOBAL_APK_TARGET) if(NOT QT_NO_GLOBAL_APK_TARGET)
if(NOT TARGET apk) if(NOT TARGET apk)
add_custom_target(apk add_custom_target(apk
DEPENDS ${target}_prepare_apk_dir DEPENDS ${target}_make_apk
COMMENT "Building all apks" COMMENT "Building all apks"
) )
endif() endif()
set(should_add_to_global_apk TRUE)
endif() endif()
set(deployment_tool "${QT_HOST_PATH}/${QT6_HOST_INFO_BINDIR}/androiddeployqt") set(deployment_tool "${QT_HOST_PATH}/${QT6_HOST_INFO_BINDIR}/androiddeployqt")
set(apk_dir "$<TARGET_PROPERTY:${target},BINARY_DIR>/android-build") set(apk_final_dir "$<TARGET_PROPERTY:${target},BINARY_DIR>/android-build")
set(apk_intermediate_dir "${CMAKE_CURRENT_BINARY_DIR}/android-build")
set(apk_file_name "${target}.apk")
set(apk_final_file_path "${apk_final_dir}/${apk_file_name}")
set(apk_intermediate_file_path "${apk_intermediate_dir}/${apk_file_name}")
# This target is used by Qt Creator's Android support.
add_custom_target(${target}_prepare_apk_dir add_custom_target(${target}_prepare_apk_dir
DEPENDS ${target} DEPENDS ${target}
COMMAND ${CMAKE_COMMAND} COMMAND ${CMAKE_COMMAND}
-E copy $<TARGET_FILE:${target}> -E copy $<TARGET_FILE:${target}>
"${apk_dir}/libs/${CMAKE_ANDROID_ARCH_ABI}/$<TARGET_FILE_NAME:${target}>" "${apk_final_dir}/libs/${CMAKE_ANDROID_ARCH_ABI}/$<TARGET_FILE_NAME:${target}>"
COMMENT "Copying ${target} binary to apk folder" COMMENT "Copying ${target} binary to apk folder"
) )
add_custom_target(${target}_make_apk # Add custom command that creates the apk in an intermediate location.
DEPENDS ${target}_prepare_apk_dir # We need the intermediate location, because we cannot have target-dependent generator
COMMAND ${deployment_tool} # expressions in OUTPUT.
--input ${deployment_file} add_custom_command(OUTPUT "${apk_intermediate_file_path}"
--output ${apk_dir} COMMAND ${CMAKE_COMMAND}
--apk ${apk_dir}/${target}.apk -E copy "$<TARGET_FILE:${target}>"
"${apk_intermediate_dir}/libs/${CMAKE_ANDROID_ARCH_ABI}/$<TARGET_FILE_NAME:${target}>"
COMMAND "${deployment_tool}"
--input "${deployment_file}"
--output "${apk_intermediate_dir}"
--apk "${apk_intermediate_file_path}"
COMMENT "Creating APK for ${target}" COMMENT "Creating APK for ${target}"
) DEPENDS "${target}" "${deployment_file}")
if(should_add_to_global_apk) # Create a ${target}_make_apk target to copy the apk from the intermediate to its final
add_dependencies(apk "${target}_make_apk") # location. If the final and intermediate locations are identical, this is a no-op.
endif() add_custom_target(${target}_make_apk
COMMAND "${CMAKE_COMMAND}"
-E copy_if_different "${apk_intermediate_file_path}" "${apk_final_file_path}"
DEPENDS "${apk_intermediate_file_path}")
endfunction() endfunction()
if(NOT QT_NO_CREATE_VERSIONLESS_FUNCTIONS) if(NOT QT_NO_CREATE_VERSIONLESS_FUNCTIONS)

View File

@ -446,12 +446,10 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
if (!QFile::exists(g_options.apkPath)) {
if (g_options.makeCommand.isEmpty()) { if (g_options.makeCommand.isEmpty()) {
fprintf(stderr, fprintf(stderr,
"No apk found at \"%s\". Provide a make command with the \"--make\" parameter " "It is required to provide a make command with the \"--make\" parameter "
"to generate it first.\n", "to generate the apk.\n");
qPrintable(g_options.apkPath));
return 1; return 1;
} }
if (!execCommand(g_options.makeCommand, nullptr, true)) { if (!execCommand(g_options.makeCommand, nullptr, true)) {
@ -468,7 +466,6 @@ int main(int argc, char *argv[])
} }
} }
} }
}
if (!QFile::exists(g_options.apkPath)) { if (!QFile::exists(g_options.apkPath)) {
fprintf(stderr, fprintf(stderr,