android_gradle_projects.cmake 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_GRADLE_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. configure_file("${OpenCV_SOURCE_DIR}/samples/android/build.gradle.in" "${ANDROID_BUILD_BASE_DIR}/build.gradle" @ONLY)
  45. set(ANDROID_ABI_FILTER "${ANDROID_INSTALL_ABI_FILTER}")
  46. configure_file("${OpenCV_SOURCE_DIR}/samples/android/build.gradle.in" "${ANDROID_TMP_INSTALL_BASE_DIR}/${ANDROID_INSTALL_SAMPLES_DIR}/build.gradle" @ONLY)
  47. install(FILES "${ANDROID_TMP_INSTALL_BASE_DIR}/${ANDROID_INSTALL_SAMPLES_DIR}/build.gradle" DESTINATION "${ANDROID_INSTALL_SAMPLES_DIR}" COMPONENT samples)
  48. 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)
  49. install(FILES "${ANDROID_BUILD_BASE_DIR}/gradle/wrapper/gradle-wrapper.properties" DESTINATION "${ANDROID_INSTALL_SAMPLES_DIR}/gradle/wrapper" COMPONENT samples)
  50. set(GRADLE_WRAPPER_FILES
  51. "gradle/wrapper/gradle-wrapper.jar"
  52. "gradlew.bat"
  53. "gradlew"
  54. "gradle.properties"
  55. )
  56. foreach(fname ${GRADLE_WRAPPER_FILES})
  57. get_filename_component(__dir "${fname}" DIRECTORY)
  58. set(__permissions "")
  59. set(__permissions_prefix "")
  60. if(fname STREQUAL "gradlew")
  61. set(__permissions FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
  62. endif()
  63. file(COPY "${OpenCV_SOURCE_DIR}/platforms/android/gradle-wrapper/${fname}" DESTINATION "${ANDROID_BUILD_BASE_DIR}/${__dir}" ${__permissions})
  64. string(REPLACE "FILE_PERMISSIONS" "PERMISSIONS" __permissions "${__permissions}")
  65. if("${__dir}" STREQUAL "")
  66. set(__dir ".")
  67. endif()
  68. install(FILES "${OpenCV_SOURCE_DIR}/platforms/android/gradle-wrapper/${fname}" DESTINATION "${ANDROID_INSTALL_SAMPLES_DIR}/${__dir}" COMPONENT samples ${__permissions})
  69. endforeach()
  70. file(WRITE "${ANDROID_BUILD_BASE_DIR}/settings.gradle" "
  71. include ':opencv'
  72. ")
  73. file(WRITE "${ANDROID_TMP_INSTALL_BASE_DIR}/settings.gradle" "
  74. rootProject.name = 'opencv_samples'
  75. def opencvsdk='../'
  76. //def opencvsdk='/<path to OpenCV-android-sdk>'
  77. //println opencvsdk
  78. include ':opencv'
  79. project(':opencv').projectDir = new File(opencvsdk + '/sdk')
  80. ")
  81. macro(add_android_project target path)
  82. get_filename_component(__dir "${path}" NAME)
  83. set(OPENCV_ANDROID_CMAKE_EXTRA_ARGS "")
  84. if(DEFINED ANDROID_TOOLCHAIN)
  85. set(OPENCV_ANDROID_CMAKE_EXTRA_ARGS "${OPENCV_ANDROID_CMAKE_EXTRA_ARGS},\n\"-DANDROID_TOOLCHAIN=${ANDROID_TOOLCHAIN}\"")
  86. endif()
  87. if(DEFINED ANDROID_STL)
  88. set(OPENCV_ANDROID_CMAKE_EXTRA_ARGS "${OPENCV_ANDROID_CMAKE_EXTRA_ARGS},\n\"-DANDROID_STL=${ANDROID_STL}\"")
  89. endif()
  90. #
  91. # Build
  92. #
  93. set(ANDROID_SAMPLE_JNI_PATH "${path}/jni")
  94. set(ANDROID_SAMPLE_JAVA_PATH "${path}/src")
  95. set(ANDROID_SAMPLE_RES_PATH "${path}/res")
  96. set(ANDROID_SAMPLE_MANIFEST_PATH "${path}/gradle/AndroidManifest.xml")
  97. set(ANDROID_ABI_FILTER "${ANDROID_BUILD_ABI_FILTER}")
  98. set(ANDROID_PROJECT_JNI_PATH "../../")
  99. string(REPLACE ";" "', '" ANDROID_SAMPLE_JAVA_PATH "['${ANDROID_SAMPLE_JAVA_PATH}']")
  100. string(REPLACE ";" "', '" ANDROID_SAMPLE_RES_PATH "['${ANDROID_SAMPLE_RES_PATH}']")
  101. configure_file("${path}/build.gradle.in" "${ANDROID_BUILD_BASE_DIR}/${__dir}/build.gradle" @ONLY)
  102. file(APPEND "${ANDROID_BUILD_BASE_DIR}/settings.gradle" "
  103. include ':${__dir}'
  104. ")
  105. if (BUILD_ANDROID_EXAMPLES)
  106. # build apk
  107. set(APK_FILE "${ANDROID_BUILD_BASE_DIR}/${__dir}/build/outputs/apk/release/${__dir}-${ANDROID_ABI}-release-unsigned.apk")
  108. ocv_update(OPENCV_GRADLE_VERBOSE_OPTIONS "-i")
  109. add_custom_command(
  110. OUTPUT "${APK_FILE}" "${OPENCV_DEPHELPER}/android_sample_${__dir}"
  111. COMMAND ./gradlew ${OPENCV_GRADLE_VERBOSE_OPTIONS} "${__dir}:assemble"
  112. COMMAND ${CMAKE_COMMAND} -E touch "${OPENCV_DEPHELPER}/android_sample_${__dir}"
  113. WORKING_DIRECTORY "${ANDROID_BUILD_BASE_DIR}"
  114. DEPENDS ${depends} opencv_java_android
  115. COMMENT "Building OpenCV Android sample project: ${__dir}"
  116. )
  117. else() # install only
  118. # copy samples
  119. add_custom_command(
  120. OUTPUT "${OPENCV_DEPHELPER}/android_sample_${__dir}"
  121. COMMAND ${CMAKE_COMMAND} -E touch "${OPENCV_DEPHELPER}/android_sample_${__dir}"
  122. WORKING_DIRECTORY "${ANDROID_BUILD_BASE_DIR}"
  123. DEPENDS ${depends} opencv_java_android
  124. COMMENT "Copying OpenCV Android sample project: ${__dir}"
  125. )
  126. endif()
  127. file(REMOVE "${OPENCV_DEPHELPER}/android_sample_${__dir}") # force rebuild after CMake run
  128. add_custom_target(android_sample_${__dir} ALL DEPENDS "${OPENCV_DEPHELPER}/android_sample_${__dir}" SOURCES "${ANDROID_SAMPLE_MANIFEST_PATH}")
  129. #
  130. # Install
  131. #
  132. set(ANDROID_SAMPLE_JNI_PATH "jni")
  133. set(ANDROID_SAMPLE_JAVA_PATH "src")
  134. set(ANDROID_SAMPLE_RES_PATH "res")
  135. set(ANDROID_SAMPLE_MANIFEST_PATH "AndroidManifest.xml")
  136. install(DIRECTORY "${path}/res" DESTINATION "${ANDROID_INSTALL_SAMPLES_DIR}/${__dir}" COMPONENT samples OPTIONAL)
  137. install(DIRECTORY "${path}/src" DESTINATION "${ANDROID_INSTALL_SAMPLES_DIR}/${__dir}" COMPONENT samples)
  138. install(DIRECTORY "${path}/jni" DESTINATION "${ANDROID_INSTALL_SAMPLES_DIR}/${__dir}" COMPONENT samples OPTIONAL)
  139. install(FILES "${path}/gradle/AndroidManifest.xml" DESTINATION "${ANDROID_INSTALL_SAMPLES_DIR}/${__dir}" COMPONENT samples)
  140. set(ANDROID_ABI_FILTER "${ANDROID_INSTALL_ABI_FILTER}")
  141. set(ANDROID_PROJECT_JNI_PATH "native/jni")
  142. string(REPLACE ";" "', '" ANDROID_SAMPLE_JAVA_PATH "['${ANDROID_SAMPLE_JAVA_PATH}']")
  143. string(REPLACE ";" "', '" ANDROID_SAMPLE_RES_PATH "['${ANDROID_SAMPLE_RES_PATH}']")
  144. configure_file("${path}/build.gradle.in" "${ANDROID_TMP_INSTALL_BASE_DIR}/${__dir}/build.gradle" @ONLY)
  145. install(FILES "${ANDROID_TMP_INSTALL_BASE_DIR}/${__dir}/build.gradle" DESTINATION "${ANDROID_INSTALL_SAMPLES_DIR}/${__dir}" COMPONENT samples)
  146. file(APPEND "${ANDROID_TMP_INSTALL_BASE_DIR}/settings.gradle" "
  147. include ':${__dir}'
  148. ")
  149. endmacro()
  150. install(FILES "${ANDROID_TMP_INSTALL_BASE_DIR}/settings.gradle" DESTINATION "${ANDROID_INSTALL_SAMPLES_DIR}" COMPONENT samples)