Update Android build configuration

Update Android build configuration to be compatible with the multi-arch
android build patch to qmake. We can now build and generate the apk
correctly. Executing on the device/simulator will only work once the
latest changes from 5.14 have been merged in.

We now replace target suffix with ${CMAKE_ANDROID_ARCH_ABI}.[so|a] so
we don't have to deal with handling targets which might have any of the
OUTPUT_NAME properties set.

The dependency and deploy settings generation has also been updated to
append the file contents to a string and then do a single file write
at the end.

Change-Id: Id3c5bd0428141ecaf962124a100390e3a4e41feb
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
This commit is contained in:
Leander Beernaert 2019-08-30 11:34:23 +02:00
parent 6c15ad7978
commit eeffac526e
4 changed files with 76 additions and 44 deletions

View File

@ -1225,6 +1225,10 @@ function(add_qt_module target)
else() else()
add_library("${target}" STATIC) add_library("${target}" STATIC)
endif() endif()
if (android)
qt_android_apply_arch_suffix("${target}")
endif()
qt_internal_add_target_aliases("${target}") qt_internal_add_target_aliases("${target}")
# Add _private target to link against the private headers: # Add _private target to link against the private headers:
@ -1683,6 +1687,10 @@ function(add_qt_plugin target)
set_property(TARGET "${target}" PROPERTY SUFFIX ".dylib") set_property(TARGET "${target}" PROPERTY SUFFIX ".dylib")
endif() endif()
endif() endif()
if (ANDROID)
qt_android_apply_arch_suffix("${target}")
endif()
qt_internal_add_target_aliases("${target}") qt_internal_add_target_aliases("${target}")
set_target_properties("${target}" PROPERTIES set_target_properties("${target}" PROPERTIES

View File

@ -112,10 +112,10 @@ function(qt_android_dependencies target)
endmacro() endmacro()
get_target_property(target_bin_dir ${target} BINARY_DIR) get_target_property(target_bin_dir ${target} BINARY_DIR)
set(dependency_file "${target_bin_dir}/${target_name}-android-dependencies.xml") set(dependency_file "${target_bin_dir}/${target_name}_${CMAKE_ANDROID_ARCH_ABI}-android-dependencies.xml")
file(WRITE ${dependency_file} "<rules><dependencies>\n") set(file_contents "<rules><dependencies>\n")
file(APPEND ${dependency_file} "<lib name=\"${target_name}\"><depends>\n") string(APPEND file_contents "<lib name=\"${target_name}_${CMAKE_ANDROID_ARCH_ABI}\"><depends>\n")
# Jar Dependencies # Jar Dependencies
if(arg_JAR_DEPENDENCIES) if(arg_JAR_DEPENDENCIES)
@ -125,7 +125,7 @@ function(qt_android_dependencies target)
set(init_class "initClass=\"${init_class}\"") set(init_class "initClass=\"${init_class}\"")
endif() endif()
file(TO_NATIVE_PATH ${jar_file} jar_file_native) file(TO_NATIVE_PATH ${jar_file} jar_file_native)
file(APPEND ${dependency_file} "<jar file=\"${jar_file_native}\" ${init_class} />\n") string(APPEND file_contents "<jar file=\"${jar_file_native}\" ${init_class} />\n")
endforeach() endforeach()
endif() endif()
@ -137,32 +137,34 @@ function(qt_android_dependencies target)
set(init_class "initClass=\"${init_class}\"") set(init_class "initClass=\"${init_class}\"")
endif() endif()
file(TO_NATIVE_PATH ${jar_bundle} jar_bundle_native) file(TO_NATIVE_PATH ${jar_bundle} jar_bundle_native)
file(APPEND ${dependency_file} "<jar bundling=\"1\" file=\"${jar_bundle_native}\" ${init_class} />\n") string(APPEND file_contents "<jar bundling=\"1\" file=\"${jar_bundle_native}\" ${init_class} />\n")
endforeach() endforeach()
endif() endif()
# Lib Dependencies # Lib Dependencies
if(arg_LIB_DEPENDENCIES) if(arg_LIB_DEPENDENCIES)
foreach(lib IN LISTS arg_LIB_DEPENDENCIES) foreach(lib IN LISTS arg_LIB_DEPENDENCIES)
string(REPLACE ".so" "_${CMAKE_ANDROID_ARCH_ABI}.so" lib ${lib})
section(${lib} ":" lib_file lib_extends) section(${lib} ":" lib_file lib_extends)
if (lib_extends) if (lib_extends)
set(lib_extends "extends=\"${lib_extends}\"") set(lib_extends "extends=\"${lib_extends}\"")
endif() endif()
file(TO_NATIVE_PATH ${lib_file} lib_file_native) file(TO_NATIVE_PATH ${lib_file} lib_file_native)
file(APPEND ${dependency_file} "<lib file=\"${lib_file_native}\" ${lib_extends} />\n") string(APPEND file_contents "<lib file=\"${lib_file_native}\" ${lib_extends} />\n")
endforeach() endforeach()
endif() endif()
# Lib Dependencies Replacements # Lib Dependencies Replacements
if(arg_LIB_DEPENDENCY_REPLACEMENTS) if(arg_LIB_DEPENDENCY_REPLACEMENTS)
foreach(lib IN LISTS arg_LIB_DEPENDENCY_REPLACEMENTS) foreach(lib IN LISTS arg_LIB_DEPENDENCY_REPLACEMENTS)
string(REPLACE ".so" "_${CMAKE_ANDROID_ARCH_ABI}.so" lib ${lib})
section(${lib} ":" lib_file lib_replacement) section(${lib} ":" lib_file lib_replacement)
if (lib_replacement) if (lib_replacement)
file(TO_NATIVE_PATH ${lib_replacement} lib_replacement_native) file(TO_NATIVE_PATH ${lib_replacement} lib_replacement_native)
set(lib_replacement "replaces=\"${lib_replacement_native}\"") set(lib_replacement "replaces=\"${lib_replacement_native}\"")
endif() endif()
file(TO_NATIVE_PATH ${lib_file} lib_file_native) file(TO_NATIVE_PATH ${lib_file} lib_file_native)
file(APPEND ${dependency_file} "<lib file=\"${lib_file_native}\" ${lib_replacement} />\n") string(APPEND file_contents "<lib file=\"${lib_file_native}\" ${lib_replacement} />\n")
endforeach() endforeach()
endif() endif()
@ -171,26 +173,27 @@ function(qt_android_dependencies target)
if(arg_BUNDLED_FILES) if(arg_BUNDLED_FILES)
foreach(file IN LISTS arg_BUNDLED_FILES) foreach(file IN LISTS arg_BUNDLED_FILES)
file(TO_NATIVE_PATH ${lib_file} file_native) file(TO_NATIVE_PATH ${lib_file} file_native)
file(APPEND ${dependency_file} "<bundled file=\"${file_native}\" />\n") string(APPEND file_contents "<bundled file=\"${file_native}\" />\n")
endforeach() endforeach()
endif() endif()
# Module plugins # Module plugins
if(module_plugins) if(module_plugins)
foreach(plugin IN LISTS module_plugins) foreach(plugin IN LISTS module_plugins)
file(APPEND ${dependency_file} "<bundled file=\"plugins/${plugin}\" />\n") string(APPEND file_contents "<bundled file=\"plugins/${plugin}\" />\n")
endforeach() endforeach()
endif() endif()
# Android Permissions # Android Permissions
if(arg_PERMISSIONS) if(arg_PERMISSIONS)
foreach(permission IN LISTS arg_PERMISSIONS) foreach(permission IN LISTS arg_PERMISSIONS)
file(APPEND ${dependency_file} "<permission name=\"${permission}\" />\n") string(APPEND file_contents "<permission name=\"${permission}\" />\n")
endforeach() endforeach()
endif() endif()
file(APPEND ${dependency_file} "</depends></lib>") string(APPEND file_contents "</depends></lib>")
file(APPEND ${dependency_file} "</dependencies></rules>\n") string(APPEND file_contents "</dependencies></rules>\n")
file(WRITE ${dependency_file} ${file_contents})
get_target_property(target_install_dir ${target} QT_ANDROID_MODULE_INSTALL_DIR) get_target_property(target_install_dir ${target} QT_ANDROID_MODULE_INSTALL_DIR)
if (NOT target_install_dir) if (NOT target_install_dir)
@ -206,3 +209,5 @@ function(qt_android_dependencies target)
COMPONENT COMPONENT
Devel) Devel)
endfunction() endfunction()

