2017-12-29 12:15:30 +08:00
project ( ${ the_module } _android )
2018-11-06 01:39:09 +08:00
if ( ANDROID_EXECUTABLE )
set ( OPENCV_JAVA_DIR "${OpenCV_BINARY_DIR}/android_sdk" CACHE INTERNAL "" )
else ( ) # gradle
set ( OPENCV_JAVA_DIR "${ANDROID_BUILD_BASE_DIR}/opencv" CACHE INTERNAL "" )
endif ( )
2017-12-29 12:15:30 +08:00
set ( OPENCV_ANDROID_LIB_DIR "${OPENCV_JAVA_DIR}" CACHE INTERNAL "" ) # for OpenCV samples
file ( REMOVE_RECURSE "${OPENCV_JAVA_DIR}" )
file ( MAKE_DIRECTORY "${OPENCV_JAVA_DIR}/bin" )
set ( java_src_dir "${OPENCV_JAVA_DIR}/src" )
file ( MAKE_DIRECTORY "${java_src_dir}" )
ocv_copyfiles_append_dir ( JAVA_SRC_COPY "${OPENCV_JAVA_BINDINGS_DIR}/gen/java" "${java_src_dir}" )
2018-11-06 01:39:09 +08:00
if ( ANDROID_EXECUTABLE )
ocv_assert ( ANDROID_TOOLS_Pkg_Revision GREATER 13 )
2017-12-29 12:15:30 +08:00
ocv_copyfiles_append_dir ( JAVA_SRC_COPY "${OPENCV_JAVA_BINDINGS_DIR}/gen/android/java" "${java_src_dir}" )
# calc default SDK Target
2018-01-06 13:05:40 +08:00
android_get_compatible_target ( ANDROID_SDK_COMPATIBLE_TARGET ${ ANDROID_NATIVE_API_LEVEL } ${ ANDROID_SDK_TARGET } 14 )
if ( ANDROID_SDK_COMPATIBLE_TARGET )
set ( ANDROID_SDK_COMPATIBLE_TARGET "${ANDROID_SDK_COMPATIBLE_TARGET}" CACHE INTERNAL "" )
endif ( )
string ( REGEX REPLACE "android-" "" android_sdk_target_num ${ ANDROID_SDK_COMPATIBLE_TARGET } )
2017-12-29 12:15:30 +08:00
if ( ( ANDROID_SDK_TARGET AND ANDROID_SDK_TARGET LESS 21 ) OR ( android_sdk_target_num LESS 21 ) )
message ( STATUS "[OpenCV for Android SDK]: A new OpenGL Camera Bridge (CameraGLSurfaceView, CameraGLRendererBase, CameraRenderer, Camera2Renderer) is disabled, because ANDROID_SDK_TARGET (${android_sdk_target_num}) < 21" )
else ( )
ocv_copyfiles_append_dir ( JAVA_SRC_COPY "${OPENCV_JAVA_BINDINGS_DIR}/gen/android-21/java" "${java_src_dir}" )
endif ( )
# copy boilerplate
file ( GLOB_RECURSE seed_project_files_rel RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/android_lib/" "${CMAKE_CURRENT_SOURCE_DIR}/android_lib/*" )
list ( REMOVE_ITEM seed_project_files_rel "${ANDROID_MANIFEST_FILE}" )
foreach ( file ${ seed_project_files_rel } )
configure_file ( "${CMAKE_CURRENT_SOURCE_DIR}/android_lib/${file}" "${OPENCV_JAVA_DIR}/${file}" @ONLY )
list ( APPEND depends "${CMAKE_CURRENT_SOURCE_DIR}/android_lib/${file}" )
get_filename_component ( install_subdir "${file}" PATH )
install ( FILES "${OPENCV_JAVA_DIR}/${file}" DESTINATION "${JAVA_INSTALL_ROOT}/${install_subdir}" COMPONENT java )
endforeach ( )
list ( APPEND depends gen_opencv_java_source "${OPENCV_DEPHELPER}/gen_opencv_java_source" )
ocv_copyfiles_add_target ( ${ the_module } _android_source_copy JAVA_SRC_COPY "Copy Java(Andoid SDK) source files" ${ depends } )
file ( REMOVE "${OPENCV_DEPHELPER}/${the_module}_android_source_copy" ) # force rebuild after CMake run
set ( depends ${ the_module } _android_source_copy "${OPENCV_DEPHELPER}/${the_module}_android_source_copy" )
# generate Android library project
set ( android_sdk_project_files ${ ANDROID_LIB_PROJECT_FILES } ) # build.xml;local.properties;proguard-project.txt;project.properties
ocv_list_add_prefix ( android_sdk_project_files "${OPENCV_JAVA_DIR}/" )
configure_file ( "${CMAKE_CURRENT_SOURCE_DIR}/android_lib/${ANDROID_MANIFEST_FILE}" "${CMAKE_CURRENT_BINARY_DIR}/${ANDROID_MANIFEST_FILE}" @ONLY )
add_custom_command (
O U T P U T $ { a n d r o i d _ s d k _ p r o j e c t _ f i l e s } " $ { O P E N C V _ J A V A _ D I R } / $ { A N D R O I D _ M A N I F E S T _ F I L E } "
C O M M A N D $ { C M A K E _ C O M M A N D } - E r e m o v e $ { a n d r o i d _ s d k _ p r o j e c t _ f i l e s }
C O M M A N D $ { C M A K E _ C O M M A N D } - E c o p y _ i f _ d i f f e r e n t " $ { C M A K E _ C U R R E N T _ B I N A R Y _ D I R } / $ { A N D R O I D _ M A N I F E S T _ F I L E } " " $ { O P E N C V _ J A V A _ D I R } / $ { A N D R O I D _ M A N I F E S T _ F I L E } "
2018-01-06 13:05:40 +08:00
C O M M A N D $ { A N D R O I D _ E X E C U T A B L E } - - s i l e n t c r e a t e l i b - p r o j e c t - - p a t h \ " $ { O P E N C V _ J A V A _ D I R } \ " - - t a r g e t \ " $ { A N D R O I D _ S D K _ C O M P A T I B L E _ T A R G E T } \ " - - n a m e O p e n C V - - p a c k a g e o r g . o p e n c v 2 > \ " $ { C M A K E _ C U R R E N T _ B I N A R Y _ D I R } / c r e a t e _ l i b _ p r o j e c t . l o g \ "
2017-12-29 12:15:30 +08:00
C O M M A N D $ { C M A K E _ C O M M A N D } - E c o p y _ i f _ d i f f e r e n t " $ { C M A K E _ C U R R E N T _ B I N A R Y _ D I R } / $ { A N D R O I D _ M A N I F E S T _ F I L E } " " $ { O P E N C V _ J A V A _ D I R } / $ { A N D R O I D _ M A N I F E S T _ F I L E } "
W O R K I N G _ D I R E C T O R Y " $ { O P E N C V _ J A V A _ D I R } "
M A I N _ D E P E N D E N C Y " $ { C M A K E _ C U R R E N T _ B I N A R Y _ D I R } / $ { A N D R O I D _ M A N I F E S T _ F I L E } "
D E P E N D S $ { d e p e n d s }
C O M M E N T " G e n e r a t i n g O p e n C V A n d r o i d l i b r a r y p r o j e c t . S D K t a r g e t : $ { l i b _ t a r g e t _ s d k _ t a r g e t } "
)
list ( APPEND depends ${ android_sdk_project_files } "${OPENCV_JAVA_DIR}/${ANDROID_MANIFEST_FILE}" )
install ( DIRECTORY "${OPENCV_JAVA_DIR}/src" DESTINATION "${JAVA_INSTALL_ROOT}" COMPONENT java )
install ( FILES "${OPENCV_JAVA_DIR}/${ANDROID_PROJECT_PROPERTIES_FILE}" DESTINATION ${ JAVA_INSTALL_ROOT } COMPONENT java )
install ( FILES "${OPENCV_JAVA_DIR}/${ANDROID_MANIFEST_FILE}" DESTINATION ${ JAVA_INSTALL_ROOT } COMPONENT java )
# build jar
set ( JAR_FILE "${OpenCV_BINARY_DIR}/bin/classes.jar" )
# build the library project
# normally we should do this after a native part, but for a library project we can build the java part first
add_custom_command (
O U T P U T " $ { J A R _ F I L E } " " $ { O P E N C V _ D E P H E L P E R } / $ { t h e _ m o d u l e } _ a n d r o i d "
C O M M A N D $ { A N T _ E X E C U T A B L E } - q - n o i n p u t - k d e b u g - D j a v a . t a r g e t = 1 . 6 - D j a v a . s o u r c e = 1 . 6
C O M M A N D $ { C M A K E _ C O M M A N D } - E t o u c h " $ { O P E N C V _ D E P H E L P E R } / $ { t h e _ m o d u l e } _ a n d r o i d "
W O R K I N G _ D I R E C T O R Y " $ { O P E N C V _ J A V A _ D I R } "
D E P E N D S $ { d e p e n d s }
C O M M E N T " B u i l d i n g O p e n C V A n d r o i d l i b r a r y p r o j e c t "
)
add_custom_target ( ${ the_module } _android DEPENDS "${OPENCV_DEPHELPER}/${the_module}_android" SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/android_lib/${ANDROID_MANIFEST_FILE}" )
add_dependencies ( ${ the_module } ${ the_module } _android )
# empty 'gen' and 'res' folders
install ( CODE "
FILE ( MAKE_DIRECTORY \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${JAVA_INSTALL_ROOT}/gen\")
FILE ( MAKE_DIRECTORY \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${JAVA_INSTALL_ROOT}/res\")
" C O M P O N E N T j a v a )
2018-02-18 06:36:15 +08:00
2018-11-06 01:39:09 +08:00
ocv_update ( ANDROID_COMPILE_SDK_VERSION "27" )
ocv_update ( ANDROID_MIN_SDK_VERSION "14" )
ocv_update ( ANDROID_TARGET_SDK_VERSION "21" )
2018-02-18 06:36:15 +08:00
configure_file ( "${CMAKE_CURRENT_SOURCE_DIR}/build.gradle.in" "${CMAKE_CURRENT_BINARY_DIR}/build.gradle" @ONLY )
install ( FILES "${CMAKE_CURRENT_BINARY_DIR}/build.gradle" DESTINATION ${ JAVA_INSTALL_ROOT } /.. COMPONENT java )
2018-11-06 01:39:09 +08:00
else ( ) # gradle build
#
# Android Gradle-based project
#
#TODO: INSTALL ONLY
ocv_copyfiles_append_dir ( JAVA_SRC_COPY "${OPENCV_JAVA_BINDINGS_DIR}/gen/android/java" "${java_src_dir}" )
ocv_copyfiles_append_dir ( JAVA_SRC_COPY "${OPENCV_JAVA_BINDINGS_DIR}/gen/android-21/java" "${java_src_dir}" )
# copy boilerplate
set ( __base_dir "${CMAKE_CURRENT_SOURCE_DIR}/android_gradle_lib/" )
file ( GLOB_RECURSE seed_project_files_rel RELATIVE "${__base_dir}/" "${__base_dir}/*" )
list ( REMOVE_ITEM seed_project_files_rel "${ANDROID_MANIFEST_FILE}" )
foreach ( file ${ seed_project_files_rel } )
configure_file ( "${__base_dir}/${file}" "${OPENCV_JAVA_DIR}/${file}" @ONLY )
list ( APPEND depends "${__base_dir}/${file}" )
get_filename_component ( install_subdir "${file}" PATH )
2019-03-14 02:52:15 +08:00
if ( NOT file STREQUAL "build.gradle" )
install ( FILES "${OPENCV_JAVA_DIR}/${file}" DESTINATION "${JAVA_INSTALL_ROOT}/${install_subdir}" COMPONENT java )
endif ( )
2018-11-06 01:39:09 +08:00
endforeach ( )
list ( APPEND depends gen_opencv_java_source "${OPENCV_DEPHELPER}/gen_opencv_java_source" )
ocv_copyfiles_add_target ( ${ the_module } _android_source_copy JAVA_SRC_COPY "Copy Java(Andoid SDK) source files" ${ depends } )
file ( REMOVE "${OPENCV_DEPHELPER}/${the_module}_android_source_copy" ) # force rebuild after CMake run
set ( depends ${ the_module } _android_source_copy "${OPENCV_DEPHELPER}/${the_module}_android_source_copy" )
# build jar
set ( AAR_FILE "${OPENCV_JAVA_DIR}/build/outputs/aar/opencv-release.aar" )
ocv_update ( OPENCV_GRADLE_VERBOSE_OPTIONS "-i" )
add_custom_command (
O U T P U T " $ { A A R _ F I L E } " " $ { O P E N C V _ D E P H E L P E R } / $ { t h e _ m o d u l e } _ a n d r o i d "
C O M M A N D . / g r a d l e w $ { O P E N C V _ G R A D L E _ V E R B O S E _ O P T I O N S } " o p e n c v : a s s e m b l e "
C O M M A N D $ { C M A K E _ C O M M A N D } - E t o u c h " $ { O P E N C V _ D E P H E L P E R } / $ { t h e _ m o d u l e } _ a n d r o i d "
W O R K I N G _ D I R E C T O R Y " $ { A N D R O I D _ B U I L D _ B A S E _ D I R } "
D E P E N D S $ { d e p e n d s } $ { t h e _ m o d u l e }
C O M M E N T " B u i l d i n g O p e n C V A n d r o i d l i b r a r y p r o j e c t "
)
file ( REMOVE "${OPENCV_DEPHELPER}/${the_module}_android" ) # force rebuild after CMake run
add_custom_target ( ${ the_module } _android ALL DEPENDS "${OPENCV_DEPHELPER}/${the_module}_android" SOURCES "${__base_dir}/${ANDROID_MANIFEST_FILE}" )
configure_file ( "${CMAKE_CURRENT_SOURCE_DIR}/build.gradle.in" "${ANDROID_TMP_INSTALL_BASE_DIR}/opencv/build.gradle" @ONLY )
install ( FILES "${ANDROID_TMP_INSTALL_BASE_DIR}/opencv/build.gradle" DESTINATION ${ JAVA_INSTALL_ROOT } /.. COMPONENT java )
install ( DIRECTORY "${java_src_dir}" DESTINATION "${JAVA_INSTALL_ROOT}" COMPONENT java )
endif ( ) # gradle build