android_gradle_projects.cmake 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. # https://developer.android.com/studio/releases/gradle-plugin
  2. set(ANDROID_GRADLE_PLUGIN_VERSION "3.2.1" CACHE STRING "Android Gradle Plugin version")
  3. message(STATUS "Android Gradle Plugin version: ${ANDROID_GRADLE_PLUGIN_VERSION}")
  4. set(KOTLIN_PLUGIN_VERSION "1.4.10" CACHE STRING "Kotlin Plugin version")
  5. message(STATUS "Kotlin Plugin version: ${KOTLIN_PLUGIN_VERSION}")
  6. if(BUILD_KOTLIN_EXTENSIONS)
  7. set(KOTLIN_PLUGIN_DECLARATION "apply plugin: 'kotlin-android'" CACHE STRING "Kotlin Plugin version")
  8. set(KOTLIN_STD_LIB "implementation 'org.jetbrains.kotlin:kotlin-stdlib:${KOTLIN_PLUGIN_VERSION}'" CACHE STRING "Kotlin Standard Library dependency")
  9. else()
  10. set(KOTLIN_PLUGIN_DECLARATION "" CACHE STRING "Kotlin Plugin version")
  11. set(KOTLIN_STD_LIB "" CACHE STRING "Kotlin Standard Library dependency")
  12. endif()
  13. set(GRADLE_VERSION "5.6.4" CACHE STRING "Gradle version")
  14. message(STATUS "Gradle version: ${GRADLE_VERSION}")
  15. set(ANDROID_COMPILE_SDK_VERSION "26" CACHE STRING "Android compileSdkVersion")
  16. if(ANDROID_NATIVE_API_LEVEL GREATER 21)
  17. set(ANDROID_MIN_SDK_VERSION "${ANDROID_NATIVE_API_LEVEL}" CACHE STRING "Android minSdkVersion")
  18. else()
  19. set(ANDROID_MIN_SDK_VERSION "21" CACHE STRING "Android minSdkVersion")
  20. endif()
  21. set(ANDROID_TARGET_SDK_VERSION "26" CACHE STRING "Android minSdkVersion")
  22. set(ANDROID_BUILD_BASE_DIR "${OpenCV_BINARY_DIR}/opencv_android" CACHE INTERNAL "")
  23. set(ANDROID_TMP_INSTALL_BASE_DIR "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/install/opencv_android")
  24. set(ANDROID_INSTALL_SAMPLES_DIR "samples")
  25. set(ANDROID_BUILD_ABI_FILTER "
  26. reset()
  27. include '${ANDROID_ABI}'
  28. ")
  29. set(ANDROID_INSTALL_ABI_FILTER "
  30. //reset()
  31. //include 'armeabi-v7a'
  32. //include 'arm64-v8a'
  33. //include 'x86'
  34. //include 'x86_64'
  35. ")
  36. if(NOT INSTALL_CREATE_DISTRIB)
  37. set(ANDROID_INSTALL_ABI_FILTER "${ANDROID_BUILD_ABI_FILTER}")
  38. endif()
  39. # BUG: Ninja generator generates broken targets with ANDROID_ABI_FILTER name (CMake 3.11.2)
  40. #set(__spaces " ")
  41. #string(REPLACE "\n" "\n${__spaces}" ANDROID_ABI_FILTER "${__spaces}${ANDROID_BUILD_ABI_FILTER}")
  42. #string(REPLACE REGEX "[ ]+$" "" ANDROID_ABI_FILTER "${ANDROID_ABI_FILTER}")
  43. set(ANDROID_ABI_FILTER "${ANDROID_BUILD_ABI_FILTER}")
  44. set(ANDROID_STRICT_BUILD_CONFIGURATION "true")
  45. configure_file("${OpenCV_SOURCE_DIR}/samples/android/build.gradle.in" "${ANDROID_BUILD_BASE_DIR}/build.gradle" @ONLY)
  46. set(ANDROID_ABI_FILTER "${ANDROID_INSTALL_ABI_FILTER}")
  47. set(ANDROID_STRICT_BUILD_CONFIGURATION "false")
  48. configure_file("${OpenCV_SOURCE_DIR}/samples/android/build.gradle.in" "${ANDROID_TMP_INSTALL_BASE_DIR}/${ANDROID_INSTALL_SAMPLES_DIR}/build.gradle" @ONLY)
  49. install(FILES "${ANDROID_TMP_INSTALL_BASE_DIR}/${ANDROID_INSTALL_SAMPLES_DIR}/build.gradle" DESTINATION "${ANDROID_INSTALL_SAMPLES_DIR}" COMPONENT samples)
  50. configure_file("${OpenCV_SOURCE_DIR}/platforms/android/gradle-wrapper/gradle/wrapper/gradle-wrapper.properties.in" "${ANDROID_BUILD_BASE_DIR}/gradle/wrapper/gradle-wrapper.properties" @ONLY)
  51. install(FILES "${ANDROID_BUILD_BASE_DIR}/gradle/wrapper/gradle-wrapper.properties" DESTINATION "${ANDROID_INSTALL_SAMPLES_DIR}/gradle/wrapper" COMPONENT samples)
  52. set(GRADLE_WRAPPER_FILES
  53. "gradle/wrapper/gradle-wrapper.jar"
  54. "gradlew.bat"
  55. "gradlew"
  56. "gradle.properties"
  57. )
  58. foreach(fname ${GRADLE_WRAPPER_FILES})
  59. get_filename_component(__dir "${fname}" DIRECTORY)
  60. set(__permissions "")
  61. set(__permissions_prefix "")
  62. if(fname STREQUAL "gradlew")
  63. set(__permissions FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
  64. endif()
  65. file(COPY "${OpenCV_SOURCE_DIR}/platforms/android/gradle-wrapper/${fname}" DESTINATION "${ANDROID_BUILD_BASE_DIR}/${__dir}" ${__permissions})
  66. string(REPLACE "FILE_PERMISSIONS" "PERMISSIONS" __permissions "${__permissions}")
  67. if("${__dir}" STREQUAL "")
  68. set(__dir ".")
  69. endif()
  70. install(FILES "${OpenCV_SOURCE_DIR}/platforms/android/gradle-wrapper/${fname}" DESTINATION "${ANDROID_INSTALL_SAMPLES_DIR}/${__dir}" COMPONENT samples ${__permissions})
  71. endforeach()
  72. # set build.gradle namespace
  73. if(NOT (ANDROID_GRADLE_PLUGIN_VERSION VERSION_LESS "7.3.0"))
  74. ocv_update(OPENCV_ANDROID_NAMESPACE_DECLARATION "namespace 'org.opencv'")
  75. else()
  76. ocv_update(OPENCV_ANDROID_NAMESPACE_DECLARATION "")
  77. endif()
  78. # set android gradle java version in build.gradle and set aidl config
  79. if(NOT (ANDROID_GRADLE_PLUGIN_VERSION VERSION_LESS "8.0.0"))
  80. # AGP-8.0 requires a minimum JDK version of JDK17
  81. ocv_update(ANDROID_GRADLE_JAVA_VERSION_INIT "17")
  82. # Enable aidl configuration for OpenCV compile with AGP-8.0
  83. ocv_update(ANDROID_GRADLE_BUILD_FEATURE_AIDL "buildFeatures { aidl true }")
  84. else()
  85. ocv_update(ANDROID_GRADLE_JAVA_VERSION_INIT "1_8")
  86. ocv_update(ANDROID_GRADLE_BUILD_FEATURE_AIDL "")
  87. endif()
  88. set(ANDROID_GRADLE_JAVA_VERSION "${ANDROID_GRADLE_JAVA_VERSION_INIT}" CACHE STRING "Android Gradle Java version")
  89. message(STATUS "Android Gradle Java version: ${ANDROID_GRADLE_JAVA_VERSION}")
  90. # force reusing of the same CMake version
  91. if(NOT OPENCV_SKIP_ANDROID_FORCE_CMAKE)
  92. if(NOT DEFINED _CMAKE_INSTALL_DIR)
  93. get_filename_component(_CMAKE_INSTALL_DIR "${CMAKE_ROOT}" PATH)
  94. get_filename_component(_CMAKE_INSTALL_DIR "${_CMAKE_INSTALL_DIR}" PATH)
  95. endif()
  96. ocv_update_file("${ANDROID_BUILD_BASE_DIR}/local.properties" "cmake.dir=${_CMAKE_INSTALL_DIR}")
  97. endif()
  98. file(WRITE "${ANDROID_BUILD_BASE_DIR}/settings.gradle" "
  99. include ':opencv'
  100. ")
  101. file(WRITE "${ANDROID_TMP_INSTALL_BASE_DIR}/settings.gradle" "
  102. rootProject.name = 'opencv_samples'
  103. def opencvsdk='../'
  104. //def opencvsdk='/<path to OpenCV-android-sdk>'
  105. //println opencvsdk
  106. include ':opencv'
  107. project(':opencv').projectDir = new File(opencvsdk + '/sdk')
  108. ")
  109. ocv_check_environment_variables(OPENCV_GRADLE_VERBOSE_OPTIONS)
  110. macro(add_android_project target path)
  111. get_filename_component(__dir "${path}" NAME)
  112. set(OPENCV_ANDROID_CMAKE_EXTRA_ARGS "")
  113. if(DEFINED ANDROID_TOOLCHAIN)
  114. set(OPENCV_ANDROID_CMAKE_EXTRA_ARGS "${OPENCV_ANDROID_CMAKE_EXTRA_ARGS},\n\"-DANDROID_TOOLCHAIN=${ANDROID_TOOLCHAIN}\"")
  115. endif()
  116. if(DEFINED ANDROID_STL)
  117. set(OPENCV_ANDROID_CMAKE_EXTRA_ARGS "${OPENCV_ANDROID_CMAKE_EXTRA_ARGS},\n\"-DANDROID_STL=${ANDROID_STL}\"")
  118. endif()
  119. #
  120. # Build
  121. #
  122. set(ANDROID_SAMPLE_JNI_PATH "${path}/jni")
  123. set(ANDROID_SAMPLE_JAVA_PATH "${path}/src")
  124. set(ANDROID_SAMPLE_RES_PATH "${path}/res")
  125. set(ANDROID_SAMPLE_MANIFEST_PATH "${path}/gradle/AndroidManifest.xml")
  126. set(ANDROID_ABI_FILTER "${ANDROID_BUILD_ABI_FILTER}")
  127. set(ANDROID_PROJECT_JNI_PATH "../../")
  128. string(REPLACE ";" "', '" ANDROID_SAMPLE_JAVA_PATH "['${ANDROID_SAMPLE_JAVA_PATH}']")
  129. string(REPLACE ";" "', '" ANDROID_SAMPLE_RES_PATH "['${ANDROID_SAMPLE_RES_PATH}']")
  130. configure_file("${path}/build.gradle.in" "${ANDROID_BUILD_BASE_DIR}/${__dir}/build.gradle" @ONLY)
  131. file(APPEND "${ANDROID_BUILD_BASE_DIR}/settings.gradle" "
  132. include ':${__dir}'
  133. ")
  134. if (BUILD_ANDROID_EXAMPLES)
  135. # build apk
  136. set(APK_FILE "${ANDROID_BUILD_BASE_DIR}/${__dir}/build/outputs/apk/release/${__dir}-${ANDROID_ABI}-release-unsigned.apk")
  137. ocv_update(OPENCV_GRADLE_VERBOSE_OPTIONS "-i")
  138. add_custom_command(
  139. OUTPUT "${APK_FILE}" "${OPENCV_DEPHELPER}/android_sample_${__dir}"
  140. COMMAND ./gradlew ${OPENCV_GRADLE_VERBOSE_OPTIONS} "${__dir}:assemble"
  141. COMMAND ${CMAKE_COMMAND} -E touch "${OPENCV_DEPHELPER}/android_sample_${__dir}"
  142. WORKING_DIRECTORY "${ANDROID_BUILD_BASE_DIR}"
  143. DEPENDS ${depends} opencv_java_android
  144. COMMENT "Building OpenCV Android sample project: ${__dir}"
  145. )
  146. else() # install only
  147. # copy samples
  148. add_custom_command(
  149. OUTPUT "${OPENCV_DEPHELPER}/android_sample_${__dir}"
  150. COMMAND ${CMAKE_COMMAND} -E touch "${OPENCV_DEPHELPER}/android_sample_${__dir}"
  151. WORKING_DIRECTORY "${ANDROID_BUILD_BASE_DIR}"
  152. DEPENDS ${depends} opencv_java_android
  153. COMMENT "Copying OpenCV Android sample project: ${__dir}"
  154. )
  155. endif()
  156. file(REMOVE "${OPENCV_DEPHELPER}/android_sample_${__dir}") # force rebuild after CMake run
  157. add_custom_target(android_sample_${__dir} ALL DEPENDS "${OPENCV_DEPHELPER}/android_sample_${__dir}" SOURCES "${ANDROID_SAMPLE_MANIFEST_PATH}")
  158. #
  159. # Install
  160. #
  161. set(ANDROID_SAMPLE_JNI_PATH "jni")
  162. set(ANDROID_SAMPLE_JAVA_PATH "src")
  163. set(ANDROID_SAMPLE_RES_PATH "res")
  164. set(ANDROID_SAMPLE_MANIFEST_PATH "AndroidManifest.xml")
  165. install(DIRECTORY "${path}/res" DESTINATION "${ANDROID_INSTALL_SAMPLES_DIR}/${__dir}" COMPONENT samples OPTIONAL)
  166. install(DIRECTORY "${path}/src" DESTINATION "${ANDROID_INSTALL_SAMPLES_DIR}/${__dir}" COMPONENT samples)
  167. install(DIRECTORY "${path}/jni" DESTINATION "${ANDROID_INSTALL_SAMPLES_DIR}/${__dir}" COMPONENT samples OPTIONAL)
  168. install(FILES "${path}/gradle/AndroidManifest.xml" DESTINATION "${ANDROID_INSTALL_SAMPLES_DIR}/${__dir}" COMPONENT samples)
  169. set(ANDROID_ABI_FILTER "${ANDROID_INSTALL_ABI_FILTER}")
  170. set(ANDROID_PROJECT_JNI_PATH "native/jni")
  171. string(REPLACE ";" "', '" ANDROID_SAMPLE_JAVA_PATH "['${ANDROID_SAMPLE_JAVA_PATH}']")
  172. string(REPLACE ";" "', '" ANDROID_SAMPLE_RES_PATH "['${ANDROID_SAMPLE_RES_PATH}']")
  173. configure_file("${path}/build.gradle.in" "${ANDROID_TMP_INSTALL_BASE_DIR}/${__dir}/build.gradle" @ONLY)
  174. install(FILES "${ANDROID_TMP_INSTALL_BASE_DIR}/${__dir}/build.gradle" DESTINATION "${ANDROID_INSTALL_SAMPLES_DIR}/${__dir}" COMPONENT samples)
  175. file(APPEND "${ANDROID_TMP_INSTALL_BASE_DIR}/settings.gradle" "
  176. include ':${__dir}'
  177. ")
  178. endmacro()
  179. install(FILES "${ANDROID_TMP_INSTALL_BASE_DIR}/settings.gradle" DESTINATION "${ANDROID_INSTALL_SAMPLES_DIR}" COMPONENT samples)