View File

@ -12,8 +12,6 @@
# Android API version # Android API version
# QT_ANDROID_SDK_BUILD_TOOLS_VERSION # QT_ANDROID_SDK_BUILD_TOOLS_VERSION
# Detected Android sdk build tools version # Detected Android sdk build tools version
# QT_ANDROID_NDK_STDLIB_PATH
# Detected path to the c++ stl lib shared library
# #
# Public functions: # Public functions:
# #
@ -82,10 +80,6 @@ if(NOT ANDROID_STL STREQUAL c++_shared)
message(FATAL_ERROR "The Qt libraries on Android only supports the shared library configuration of stl. Please use -DANDROID_STL=\"c++_shared\" as configuration argument.") message(FATAL_ERROR "The Qt libraries on Android only supports the shared library configuration of stl. Please use -DANDROID_STL=\"c++_shared\" as configuration argument.")
endif() endif()
# Location of stdlibc++
set(QT_ANDROID_NDK_STDLIB_PATH "${ANDROID_NDK}/sources/cxx-stl/llvm-libc++/libs/${ANDROID_ABI}/libc++_shared.so")
# Target properties required for android deploy tool # Target properties required for android deploy tool
define_property(TARGET define_property(TARGET
PROPERTY PROPERTY
@ -155,14 +149,14 @@ function(qt_android_generate_deployment_settings target)
endif() endif()
set(deploy_file "${target_binary_dir}/android-lib${target_output_name}.so-deployment-settings.json") set(deploy_file "${target_binary_dir}/android-lib${target_output_name}.so-deployment-settings.json")
file(WRITE ${deploy_file} "{\n") set(file_contents "{\n")
# content begin # content begin
file(APPEND ${deploy_file} string(APPEND file_contents
" \"description\": \"This file is generated by cmake to be read by androiddeployqt and should not be modified by hand.\",\n") " \"description\": \"This file is generated by cmake to be read by androiddeployqt and should not be modified by hand.\",\n")
# Host Qt Android install path # Host Qt Android install path
if (NOT QT_BUILDING_QT) if (NOT QT_BUILDING_QT)
set(file_check "${Qt6_DIR}/plugins/platforms/android/libqtforandroid.so") set(file_check "${Qt6_DIR}/plugins/platforms/android/libqtforandroid_${CMAKE_ANDROID_ARCH_ABI}.so")
if (NOT EXISTS ${file_check}) if (NOT EXISTS ${file_check})
message(SEND_ERROR "Detected Qt installation does not contain libqtforandroid.so. This is most likely due to the installation not being a build of Qt for Android. Please update your settings.") message(SEND_ERROR "Detected Qt installation does not contain libqtforandroid.so. This is most likely due to the installation not being a build of Qt for Android. Please update your settings.")
return() return()
@ -174,49 +168,59 @@ function(qt_android_generate_deployment_settings target)
endif() endif()
file(TO_NATIVE_PATH "${qt_android_install_dir}" qt_android_install_dir_native) file(TO_NATIVE_PATH "${qt_android_install_dir}" qt_android_install_dir_native)
file(APPEND ${deploy_file} string(APPEND file_contents
" \"qt\": \"${qt_android_install_dir_native}\",\n") " \"qt\": \"${qt_android_install_dir_native}\",\n")
# Android SDK path # Android SDK path
file(TO_NATIVE_PATH "${ANDROID_SDK_ROOT}" android_sdk_root_native) file(TO_NATIVE_PATH "${ANDROID_SDK_ROOT}" android_sdk_root_native)
file(APPEND ${deploy_file} string(APPEND file_contents
" \"sdk\": \"${android_sdk_root_native}\",\n") " \"sdk\": \"${android_sdk_root_native}\",\n")
# Android SDK Build Tools Revision # Android SDK Build Tools Revision
file(APPEND ${deploy_file} string(APPEND file_contents
" \"sdkBuildToolsRevision\": \"${QT_ANDROID_SDK_BUILD_TOOLS_VERSION}\",\n") " \"sdkBuildToolsRevision\": \"${QT_ANDROID_SDK_BUILD_TOOLS_VERSION}\",\n")
# Android NDK # Android NDK
file(TO_NATIVE_PATH "${ANDROID_NDK}" android_ndk_root_native) file(TO_NATIVE_PATH "${ANDROID_NDK}" android_ndk_root_native)
file(APPEND ${deploy_file} string(APPEND file_contents
" \"ndk\": \"${android_ndk_root_native}\",\n") " \"ndk\": \"${android_ndk_root_native}\",\n")
# Setup LLVM toolchain # Setup LLVM toolchain
file(APPEND ${deploy_file} string(APPEND file_contents
" \"toolchain-prefix\": \"llvm\",\n") " \"toolchain-prefix\": \"llvm\",\n")
file(APPEND ${deploy_file} string(APPEND file_contents
" \"tool-prefix\": \"llvm\",\n") " \"tool-prefix\": \"llvm\",\n")
file(APPEND ${deploy_file} string(APPEND file_contents
" \"useLLVM\": true,\n") " \"useLLVM\": true,\n")
# NDK Toolchain Version # NDK Toolchain Version
file(APPEND ${deploy_file} string(APPEND file_contents
" \"toolchain-version\": \"${CMAKE_ANDROID_NDK_TOOLCHAIN_VERSION}\",\n") " \"toolchain-version\": \"${CMAKE_ANDROID_NDK_TOOLCHAIN_VERSION}\",\n")
# NDK Host # NDK Host
file(APPEND ${deploy_file} string(APPEND file_contents
" \"ndk-host\": \"${ANDROID_NDK_HOST_SYSTEM_NAME}\",\n") " \"ndk-host\": \"${ANDROID_NDK_HOST_SYSTEM_NAME}\",\n")
if (CMAKE_ANDROID_ARCH_ABI STREQUAL "x86")
set(arch_value "i686-linux-android")
elseif (CMAKE_ANDROID_ARCH_ABI STREQUAL "x86_64")
set(arch_value "x86_64-linux-android")
elseif (CMAKE_ANDROID_ARCH_ABI STREQUAL "armv64-v8a")
set(arch_value "aarch64-linux-android")
else()
set(arch_value "arm-linux-androideabi")
endif()
# Architecture # Architecture
file(APPEND ${deploy_file} string(APPEND file_contents
" \"target-architecture\": \"${CMAKE_ANDROID_ARCH_ABI}\",\n") " \"architectures\": { \"${CMAKE_ANDROID_ARCH_ABI}\" : \"${arch_value}\" },\n")
# deployment dependencies # deployment dependencies
get_target_property(android_deployment_dependencies get_target_property(android_deployment_dependencies
${target} QT_ANDROID_DEPLOYMENT_DEPENDENCIES) ${target} QT_ANDROID_DEPLOYMENT_DEPENDENCIES)
if (android_deployment_dependencies) if (android_deployment_dependencies)
list(JOIN android_deployment_dependencies "," android_deployment_dependencies) list(JOIN android_deployment_dependencies "," android_deployment_dependencies)
file(APPEND ${deploy_file} string(APPEND file_contents
" \"deployment-dependencies\": \"${android_deployment_dependencies}\",\n") " \"deployment-dependencies\": \"${android_deployment_dependencies}\",\n")
endif() endif()
@ -225,7 +229,7 @@ function(qt_android_generate_deployment_settings target)
${target} QT_ANDROID_EXTRA_PLUGINS) ${target} QT_ANDROID_EXTRA_PLUGINS)
if (android_extra_plugins) if (android_extra_plugins)
list(JOIN android_extra_plugins "," android_extra_plugins) list(JOIN android_extra_plugins "," android_extra_plugins)
file(APPEND ${deploy_file} string(APPEND file_contents
" \"android-extra-plugins\": \"${android_extra_plugins}\",\n") " \"android-extra-plugins\": \"${android_extra_plugins}\",\n")
endif() endif()
@ -234,7 +238,7 @@ function(qt_android_generate_deployment_settings target)
${target} QT_ANDROID_EXTRA_LIBS) ${target} QT_ANDROID_EXTRA_LIBS)
if (android_extra_libs) if (android_extra_libs)
list(JOIN android_extra_libs "," android_extra_libs) list(JOIN android_extra_libs "," android_extra_libs)
file(APPEND ${deploy_file} string(APPEND file_contents
" \"android-extra-libs\": \"${android_extra_libs}\",\n") " \"android-extra-libs\": \"${android_extra_libs}\",\n")
endif() endif()
@ -243,7 +247,7 @@ function(qt_android_generate_deployment_settings target)
${target} QT_ANDROID_PACKAGE_SOURCE_DIR) ${target} QT_ANDROID_PACKAGE_SOURCE_DIR)
if (android_package_source_dir) if (android_package_source_dir)
file(TO_NATIVE_PATH "${android_package_source_dir}" android_package_source_dir_native) file(TO_NATIVE_PATH "${android_package_source_dir}" android_package_source_dir_native)
file(APPEND ${deploy_file} string(APPEND file_contents
" \"android-package-source-directory\": \"${android_package_source_dir_native}\",\n") " \"android-package-source-directory\": \"${android_package_source_dir_native}\",\n")
endif() endif()
@ -254,7 +258,7 @@ endif()
get_target_property(qml_import_path ${target} QT_QML_IMPORT_PATH) get_target_property(qml_import_path ${target} QT_QML_IMPORT_PATH)
if (qml_import_path) if (qml_import_path)
file(TO_NATIVE_PATH "${qml_import_path}" qml_import_path_native) file(TO_NATIVE_PATH "${qml_import_path}" qml_import_path_native)
file(APPEND ${deploy_file} string(APPEND file_contents
" \"qml-import-path\": \"${qml_import_path_native}\",\n") " \"qml-import-path\": \"${qml_import_path_native}\",\n")
endif() endif()
@ -263,24 +267,38 @@ endif()
set(qml_root_path "${target_source_dir}") set(qml_root_path "${target_source_dir}")
endif() endif()
file(TO_NATIVE_PATH "${qml_root_path}" qml_root_path_native) file(TO_NATIVE_PATH "${qml_root_path}" qml_root_path_native)
file(APPEND ${deploy_file} string(APPEND file_contents
" \"qml-root-path\": \"${qml_root_path_native}\",\n") " \"qml-root-path\": \"${qml_root_path_native}\",\n")
# App binary # App binary
file(TO_NATIVE_PATH "${target_binary_dir}/lib${target_output_name}.so" target_binary_dir_native) string(APPEND file_contents
file(APPEND ${deploy_file} " \"application-binary\": \"${target_output_name}\",\n")
" \"application-binary\": \"${target_binary_dir_native}\",\n")
# Lats item in json file # Lats item in json file
file(APPEND ${deploy_file} # base location of stdlibc++, will be suffixed by androiddeploy qt
" \"stdcpp-path\": \"${QT_ANDROID_NDK_STDLIB_PATH}\"\n") set(android_ndk_stdlib_base_path
"${ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/"
)
string(APPEND file_contents
" \"stdcpp-path\": \"${android_ndk_stdlib_base_path}\"\n")
# content end # content end
file(APPEND ${deploy_file} "}\n") string(APPEND file_contents "}\n")
file(WRITE ${deploy_file} ${file_contents})
set_target_properties(${target} set_target_properties(${target}
PROPERTIES PROPERTIES
QT_ANDROID_DEPLOYMENT_SETTINGS_FILE ${deploy_file} QT_ANDROID_DEPLOYMENT_SETTINGS_FILE ${deploy_file}
) )
endfunction() endfunction()
function(qt_android_apply_arch_suffix target)
get_target_property(target_type ${target} TYPE)
if (target_type STREQUAL "SHARED_LIBRARY")
set_property(TARGET "${target}" PROPERTY SUFFIX "_${CMAKE_ANDROID_ARCH_ABI}.so")
elseif (target_type STREQUAL "STATIC_LIBRARY")
set_property(TARGET "${target}" PROPERTY SUFFIX "_${CMAKE_ANDROID_ARCH_ABI}.a")
endif()
endfunction()

View File

@ -408,6 +408,7 @@ function(add_qt_gui_executable target)
# through dlopen() # through dlopen()
set_property(TARGET "${target}" PROPERTY C_VISIBILITY_PRESET default) set_property(TARGET "${target}" PROPERTY C_VISIBILITY_PRESET default)
set_property(TARGET "${target}" PROPERTY CXX_VISIBILITY_PRESET default) set_property(TARGET "${target}" PROPERTY CXX_VISIBILITY_PRESET default)
qt_android_apply_arch_suffix("${target}")
else() else()
add_executable("${target}" WIN32 MACOSX_BUNDLE ${ARGN}) add_executable("${target}" WIN32 MACOSX_BUNDLE ${ARGN})
endif() endif()