1 if(EXISTS "${ANDROID_EXECUTABLE}")
2 set(ANDROID_SDK_DETECT_QUIET TRUE)
5 file(TO_CMAKE_PATH "$ENV{ProgramFiles}" ProgramFiles_ENV_PATH)
6 file(TO_CMAKE_PATH "$ENV{HOME}" HOME_ENV_PATH)
9 set(ANDROID_SDK_OS windows)
10 elseif(CMAKE_HOST_APPLE)
11 set(ANDROID_SDK_OS macosx)
13 set(ANDROID_SDK_OS linux)
16 #find android SDK: search in ANDROID_SDK first
17 find_host_program(ANDROID_EXECUTABLE
18 NAMES android.bat android
22 DOC "Android SDK location"
26 # Now search default paths
27 find_host_program(ANDROID_EXECUTABLE
28 NAMES android.bat android
29 PATH_SUFFIXES android-sdk-${ANDROID_SDK_OS}/tools
30 android-sdk-${ANDROID_SDK_OS}_x86/tools
31 android-sdk-${ANDROID_SDK_OS}_86/tools
34 "${HOME_ENV_PATH}/NVPACK"
35 "$ENV{SystemDrive}/NVPACK"
36 "${ProgramFiles_ENV_PATH}/Android"
37 DOC "Android SDK location"
40 if(ANDROID_EXECUTABLE)
41 if(NOT ANDROID_SDK_DETECT_QUIET)
42 message(STATUS "Found android tool: ${ANDROID_EXECUTABLE}")
45 get_filename_component(ANDROID_SDK_TOOLS_PATH "${ANDROID_EXECUTABLE}" PATH)
47 #read source.properties
48 if(EXISTS "${ANDROID_SDK_TOOLS_PATH}/source.properties")
49 file(STRINGS "${ANDROID_SDK_TOOLS_PATH}/source.properties" ANDROID_SDK_TOOLS_SOURCE_PROPERTIES_LINES REGEX "^[ ]*[^#].*$")
50 foreach(line ${ANDROID_SDK_TOOLS_SOURCE_PROPERTIES_LINES})
51 string(REPLACE "\\:" ":" line ${line})
52 string(REPLACE "=" ";" line ${line})
53 list(GET line 0 line_name)
54 list(GET line 1 line_value)
55 string(REPLACE "." "_" line_name ${line_name})
56 SET(ANDROID_TOOLS_${line_name} "${line_value}" CACHE INTERNAL "from ${ANDROID_SDK_TOOLS_PATH}/source.properties")
57 MARK_AS_ADVANCED(ANDROID_TOOLS_${line_name})
61 #fix missing revision (SDK tools before r9 don't set revision number correctly)
62 if(NOT ANDROID_TOOLS_Pkg_Revision)
63 SET(ANDROID_TOOLS_Pkg_Revision "Unknown" CACHE INTERNAL "")
64 MARK_AS_ADVANCED(ANDROID_TOOLS_Pkg_Revision)
67 #fix missing description
68 if(NOT ANDROID_TOOLS_Pkg_Desc)
69 SET(ANDROID_TOOLS_Pkg_Desc "Android SDK Tools, revision ${ANDROID_TOOLS_Pkg_Revision}." CACHE INTERNAL "")
70 MARK_AS_ADVANCED(ANDROID_TOOLS_Pkg_Desc)
73 #warn about outdated SDK
74 if(NOT ANDROID_TOOLS_Pkg_Revision GREATER 13)
75 SET(ANDROID_TOOLS_Pkg_Desc "${ANDROID_TOOLS_Pkg_Desc} It is recommended to update your SDK tools to revision 14 or newer." CACHE INTERNAL "")
78 if(ANDROID_TOOLS_Pkg_Revision GREATER 13)
79 SET(ANDROID_PROJECT_PROPERTIES_FILE project.properties)
80 SET(ANDROID_ANT_PROPERTIES_FILE ant.properties)
82 SET(ANDROID_PROJECT_PROPERTIES_FILE default.properties)
83 SET(ANDROID_ANT_PROPERTIES_FILE build.properties)
86 set(ANDROID_MANIFEST_FILE AndroidManifest.xml)
87 set(ANDROID_LIB_PROJECT_FILES build.xml local.properties proguard-project.txt ${ANDROID_PROJECT_PROPERTIES_FILE})
88 set(ANDROID_PROJECT_FILES ${ANDROID_LIB_PROJECT_FILES})
90 #get installed targets
91 if(ANDROID_TOOLS_Pkg_Revision GREATER 11)
92 execute_process(COMMAND ${ANDROID_EXECUTABLE} list target -c
93 RESULT_VARIABLE ANDROID_PROCESS
94 OUTPUT_VARIABLE ANDROID_SDK_TARGETS
95 ERROR_VARIABLE ANDROID_PROCESS_ERRORS
96 OUTPUT_STRIP_TRAILING_WHITESPACE
98 string(REGEX MATCHALL "[^\n]+" ANDROID_SDK_TARGETS "${ANDROID_SDK_TARGETS}")
100 #old SDKs (r11 and older) don't provide compact list
101 execute_process(COMMAND ${ANDROID_EXECUTABLE} list target
102 RESULT_VARIABLE ANDROID_PROCESS
103 OUTPUT_VARIABLE ANDROID_SDK_TARGETS_FULL
104 ERROR_VARIABLE ANDROID_PROCESS_ERRORS
105 OUTPUT_STRIP_TRAILING_WHITESPACE
107 string(REGEX MATCHALL "(^|\n)id: [0-9]+ or \"([^\n]+[0-9+])\"(\n|$)" ANDROID_SDK_TARGETS_FULL "${ANDROID_SDK_TARGETS_FULL}")
109 SET(ANDROID_SDK_TARGETS "")
110 if(ANDROID_PROCESS EQUAL 0)
111 foreach(line ${ANDROID_SDK_TARGETS_FULL})
112 string(REGEX REPLACE "(^|\n)id: [0-9]+ or \"([^\n]+[0-9+])\"(\n|$)" "\\2" line "${line}")
113 list(APPEND ANDROID_SDK_TARGETS "${line}")
118 if(NOT ANDROID_PROCESS EQUAL 0)
119 message(ERROR "Failed to get list of installed Android targets.")
120 set(ANDROID_EXECUTABLE "ANDROID_EXECUTABLE-NOTFOUND")
123 # clear ANDROID_SDK_TARGET if no target is provided by user
124 if(NOT ANDROID_SDK_TARGET)
125 set(ANDROID_SDK_TARGET "" CACHE STRING "Android SDK target for the OpenCV Java API and samples")
127 if(ANDROID_SDK_TARGETS AND CMAKE_VERSION VERSION_GREATER "2.8")
128 set_property( CACHE ANDROID_SDK_TARGET PROPERTY STRINGS ${ANDROID_SDK_TARGETS} )
130 endif(ANDROID_EXECUTABLE)
132 # finds minimal installed SDK target compatible with provided names or API levels
134 # get_compatible_android_api_level(VARIABLE [level1] [level2] ...)
135 macro(android_get_compatible_target VAR)
136 set(${VAR} "${VAR}-NOTFOUND")
137 if(ANDROID_SDK_TARGETS)
138 list(GET ANDROID_SDK_TARGETS 0 __lvl)
139 string(REGEX MATCH "[0-9]+$" __lvl "${__lvl}")
141 #find minimal level mathing to all provided levels
143 string(REGEX MATCH "[0-9]+$" __level "${lvl}")
144 if(__level GREATER __lvl)
145 set(__lvl ${__level})
149 #search for compatible levels
150 foreach(lvl ${ANDROID_SDK_TARGETS})
151 string(REGEX MATCH "[0-9]+$" __level "${lvl}")
152 if(__level EQUAL __lvl)
153 #look for exact match
154 foreach(usrlvl ${ARGN})
155 if("${usrlvl}" STREQUAL "${lvl}")
160 if("${${VAR}}" STREQUAL "${lvl}")
161 break() #exact match was found
165 elseif(__level GREATER __lvl)
178 unset(__android_project_chain CACHE)
180 # add_android_project(target_name ${path} NATIVE_DEPS opencv_core LIBRARY_DEPS ${OpenCV_BINARY_DIR} SDK_TARGET 11)
181 macro(add_android_project target path)
183 set(android_proj_arglist NATIVE_DEPS LIBRARY_DEPS SDK_TARGET IGNORE_JAVA IGNORE_MANIFEST)
184 set(__varname "android_proj_")
185 foreach(v ${android_proj_arglist})
186 set(${__varname}${v} "")
189 set(__var "${__varname}")
190 foreach(v ${android_proj_arglist})
191 if("${v}" STREQUAL "${arg}")
192 set(__varname "android_proj_${v}")
196 if(__var STREQUAL __varname)
197 list(APPEND ${__var} "${arg}")
201 # get compatible SDK target
202 android_get_compatible_target(android_proj_sdk_target ${ANDROID_NATIVE_API_LEVEL} ${android_proj_SDK_TARGET})
204 if(NOT android_proj_sdk_target)
205 message(WARNING "Can not find any SDK target compatible with: ${ANDROID_NATIVE_API_LEVEL} ${android_proj_SDK_TARGET}
206 The project ${target} will not be build")
209 # check native dependencies
210 if(android_proj_IGNORE_JAVA)
211 ocv_check_dependencies(${android_proj_NATIVE_DEPS})
213 ocv_check_dependencies(${android_proj_NATIVE_DEPS} opencv_java)
216 if(EXISTS "${path}/jni/Android.mk" )
217 # find if native_app_glue is used
218 file(STRINGS "${path}/jni/Android.mk" NATIVE_APP_GLUE REGEX ".*(call import-module,android/native_app_glue)" )
220 if(ANDROID_NATIVE_API_LEVEL LESS 9 OR NOT EXISTS "${ANDROID_NDK}/sources/android/native_app_glue")
221 set(OCV_DEPENDENCIES_FOUND FALSE)
226 if(OCV_DEPENDENCIES_FOUND AND android_proj_sdk_target AND ANDROID_EXECUTABLE AND ANT_EXECUTABLE AND ANDROID_TOOLS_Pkg_Revision GREATER 13 AND EXISTS "${path}/${ANDROID_MANIFEST_FILE}")
229 set(android_proj_bin_dir "${CMAKE_CURRENT_BINARY_DIR}/.build")
231 # get project sources
232 file(GLOB_RECURSE android_proj_files RELATIVE "${path}" "${path}/res/*" "${path}/src/*")
234 if(NOT android_proj_IGNORE_MANIFEST)
235 list(APPEND android_proj_files ${ANDROID_MANIFEST_FILE})
238 # copy sources out from the build tree
239 set(android_proj_file_deps "")
240 foreach(f ${android_proj_files})
242 OUTPUT "${android_proj_bin_dir}/${f}"
243 COMMAND ${CMAKE_COMMAND} -E copy "${path}/${f}" "${android_proj_bin_dir}/${f}"
244 MAIN_DEPENDENCY "${path}/${f}"
245 COMMENT "Copying ${f}")
246 list(APPEND android_proj_file_deps "${path}/${f}" "${android_proj_bin_dir}/${f}")
249 set(android_proj_lib_deps_commands "")
250 set(android_proj_target_files ${ANDROID_PROJECT_FILES})
251 ocv_list_add_prefix(android_proj_target_files "${android_proj_bin_dir}/")
253 # process Android library dependencies
254 foreach(dep ${android_proj_LIBRARY_DEPS})
255 file(RELATIVE_PATH __dep "${android_proj_bin_dir}" "${dep}")
256 list(APPEND android_proj_lib_deps_commands
257 COMMAND ${ANDROID_EXECUTABLE} --silent update project --path "${android_proj_bin_dir}" --library "${__dep}")
260 # fix Android project
262 OUTPUT ${android_proj_target_files}
263 COMMAND ${CMAKE_COMMAND} -E remove ${android_proj_target_files}
264 COMMAND ${ANDROID_EXECUTABLE} --silent update project --path "${android_proj_bin_dir}" --target "${android_proj_sdk_target}" --name "${target}"
265 ${android_proj_lib_deps_commands}
266 MAIN_DEPENDENCY "${android_proj_bin_dir}/${ANDROID_MANIFEST_FILE}"
267 DEPENDS "${path}/${ANDROID_MANIFEST_FILE}"
268 COMMENT "Updating Android project at ${path}. SDK target: ${android_proj_sdk_target}"
271 list(APPEND android_proj_file_deps ${android_proj_target_files})
274 file(GLOB_RECURSE android_proj_jni_files "${path}/jni/*.c" "${path}/jni/*.h" "${path}/jni/*.cpp" "${path}/jni/*.hpp")
275 ocv_list_filterout(android_proj_jni_files "\\\\.svn")
277 if(android_proj_jni_files AND EXISTS ${path}/jni/Android.mk AND NOT DEFINED JNI_LIB_NAME)
278 # find local module name in Android.mk file to build native lib
279 file(STRINGS "${path}/jni/Android.mk" JNI_LIB_NAME REGEX "LOCAL_MODULE[ ]*:=[ ]*.*" )
280 string(REGEX REPLACE "LOCAL_MODULE[ ]*:=[ ]*([a-zA-Z_][a-zA-Z_0-9]*)[ ]*" "\\1" JNI_LIB_NAME "${JNI_LIB_NAME}")
283 ocv_include_modules_recurse(${android_proj_NATIVE_DEPS})
284 ocv_include_directories("${path}/jni")
287 include_directories(${ANDROID_NDK}/sources/android/native_app_glue)
288 list(APPEND android_proj_jni_files ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c)
289 ocv_warnings_disable(CMAKE_C_FLAGS -Wstrict-prototypes -Wunused-parameter -Wmissing-prototypes)
290 set(android_proj_NATIVE_DEPS ${android_proj_NATIVE_DEPS} android)
293 add_library(${JNI_LIB_NAME} MODULE ${android_proj_jni_files})
294 target_link_libraries(${JNI_LIB_NAME} ${OPENCV_LINKER_LIBS} ${android_proj_NATIVE_DEPS})
296 set_target_properties(${JNI_LIB_NAME} PROPERTIES
297 OUTPUT_NAME "${JNI_LIB_NAME}"
298 LIBRARY_OUTPUT_DIRECTORY "${android_proj_bin_dir}/libs/${ANDROID_NDK_ABI_NAME}"
301 get_target_property(android_proj_jni_location "${JNI_LIB_NAME}" LOCATION)
302 if (NOT (CMAKE_BUILD_TYPE MATCHES "debug"))
303 add_custom_command(TARGET ${JNI_LIB_NAME} POST_BUILD COMMAND ${CMAKE_STRIP} --strip-unneeded "${android_proj_jni_location}")
309 if(android_proj_IGNORE_JAVA)
311 OUTPUT "${android_proj_bin_dir}/bin/${target}-debug.apk"
312 COMMAND ${ANT_EXECUTABLE} -q -noinput -k debug
313 COMMAND ${CMAKE_COMMAND} -E touch "${android_proj_bin_dir}/bin/${target}-debug.apk" # needed because ant does not update the timestamp of updated apk
314 WORKING_DIRECTORY "${android_proj_bin_dir}"
315 MAIN_DEPENDENCY "${android_proj_bin_dir}/${ANDROID_MANIFEST_FILE}"
316 DEPENDS ${android_proj_file_deps} ${JNI_LIB_NAME})
319 OUTPUT "${android_proj_bin_dir}/bin/${target}-debug.apk"
320 COMMAND ${ANT_EXECUTABLE} -q -noinput -k debug
321 COMMAND ${CMAKE_COMMAND} -E touch "${android_proj_bin_dir}/bin/${target}-debug.apk" # needed because ant does not update the timestamp of updated apk
322 WORKING_DIRECTORY "${android_proj_bin_dir}"
323 MAIN_DEPENDENCY "${android_proj_bin_dir}/${ANDROID_MANIFEST_FILE}"
324 DEPENDS "${OpenCV_BINARY_DIR}/bin/classes.jar.dephelper" opencv_java # as we are part of OpenCV we can just force this dependency
325 DEPENDS ${android_proj_file_deps} ${JNI_LIB_NAME})
330 add_custom_target(${target} ALL SOURCES "${android_proj_bin_dir}/bin/${target}-debug.apk" )
331 if(NOT android_proj_IGNORE_JAVA)
332 add_dependencies(${target} opencv_java)
334 if(android_proj_native_deps)
335 add_dependencies(${target} ${android_proj_native_deps})
338 if(__android_project_chain)
339 add_dependencies(${target} ${__android_project_chain})
341 set(__android_project_chain ${target} CACHE INTERNAL "auxiliary variable used for Android progects chaining")
343 # put the final .apk to the OpenCV's bin folder
344 add_custom_command(TARGET ${target} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy "${android_proj_bin_dir}/bin/${target}-debug.apk" "${OpenCV_BINARY_DIR}/bin/${target}.apk")
345 if(INSTALL_ANDROID_EXAMPLES AND "${target}" MATCHES "^example-")
347 install(FILES "${OpenCV_BINARY_DIR}/bin/${target}.apk" DESTINATION "samples" COMPONENT main)
348 get_filename_component(sample_dir "${path}" NAME)
350 list(REMOVE_ITEM android_proj_files ${ANDROID_MANIFEST_FILE})
351 foreach(f ${android_proj_files} ${ANDROID_MANIFEST_FILE})
352 get_filename_component(install_subdir "${f}" PATH)
353 install(FILES "${android_proj_bin_dir}/${f}" DESTINATION "samples/${sample_dir}/${install_subdir}" COMPONENT main)
355 #jni part + eclipse files
356 file(GLOB_RECURSE jni_files RELATIVE "${path}" "${path}/jni/*" "${path}/.cproject")
357 ocv_list_filterout(jni_files "\\\\.svn")
358 foreach(f ${jni_files} ".classpath" ".project" ".settings/org.eclipse.jdt.core.prefs")
359 get_filename_component(install_subdir "${f}" PATH)
360 install(FILES "${path}/${f}" DESTINATION "samples/${sample_dir}/${install_subdir}" COMPONENT main)
363 if(android_proj_lib_deps_commands)
364 set(inst_lib_opt " --library ../../sdk/java")
366 install(CODE "EXECUTE_PROCESS(COMMAND ${ANDROID_EXECUTABLE} --silent update project --path . --target \"${android_proj_sdk_target}\" --name \"${target}\" ${inst_lib_opt}
367 WORKING_DIRECTORY \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/samples/${sample_dir}\"
370 install(CODE "MAKE_DIRECTORY(\"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/samples/${sample_dir}/gen\")" COMPONENT main)