1 file(TO_CMAKE_PATH "$ENV{ProgramFiles}" ProgramFiles_ENV_PATH)
2 file(TO_CMAKE_PATH "$ENV{ANDROID_SDK}" ANDROID_SDK_ENV_PATH)
5 find_host_program(ANDROID_EXECUTABLE
6 NAMES android.bat android
7 PATHS "${ANDROID_SDK_ENV_PATH}/tools/"
8 "${ProgramFiles_ENV_PATH}/Android/android-sdk/tools/"
9 "/opt/android-sdk/tools/"
10 "/opt/android-sdk-linux_x86/tools/"
11 "/opt/android-sdk-mac_x86/tools/"
12 "/opt/android-sdk-linux_86/tools/"
13 "/opt/android-sdk-mac_86/tools/"
16 if(ANDROID_EXECUTABLE)
17 message(STATUS " Found android tool: ${ANDROID_EXECUTABLE}")
18 get_filename_component(ANDROID_SDK_TOOLS_PATH "${ANDROID_EXECUTABLE}" PATH)
20 #read source.properties
21 if(EXISTS "${ANDROID_SDK_TOOLS_PATH}/source.properties")
22 file(STRINGS "${ANDROID_SDK_TOOLS_PATH}/source.properties" ANDROID_SDK_TOOLS_SOURCE_PROPERTIES_LINES REGEX "^[ ]*[^#].*$")
23 foreach(line ${ANDROID_SDK_TOOLS_SOURCE_PROPERTIES_LINES})
24 string(REPLACE "\\:" ":" line ${line})
25 string(REPLACE "=" ";" line ${line})
26 list(GET line 0 line_name)
27 list(GET line 1 line_value)
28 string(REPLACE "." "_" line_name ${line_name})
29 SET(ANDROID_TOOLS_${line_name} "${line_value}")
30 MARK_AS_ADVANCED(ANDROID_TOOLS_${line_name})
34 if(NOT ANDROID_TOOLS_Pkg_Revision)
35 SET(ANDROID_TOOLS_Pkg_Revision "Unknown")
36 MARK_AS_ADVANCED(ANDROID_TOOLS_Pkg_Revision)
39 if(NOT ANDROID_TOOLS_Pkg_Desc)
40 SET(ANDROID_TOOLS_Pkg_Desc "Android SDK Tools, revision ${ANDROID_TOOLS_Pkg_Revision}.")
41 if(NOT ANDROID_TOOLS_Pkg_Revision GREATER 11)
42 SET(ANDROID_TOOLS_Pkg_Desc "${ANDROID_TOOLS_Pkg_Desc} It is recommended to update your SDK tools to revision 12 or newer.")
44 MARK_AS_ADVANCED(ANDROID_TOOLS_Pkg_Desc)
47 #get installed targets
48 execute_process(COMMAND ${ANDROID_EXECUTABLE} list target
49 RESULT_VARIABLE ANDROID_PROCESS
50 OUTPUT_VARIABLE ANDROID_SDK_TARGETS_FULL
51 ERROR_VARIABLE ANDROID_PROCESS_ERRORS
52 OUTPUT_STRIP_TRAILING_WHITESPACE
54 string(REGEX MATCHALL "(^|\n)id: [0-9]+ or \"([^\n]+[0-9+])\"(\n|$)" ANDROID_SDK_TARGETS_FULL "${ANDROID_SDK_TARGETS_FULL}")
56 SET(ANDROID_SDK_TARGETS "")
57 if(ANDROID_PROCESS EQUAL 0)
58 foreach(line ${ANDROID_SDK_TARGETS_FULL})
59 string(REGEX REPLACE "(^|\n)id: [0-9]+ or \"([^\n]+[0-9+])\"(\n|$)" "\\2" line "${line}")
60 list(APPEND ANDROID_SDK_TARGETS "${line}")
64 # detect ANDROID_SDK_TARGET if no target is provided by user
65 if(NOT ANDROID_SDK_TARGET)
66 set(desired_android_target_level ${ANDROID_NATIVE_API_LEVEL})
67 if(desired_android_target_level LESS 8)
68 set(desired_android_target_level 8)
70 if(ANDROID_PROCESS EQUAL 0)
71 math(EXPR desired_android_target_level_1 "${desired_android_target_level}-1")
73 foreach(target ${ANDROID_SDK_TARGETS})
74 string(REGEX MATCH "[0-9]+$" target_level "${target}")
75 if(target_level GREATER desired_android_target_level_1)
76 set(ANDROID_SDK_TARGET "${target}")
81 set(ANDROID_SDK_TARGET android-${desired_android_target_level})
82 message(WARNING "Could not retrieve list of installed Android targets. Will try to use \"${ANDROID_SDK_TARGET}\" target")
84 endif(NOT ANDROID_SDK_TARGET)
86 SET(ANDROID_SDK_TARGET "${ANDROID_SDK_TARGET}" CACHE STRING "SDK target for Android tests and samples")
87 if(ANDROID_PROCESS EQUAL 0 AND CMAKE_VERSION VERSION_GREATER "2.8")
88 set_property( CACHE ANDROID_SDK_TARGET PROPERTY STRINGS ${ANDROID_SDK_TARGETS} )
90 string(REGEX MATCH "[0-9]+$" ANDROID_SDK_TARGET_LEVEL "${ANDROID_SDK_TARGET}")
91 endif(ANDROID_EXECUTABLE)