*/}}

apk_build.cmake 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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(FREETYPE_SRC_DIR "${FREETYPE_SRC_DIR}" CACHE PATH "Path to Freetype source")
  14. set(PNG_SRC_DIR "${PNG_SRC_DIR_SRC_DIR}" CACHE PATH "Path to PNG source")
  15. set(LAGUI_SRC_DIR "${LAGUI_SRC_DIR}" CACHE PATH "Path to LaGUI source")
  16. set(EXECUTABLE_NAME "${EXECUTABLE_NAME}" CACHE PATH "Executable name")
  17. set(EXECUTABLE_SRC_DIR "${EXECUTABLE_SRC_DIR}" CACHE PATH "Executable source")
  18. find_program(ADB NAMES adb REQUIRED PATHS ${ANDROID_PLATFORM_TOOLS_DIR})
  19. find_program(AAPT2 NAMES aapt2 REQUIRED PATHS ${ANDROID_BUILD_TOOLS_DIR})
  20. find_program(APKSIGNER NAMES apksigner REQUIRED PATHS ${ANDROID_BUILD_TOOLS_DIR})
  21. find_program(ZIPALIGN NAMES zipalign REQUIRED PATHS ${ANDROID_BUILD_TOOLS_DIR})
  22. set(APK_CONTENTS_ROOT "${CMAKE_CURRENT_BINARY_DIR}/apk")
  23. macro(setup_variant VARIANT)
  24. ExternalProject_Add(freetype-${VARIANT}
  25. SOURCE_DIR ${FREETYPE_SRC_DIR}
  26. #EXCLUDE_FROM_ALL TRUE
  27. CMAKE_ARGS
  28. -DANDROID_ABI=${VARIANT}
  29. -DANDROID_NDK=${ANDROID_NDK}
  30. -DANDROID_STL=c++_static
  31. -DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/install/${VARIANT}
  32. #-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
  33. #-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
  34. -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake
  35. BUILD_ALWAYS True
  36. )
  37. ExternalProject_Add(png-${VARIANT}
  38. SOURCE_DIR ${PNG_SRC_DIR}
  39. #EXCLUDE_FROM_ALL TRUE
  40. CMAKE_ARGS
  41. -DANDROID_ABI=${VARIANT}
  42. -DANDROID_NDK=${ANDROID_NDK}
  43. -DANDROID_STL=c++_static
  44. -DPNG_SHARED=OFF
  45. -DHAVE_LD_VERSION_SCRIPT=OFF
  46. -DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/install/${VARIANT}
  47. #-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
  48. #-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
  49. -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake
  50. BUILD_ALWAYS True
  51. )
  52. ExternalProject_Add(lagui-${VARIANT}
  53. SOURCE_DIR ${LAGUI_SRC_DIR}
  54. #EXCLUDE_FROM_ALL TRUE
  55. CMAKE_ARGS
  56. -DANDROID_ABI=${VARIANT}
  57. -DANDROID_NDK=${ANDROID_NDK}
  58. -DANDROID_STL=c++_static
  59. -DLAGUI_USE_GLES=True
  60. -DLAGUI_USE_LUAJIT=False
  61. -DLAGUI_USE_PNG=True
  62. -DLAGUI_ANDROID=True
  63. -DFREETYPE_SRC_DIR=${FREETYPE_SRC_DIR}
  64. -DPNG_SRC_DIR=${PNG_SRC_DIR}
  65. -DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/install/${VARIANT}
  66. #-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
  67. #-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
  68. -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake
  69. BUILD_ALWAYS True
  70. )
  71. ExternalProject_Add(${EXECUTABLE_NAME}-${VARIANT}
  72. SOURCE_DIR ${EXECUTABLE_SRC_DIR}
  73. #EXCLUDE_FROM_ALL TRUE
  74. CMAKE_ARGS
  75. -DANDROID_ABI=${VARIANT}
  76. -DANDROID_NDK=${ANDROID_NDK}
  77. -DANDROID_STL=c++_static
  78. -DLAGUI_SRC_DIR=${LAGUI_SRC_DIR}
  79. -DLAGUI_USE_GLES=True
  80. -DLAGUI_USE_LUAJIT=False
  81. -DLAGUI_USE_PNG=True
  82. -DLAGUI_ANDROID=True
  83. -DFREETYPE_SRC_DIR=${FREETYPE_SRC_DIR}
  84. -DLAGUI_SRC_DIR=${LAGUI_SRC_DIR}
  85. -DPNG_SRC_DIR=${PNG_SRC_DIR}
  86. -DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/install/${VARIANT}
  87. -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
  88. -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=${APK_CONTENTS_ROOT}/lib/${VARIANT}
  89. -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake
  90. BUILD_ALWAYS True
  91. )
  92. endmacro()
  93. setup_variant(arm64-v8a)
  94. #setup_variant(armeabi-v7a)
  95. #setup_variant(x86)
  96. setup_variant(x86_64)
  97. set(MANIFEST "${CMAKE_CURRENT_SOURCE_DIR}/src/AndroidManifest.xml")
  98. set(RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/res")
  99. set(KEYSTORE "${CMAKE_CURRENT_SOURCE_DIR}/keystore.jks")
  100. set(VALUES_STRING "${CMAKE_CURRENT_BINARY_DIR}/values_strings.arsc.flat")
  101. set(FINAL_APK "${CMAKE_CURRENT_BINARY_DIR}/app.apk")
  102. set(RESOURCES_APK "${CMAKE_CURRENT_BINARY_DIR}/app.res.apk")
  103. set(UNALIGNED_APK "${CMAKE_CURRENT_BINARY_DIR}/app.unaligned.apk")
  104. # keytool -genkeypair -keystore keystore.jks -alias androidkey -validity 10000 -keyalg RSA -keysize 2048 -storepass android -keypass android
  105. add_custom_command(
  106. OUTPUT ${VALUES_STRING}
  107. COMMAND ${AAPT2} compile ${RESOURCES}/values/strings.xml -o ${CMAKE_CURRENT_BINARY_DIR}
  108. DEPENDS ${RESOURCES}/values/strings.xml
  109. )
  110. add_custom_command(
  111. OUTPUT ${RESOURCES_APK}
  112. COMMAND ${AAPT2} link ${VALUES_STRING} -o ${RESOURCES_APK} --manifest ${MANIFEST} -I ${ANDROID_JAR}
  113. DEPENDS ${MANIFEST} ${VALUES_STRING}
  114. )
  115. add_custom_command(
  116. OUTPUT ${UNALIGNED_APK}
  117. COMMAND ${CMAKE_COMMAND} -E tar x ${RESOURCES_APK}
  118. COMMAND ${CMAKE_COMMAND} -E tar c ${UNALIGNED_APK} --format=zip .
  119. WORKING_DIRECTORY ${APK_CONTENTS_ROOT}
  120. DEPENDS
  121. ${RESOURCES_APK}
  122. #${EXECUTABLE_NAME}-armeabi-v7a
  123. ${EXECUTABLE_NAME}-arm64-v8a
  124. #${EXECUTABLE_NAME}-x86
  125. ${EXECUTABLE_NAME}-x86_64
  126. freetype-x86_64
  127. #freetype-x86
  128. freetype-arm64-v8a
  129. #freetype-armeabi-v7a
  130. png-x86_64
  131. png-arm64-v8a
  132. )
  133. add_custom_command(
  134. OUTPUT ${FINAL_APK}
  135. COMMAND ${ZIPALIGN} -p -f -v 4 ${UNALIGNED_APK} ${FINAL_APK}
  136. COMMAND ${APKSIGNER} sign --in ${FINAL_APK} -ks ${KEYSTORE} --ks-key-alias androidkey --ks-pass pass:android --key-pass pass:android
  137. DEPENDS ${UNALIGNED_APK}
  138. )
  139. add_custom_target(apk ALL DEPENDS ${FINAL_APK})
  140. install(CODE "execute_process(COMMAND ${ADB} install ${FINAL_APK})")