*/}}

apk_build.cmake 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. include(ExternalProject)
  2. set(ANDROID_SDK_ROOT $ENV{ANDROID_SDK_ROOT} CACHE PATH "Path to Android SDK")
  3. set(ANDROID_PLATFORM_TOOLS_DIR "${ANDROID_SDK_ROOT}/platform-tools")
  4. file(GLOB ANDROID_BUILD_TOOLS_VERSIONS "${ANDROID_SDK_ROOT}/build-tools/*")
  5. list(POP_BACK ANDROID_BUILD_TOOLS_VERSIONS ANDROID_BUILD_TOOLS_LATEST)
  6. set(ANDROID_BUILD_TOOLS_DIR "${ANDROID_BUILD_TOOLS_LATEST}" CACHE PATH "Path to Android build tools (i.e. where to find aapt2, etc)")
  7. file(GLOB ANDROID_NDK_VERSIONS "${ANDROID_SDK_ROOT}/ndk/*")
  8. list(POP_BACK ANDROID_NDK_VERSIONS ANDROID_NDK_LATEST)
  9. set(ANDROID_NDK "${ANDROID_NDK_LATEST}" CACHE PATH "Path to Android NDK")
  10. file(GLOB ANDROID_JAR_VERSIONS "${ANDROID_SDK_ROOT}/platforms/*")
  11. list(POP_BACK ANDROID_JAR_VERSIONS ANDROID_JAR_LATEST)
  12. set(ANDROID_JAR "${ANDROID_JAR_LATEST}/android.jar" CACHE PATH "Path to Android JAR")
  13. set(LCMS_SRC_DIR "${LCMS_SRC_DIR}" CACHE PATH "Path to LCMS source")
  14. set(FREETYPE_SRC_DIR "${FREETYPE_SRC_DIR}" CACHE PATH "Path to Freetype source")
  15. set(PNG_SRC_DIR "${PNG_SRC_DIR_SRC_DIR}" CACHE PATH "Path to PNG source")
  16. set(LAGUI_SRC_DIR "${LAGUI_SRC_DIR}" CACHE PATH "Path to LaGUI source")
  17. set(EXECUTABLE_NAME "${EXECUTABLE_NAME}" CACHE STRING "Executable name")
  18. set(EXECUTABLE_SRC_DIR "${EXECUTABLE_SRC_DIR}" CACHE PATH "Executable source")
  19. set(ANDROID_NATIVE_API_LEVEL 24 CACHE STRING "Api level")
  20. find_program(ADB NAMES adb REQUIRED PATHS ${ANDROID_PLATFORM_TOOLS_DIR})
  21. find_program(AAPT2 NAMES aapt2 REQUIRED PATHS ${ANDROID_BUILD_TOOLS_DIR})
  22. find_program(APKSIGNER NAMES apksigner REQUIRED PATHS ${ANDROID_BUILD_TOOLS_DIR})
  23. find_program(ZIPALIGN NAMES zipalign REQUIRED PATHS ${ANDROID_BUILD_TOOLS_DIR})
  24. set(APK_CONTENTS_ROOT "${CMAKE_CURRENT_BINARY_DIR}/apk")
  25. macro(setup_variant VARIANT)
  26. ExternalProject_Add(freetype-${VARIANT}
  27. SOURCE_DIR ${FREETYPE_SRC_DIR}
  28. #EXCLUDE_FROM_ALL TRUE
  29. CMAKE_ARGS
  30. -DANDROID_ABI=${VARIANT}
  31. -DANDROID_NDK=${ANDROID_NDK}
  32. -DANDROID_STL=c++_static
  33. -DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/install/${VARIANT}
  34. #-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
  35. #-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
  36. -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake
  37. BUILD_ALWAYS True
  38. )
  39. ExternalProject_Add(png-${VARIANT}
  40. SOURCE_DIR ${PNG_SRC_DIR}
  41. #EXCLUDE_FROM_ALL TRUE
  42. CMAKE_ARGS
  43. -DANDROID_ABI=${VARIANT}
  44. -DANDROID_NDK=${ANDROID_NDK}
  45. -DANDROID_STL=c++_static
  46. -DPNG_SHARED=OFF
  47. -DHAVE_LD_VERSION_SCRIPT=OFF
  48. -DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/install/${VARIANT}
  49. #-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
  50. #-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
  51. -DANDROID_NATIVE_API_LEVEL=${ANDROID_NATIVE_API_LEVEL}
  52. -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake
  53. BUILD_ALWAYS True
  54. )
  55. ExternalProject_Add(lcms2-${VARIANT}
  56. SOURCE_DIR ${LCMS_SRC_DIR}
  57. #EXCLUDE_FROM_ALL TRUE
  58. CMAKE_ARGS
  59. -DANDROID_ABI=${VARIANT}
  60. -DANDROID_NDK=${ANDROID_NDK}
  61. -DANDROID_STL=c++_static
  62. -DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/install/${VARIANT}
  63. #-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
  64. #-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
  65. -DANDROID_NATIVE_API_LEVEL=${ANDROID_NATIVE_API_LEVEL}
  66. -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake
  67. BUILD_ALWAYS True
  68. )
  69. ExternalProject_Add(lagui-${VARIANT}
  70. SOURCE_DIR ${LAGUI_SRC_DIR}
  71. #EXCLUDE_FROM_ALL TRUE
  72. CMAKE_ARGS
  73. -DANDROID_ABI=${VARIANT}
  74. -DANDROID_NDK=${ANDROID_NDK}
  75. -DANDROID_STL=c++_static
  76. -DLAGUI_IGNORE_PACKAGES=True
  77. -DLAGUI_USE_GLES=True
  78. -DLAGUI_USE_PNG=True
  79. -DLAGUI_ANDROID=True
  80. -DLAGUI_USE_LUAJIT=False
  81. -DFREETYPE_SRC_DIR=${FREETYPE_SRC_DIR}
  82. -DPNG_SRC_DIR=${PNG_SRC_DIR}
  83. -DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/install/${VARIANT}
  84. #-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
  85. #-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
  86. -DANDROID_NATIVE_API_LEVEL=${ANDROID_NATIVE_API_LEVEL}
  87. -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake
  88. BUILD_ALWAYS True
  89. )
  90. ExternalProject_Add(${EXECUTABLE_NAME}-${VARIANT}
  91. SOURCE_DIR ${EXECUTABLE_SRC_DIR}
  92. #EXCLUDE_FROM_ALL TRUE
  93. CMAKE_ARGS
  94. -DANDROID_ABI=${VARIANT}
  95. -DANDROID_NDK=${ANDROID_NDK}
  96. -DANDROID_STL=c++_static
  97. -DLAGUI_SRC_DIR=${LAGUI_SRC_DIR}
  98. -DLAGUI_USE_GLES=True
  99. -DLAGUI_USE_LUAJIT=False
  100. -DLAGUI_USE_PNG=True
  101. -DLAGUI_ANDROID=True
  102. -DFREETYPE_SRC_DIR=${FREETYPE_SRC_DIR}
  103. -DLAGUI_SRC_DIR=${LAGUI_SRC_DIR}
  104. -DPNG_SRC_DIR=${PNG_SRC_DIR}
  105. -DLCMS_SRC_DIR=${LCMS_SRC_DIR}
  106. -DANDROID_COMPILE=True
  107. -DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/install/${VARIANT}
  108. -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
  109. -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
  110. -DANDROID_NATIVE_API_LEVEL=${ANDROID_NATIVE_API_LEVEL}
  111. -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake
  112. BUILD_ALWAYS True
  113. )
  114. endmacro()
  115. setup_variant(arm64-v8a)
  116. #setup_variant(armeabi-v7a)
  117. #setup_variant(x86)
  118. setup_variant(x86_64)
  119. set(MANIFEST "${CMAKE_CURRENT_SOURCE_DIR}/android/src/AndroidManifest.xml")
  120. set(RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/android/res")
  121. set(KEYSTORE "${CMAKE_CURRENT_SOURCE_DIR}/android/keystore.jks")
  122. set(VALUES_STRING "${CMAKE_CURRENT_BINARY_DIR}/values_strings.arsc.flat")
  123. include(CMakePrintHelpers)
  124. cmake_print_variables(CMAKE_CURRENT_SOURCE_DIR)
  125. cmake_print_variables(CMAKE_CURRENT_BINARY_DIR)
  126. set(FINAL_APK "${CMAKE_CURRENT_BINARY_DIR}/app.apk")
  127. set(RESOURCES_APK "${CMAKE_CURRENT_BINARY_DIR}/app.res.apk")
  128. set(UNALIGNED_APK "${CMAKE_CURRENT_BINARY_DIR}/app.unaligned.apk")
  129. # keytool -genkeypair -keystore keystore.jks -alias androidkey -validity 10000 -keyalg RSA -keysize 2048 -storepass android -keypass android
  130. add_custom_command(
  131. OUTPUT ${VALUES_STRING}
  132. COMMAND ${AAPT2} compile ${RESOURCES}/values/strings.xml -o ${CMAKE_CURRENT_BINARY_DIR}
  133. DEPENDS ${RESOURCES}/values/strings.xml
  134. )
  135. add_custom_command(
  136. OUTPUT ${RESOURCES_APK}
  137. COMMAND ${AAPT2} link ${VALUES_STRING} -o ${RESOURCES_APK} --manifest ${MANIFEST} -I ${ANDROID_JAR}
  138. DEPENDS ${MANIFEST} ${VALUES_STRING}
  139. )
  140. add_custom_command(
  141. OUTPUT ${UNALIGNED_APK}
  142. COMMAND ${CMAKE_COMMAND} -E tar x ${RESOURCES_APK}
  143. COMMAND ${CMAKE_COMMAND} -E tar c ${UNALIGNED_APK} --format=zip .
  144. WORKING_DIRECTORY ${APK_CONTENTS_ROOT}
  145. DEPENDS
  146. ${RESOURCES_APK}
  147. #${EXECUTABLE_NAME}-armeabi-v7a
  148. ${EXECUTABLE_NAME}-arm64-v8a
  149. #${EXECUTABLE_NAME}-x86
  150. ${EXECUTABLE_NAME}-x86_64
  151. freetype-x86_64
  152. #freetype-x86
  153. freetype-arm64-v8a
  154. #freetype-armeabi-v7a
  155. png-x86_64
  156. png-arm64-v8a
  157. )
  158. add_custom_command(
  159. OUTPUT ${FINAL_APK}
  160. COMMAND ${ZIPALIGN} -p -f -v 4 ${UNALIGNED_APK} ${FINAL_APK}
  161. COMMAND ${APKSIGNER} sign -ks ${KEYSTORE} --ks-key-alias androidkey --ks-pass pass:android --key-pass pass:android ${FINAL_APK}
  162. DEPENDS ${UNALIGNED_APK}
  163. )
  164. add_custom_target(apk ALL DEPENDS ${FINAL_APK})
  165. install(CODE "execute_process(COMMAND ${ADB} install ${FINAL_APK})")