*/}}

apk_build.cmake 5.1 KB

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