Merge pull request #24473 from alexlyulkov:al/samples_with_maven

Updated Android samples for modern Android studio. Added OpenCV from Maven support. #24473

Updated samples for recent Android studio:

- added namespace field that is required in build.gradle files
- replaced _switch_ by _if-else_ because it doesn't work with constants from resources
- added missed log library dependency in face-detection/jni/CMakeLists.txt
- use local.properties to define NDK location

Added support for OpenCV from Maven. Now you can choose 3 possible sources of OpenCV lib in settings.gradle: SDK path, local Maven repository, public Maven repository. (Creating Maven repository from SDK is added here #24456 )

There are differences in project configs for SDK and Maven versions:

- different dependencies in build.gradle
- different OpenCV library names in CMakeLists.txt
- SDK version requires OpenCV_DIR definition

Requires:
- https://github.com/opencv/ci-gha-workflow/pull/124
- https://github.com/opencv-infrastructure/opencv-gha-dockerfile/pull/26
pull/24562/head
alexlyulkov 1 year ago committed by GitHub
parent fad0dbb9ac
commit 6c57ce9e09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 40
      cmake/android/android_gradle_projects.cmake
  2. 3
      platforms/android/build-tests/test_gradle.sh
  3. 7
      samples/android/15-puzzle/build.gradle.in
  4. 5
      samples/android/build.gradle.in
  5. 7
      samples/android/camera-calibration/build.gradle.in
  6. 11
      samples/android/camera-calibration/src/org/opencv/samples/cameracalibration/CameraCalibrationActivity.java
  7. 7
      samples/android/color-blob-detection/build.gradle.in
  8. 21
      samples/android/face-detection/build.gradle.in
  9. 10
      samples/android/face-detection/jni/CMakeLists.txt
  10. 7
      samples/android/image-manipulations/build.gradle.in
  11. 7
      samples/android/mobilenet-objdetect/build.gradle.in
  12. 7
      samples/android/tutorial-1-camerapreview/build.gradle.in
  13. 21
      samples/android/tutorial-2-mixedprocessing/build.gradle.in
  14. 7
      samples/android/tutorial-2-mixedprocessing/jni/CMakeLists.txt
  15. 7
      samples/android/tutorial-3-cameracontrol/build.gradle.in
  16. 21
      samples/android/tutorial-4-opencl/build.gradle.in
  17. 9
      samples/android/tutorial-4-opencl/jni/CMakeLists.txt

@ -1,8 +1,8 @@
# https://developer.android.com/studio/releases/gradle-plugin
set(ANDROID_GRADLE_PLUGIN_VERSION "3.2.1" CACHE STRING "Android Gradle Plugin version")
set(ANDROID_GRADLE_PLUGIN_VERSION "7.3.1" CACHE STRING "Android Gradle Plugin version")
message(STATUS "Android Gradle Plugin version: ${ANDROID_GRADLE_PLUGIN_VERSION}")
set(KOTLIN_PLUGIN_VERSION "1.4.10" CACHE STRING "Kotlin Plugin version")
set(KOTLIN_PLUGIN_VERSION "1.5.20" CACHE STRING "Kotlin Plugin version")
message(STATUS "Kotlin Plugin version: ${KOTLIN_PLUGIN_VERSION}")
if(BUILD_KOTLIN_EXTENSIONS)
@ -13,7 +13,7 @@ else()
set(KOTLIN_STD_LIB "" CACHE STRING "Kotlin Standard Library dependency")
endif()
set(GRADLE_VERSION "5.6.4" CACHE STRING "Gradle version")
set(GRADLE_VERSION "7.6.3" CACHE STRING "Gradle version")
message(STATUS "Gradle version: ${GRADLE_VERSION}")
set(ANDROID_COMPILE_SDK_VERSION "26" CACHE STRING "Android compileSdkVersion")
@ -22,7 +22,7 @@ if(ANDROID_NATIVE_API_LEVEL GREATER 21)
else()
set(ANDROID_MIN_SDK_VERSION "21" CACHE STRING "Android minSdkVersion")
endif()
set(ANDROID_TARGET_SDK_VERSION "26" CACHE STRING "Android minSdkVersion")
set(ANDROID_TARGET_SDK_VERSION "31" CACHE STRING "Android minSdkVersion")
set(ANDROID_BUILD_BASE_DIR "${OpenCV_BINARY_DIR}/opencv_android" CACHE INTERNAL "")
set(ANDROID_TMP_INSTALL_BASE_DIR "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/install/opencv_android")
@ -109,21 +109,41 @@ if(NOT OPENCV_SKIP_ANDROID_FORCE_CMAKE)
get_filename_component(_CMAKE_INSTALL_DIR "${CMAKE_ROOT}" PATH)
get_filename_component(_CMAKE_INSTALL_DIR "${_CMAKE_INSTALL_DIR}" PATH)
endif()
ocv_update_file("${ANDROID_BUILD_BASE_DIR}/local.properties" "cmake.dir=${_CMAKE_INSTALL_DIR}")
ocv_update_file("${ANDROID_BUILD_BASE_DIR}/local.properties" "cmake.dir=${_CMAKE_INSTALL_DIR}\nndk.dir=${ANDROID_NDK}")
endif()
file(WRITE "${ANDROID_BUILD_BASE_DIR}/settings.gradle" "
gradle.ext {
//opencv_source = 'maven_central'
//opencv_source = 'maven_local'
opencv_source = 'sdk_path'
}
include ':opencv'
")
file(WRITE "${ANDROID_TMP_INSTALL_BASE_DIR}/settings.gradle" "
rootProject.name = 'opencv_samples'
def opencvsdk='../'
//def opencvsdk='/<path to OpenCV-android-sdk>'
//println opencvsdk
include ':opencv'
project(':opencv').projectDir = new File(opencvsdk + '/sdk')
gradle.ext {
//opencv_source = 'maven_central'
//opencv_source = 'maven_local'
opencv_source = 'sdk_path'
}
if (gradle.opencv_source == 'maven_local') {
gradle.ext {
opencv_maven_path = '/<path_to_maven_repo>'
}
}
if (gradle.opencv_source == 'sdk_path') {
def opencvsdk='../'
//def opencvsdk='/<path to OpenCV-android-sdk>'
//println opencvsdk
include ':opencv'
project(':opencv').projectDir = new File(opencvsdk + '/sdk')
}
")
ocv_check_environment_variables(OPENCV_GRADLE_VERBOSE_OPTIONS)

@ -29,6 +29,9 @@ rm -rf "test-gradle"
cp -rp "${SDK_DIR}" "test-gradle"
echo "Cloning OpenCV Android SDK ... Done!"
echo "Force Current CMake for Gradle project"
# drop cmake bin name and "bin" folder from path
echo "cmake.dir=$(dirname $(dirname $(which cmake)))" > "test-gradle/samples/local.properties"
echo "Run gradle ..."
(cd "test-gradle/samples"; ./gradlew -i assemble)

@ -1,6 +1,7 @@
apply plugin: 'com.android.application'
android {
namespace 'org.opencv.samples.puzzle15'
compileSdkVersion @ANDROID_COMPILE_SDK_VERSION@
defaultConfig {
applicationId "org.opencv.samples.puzzle15"
@ -27,5 +28,9 @@ android {
dependencies {
//implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':opencv')
if (gradle.opencv_source == "sdk_path") {
implementation project(':opencv')
} else if (gradle.opencv_source == "maven_local" || gradle.opencv_source == "maven_cenral") {
implementation 'org.opencv:opencv:@OPENCV_VERSION_PLAIN@'
}
}

@ -19,6 +19,11 @@ allprojects {
repositories {
google()
jcenter()
if (gradle.opencv_source == "maven_local") {
maven {
url gradle.opencv_maven_path
}
}
}
}

@ -1,6 +1,7 @@
apply plugin: 'com.android.application'
android {
namespace 'org.opencv.samples.cameracalibration'
compileSdkVersion @ANDROID_COMPILE_SDK_VERSION@
defaultConfig {
applicationId "org.opencv.samples.cameracalibration"
@ -27,5 +28,9 @@ android {
dependencies {
//implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':opencv')
if (gradle.opencv_source == "sdk_path") {
implementation project(':opencv')
} else if (gradle.opencv_source == "maven_local" || gradle.opencv_source == "maven_cenral") {
implementation 'org.opencv:opencv:@OPENCV_VERSION_PLAIN@'
}
}

@ -136,23 +136,22 @@ public class CameraCalibrationActivity extends CameraActivity implements CvCamer
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.calibration:
if (item.getItemId() == R.id.calibration) {
mOnCameraFrameRender =
new OnCameraFrameRender(new CalibrationFrameRender(mCalibrator));
item.setChecked(true);
return true;
case R.id.undistortion:
} else if (item.getItemId() == R.id.undistortion) {
mOnCameraFrameRender =
new OnCameraFrameRender(new UndistortionFrameRender(mCalibrator));
item.setChecked(true);
return true;
case R.id.comparison:
} else if (item.getItemId() == R.id.comparison) {
mOnCameraFrameRender =
new OnCameraFrameRender(new ComparisonFrameRender(mCalibrator, mWidth, mHeight, getResources()));
item.setChecked(true);
return true;
case R.id.calibrate:
} else if (item.getItemId() == R.id.calibrate) {
final Resources res = getResources();
if (mCalibrator.getCornersBufferSize() < 2) {
(Toast.makeText(this, res.getString(R.string.more_samples), Toast.LENGTH_SHORT)).show();
@ -196,7 +195,7 @@ public class CameraCalibrationActivity extends CameraActivity implements CvCamer
}
}.execute();
return true;
default:
} else {
return super.onOptionsItemSelected(item);
}
}

@ -1,6 +1,7 @@
apply plugin: 'com.android.application'
android {
namespace 'org.opencv.samples.colorblobdetect'
compileSdkVersion @ANDROID_COMPILE_SDK_VERSION@
defaultConfig {
applicationId "org.opencv.samples.colorblobdetect"
@ -27,5 +28,9 @@ android {
dependencies {
//implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':opencv')
if (gradle.opencv_source == "sdk_path") {
implementation project(':opencv')
} else if (gradle.opencv_source == "maven_local" || gradle.opencv_source == "maven_cenral") {
implementation 'org.opencv:opencv:@OPENCV_VERSION_PLAIN@'
}
}

@ -1,6 +1,7 @@
apply plugin: 'com.android.application'
android {
namespace 'org.opencv.samples.facedetect'
compileSdkVersion @ANDROID_COMPILE_SDK_VERSION@
defaultConfig {
applicationId "org.opencv.samples.facedetect"
@ -11,7 +12,14 @@ android {
externalNativeBuild {
cmake {
arguments "-DOpenCV_DIR=" + project(':opencv').projectDir + "/@ANDROID_PROJECT_JNI_PATH@"@OPENCV_ANDROID_CMAKE_EXTRA_ARGS@
if (gradle.opencv_source == "sdk_path") {
arguments "-DOpenCV_DIR=" + project(':opencv').projectDir + "/@ANDROID_PROJECT_JNI_PATH@",
"-DOPENCV_FROM_SDK=TRUE"@OPENCV_ANDROID_CMAKE_EXTRA_ARGS@
} else {
arguments "-DOPENCV_VERSION_MAJOR=@OPENCV_VERSION_MAJOR@",
"-DOPENCV_FROM_SDK=FALSE"@OPENCV_ANDROID_CMAKE_EXTRA_ARGS@
}
targets "detection_based_tracker"
}
}
@ -35,9 +43,18 @@ android {
path '@ANDROID_SAMPLE_JNI_PATH@/CMakeLists.txt'
}
}
buildFeatures {
if (gradle.opencv_source == "maven_local" || gradle.opencv_source == "maven_cenral") {
prefab true
}
}
}
dependencies {
//implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':opencv')
if (gradle.opencv_source == "sdk_path") {
implementation project(':opencv')
} else if (gradle.opencv_source == "maven_local" || gradle.opencv_source == "maven_cenral") {
implementation 'org.opencv:opencv:@OPENCV_VERSION_PLAIN@'
}
}

@ -3,7 +3,12 @@ cmake_minimum_required(VERSION 3.6)
set(target detection_based_tracker)
project(${target} CXX)
set(ANDROID_OPENCV_COMPONENTS "opencv_java" CACHE STRING "")
if (OPENCV_FROM_SDK)
set(ANDROID_OPENCV_COMPONENTS "opencv_java" CACHE STRING "")
else()
set(ANDROID_OPENCV_COMPONENTS "OpenCV::opencv_java${OPENCV_VERSION_MAJOR}" CACHE STRING "")
endif()
message(STATUS "ANDROID_ABI=${ANDROID_ABI}")
find_package(OpenCV REQUIRED COMPONENTS ${ANDROID_OPENCV_COMPONENTS})
@ -12,4 +17,5 @@ file(GLOB hdrs *.hpp *.h)
include_directories("${CMAKE_CURRENT_LIST_DIR}")
add_library(${target} SHARED ${srcs} ${hdrs})
target_link_libraries(${target} ${ANDROID_OPENCV_COMPONENTS})
find_library(log_lib log)
target_link_libraries(${target} ${ANDROID_OPENCV_COMPONENTS} ${log_lib})

@ -1,6 +1,7 @@
apply plugin: 'com.android.application'
android {
namespace 'org.opencv.samples.imagemanipulations'
compileSdkVersion @ANDROID_COMPILE_SDK_VERSION@
defaultConfig {
applicationId "org.opencv.samples.imagemanipulations"
@ -27,5 +28,9 @@ android {
dependencies {
//implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':opencv')
if (gradle.opencv_source == "sdk_path") {
implementation project(':opencv')
} else if (gradle.opencv_source == "maven_local" || gradle.opencv_source == "maven_cenral") {
implementation 'org.opencv:opencv:@OPENCV_VERSION_PLAIN@'
}
}

@ -1,6 +1,7 @@
apply plugin: 'com.android.application'
android {
namespace 'org.opencv.samples.opencv_mobilenet'
compileSdkVersion @ANDROID_COMPILE_SDK_VERSION@
defaultConfig {
applicationId "org.opencv.samples.opencv_mobilenet"
@ -27,5 +28,9 @@ android {
dependencies {
//implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':opencv')
if (gradle.opencv_source == "sdk_path") {
implementation project(':opencv')
} else if (gradle.opencv_source == "maven_local" || gradle.opencv_source == "maven_cenral") {
implementation 'org.opencv:opencv:@OPENCV_VERSION_PLAIN@'
}
}

@ -1,6 +1,7 @@
apply plugin: 'com.android.application'
android {
namespace 'org.opencv.samples.tutorial1'
compileSdkVersion @ANDROID_COMPILE_SDK_VERSION@
defaultConfig {
applicationId "org.opencv.samples.tutorial1"
@ -27,5 +28,9 @@ android {
dependencies {
//implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':opencv')
if (gradle.opencv_source == "sdk_path") {
implementation project(':opencv')
} else if (gradle.opencv_source == "maven_local" || gradle.opencv_source == "maven_cenral") {
implementation 'org.opencv:opencv:@OPENCV_VERSION_PLAIN@'
}
}

@ -1,6 +1,7 @@
apply plugin: 'com.android.application'
android {
namespace 'org.opencv.samples.tutorial2'
compileSdkVersion @ANDROID_COMPILE_SDK_VERSION@
defaultConfig {
applicationId "org.opencv.samples.tutorial2"
@ -11,7 +12,14 @@ android {
externalNativeBuild {
cmake {
arguments "-DOpenCV_DIR=" + project(':opencv').projectDir + "/@ANDROID_PROJECT_JNI_PATH@"@OPENCV_ANDROID_CMAKE_EXTRA_ARGS@
if (gradle.opencv_source == "sdk_path") {
arguments "-DOpenCV_DIR=" + project(':opencv').projectDir + "/@ANDROID_PROJECT_JNI_PATH@",
"-DOPENCV_FROM_SDK=TRUE"@OPENCV_ANDROID_CMAKE_EXTRA_ARGS@
} else {
arguments "-DOPENCV_VERSION_MAJOR=@OPENCV_VERSION_MAJOR@",
"-DOPENCV_FROM_SDK=FALSE"@OPENCV_ANDROID_CMAKE_EXTRA_ARGS@
}
targets "mixed_sample"
}
}
@ -35,9 +43,18 @@ android {
path '@ANDROID_SAMPLE_JNI_PATH@/CMakeLists.txt'
}
}
buildFeatures {
if (gradle.opencv_source == "maven_local" || gradle.opencv_source == "maven_cenral") {
prefab true
}
}
}
dependencies {
//implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':opencv')
if (gradle.opencv_source == "sdk_path") {
implementation project(':opencv')
} else if (gradle.opencv_source == "maven_local" || gradle.opencv_source == "maven_cenral") {
implementation 'org.opencv:opencv:@OPENCV_VERSION_PLAIN@'
}
}

@ -3,7 +3,12 @@ cmake_minimum_required(VERSION 3.6)
set(target mixed_sample)
project(${target} CXX)
set(ANDROID_OPENCV_COMPONENTS "opencv_java" CACHE STRING "")
if (OPENCV_FROM_SDK)
set(ANDROID_OPENCV_COMPONENTS "opencv_java" CACHE STRING "")
else()
set(ANDROID_OPENCV_COMPONENTS "OpenCV::opencv_java${OPENCV_VERSION_MAJOR}" CACHE STRING "")
endif()
message(STATUS "ANDROID_ABI=${ANDROID_ABI}")
find_package(OpenCV REQUIRED COMPONENTS ${ANDROID_OPENCV_COMPONENTS})

@ -1,6 +1,7 @@
apply plugin: 'com.android.application'
android {
namespace 'org.opencv.samples.tutorial3'
compileSdkVersion @ANDROID_COMPILE_SDK_VERSION@
defaultConfig {
applicationId "org.opencv.samples.tutorial3"
@ -27,5 +28,9 @@ android {
dependencies {
//implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':opencv')
if (gradle.opencv_source == "sdk_path") {
implementation project(':opencv')
} else if (gradle.opencv_source == "maven_local" || gradle.opencv_source == "maven_cenral") {
implementation 'org.opencv:opencv:@OPENCV_VERSION_PLAIN@'
}
}

@ -1,6 +1,7 @@
apply plugin: 'com.android.application'
android {
namespace 'org.opencv.samples.tutorial4'
compileSdkVersion @ANDROID_COMPILE_SDK_VERSION@
defaultConfig {
applicationId "org.opencv.samples.tutorial4"
@ -11,7 +12,14 @@ android {
externalNativeBuild {
cmake {
arguments "-DOpenCV_DIR=" + project(':opencv').projectDir + "/@ANDROID_PROJECT_JNI_PATH@"@OPENCV_ANDROID_CMAKE_EXTRA_ARGS@
if (gradle.opencv_source == "sdk_path") {
arguments "-DOpenCV_DIR=" + project(':opencv').projectDir + "/@ANDROID_PROJECT_JNI_PATH@",
"-DOPENCV_FROM_SDK=TRUE"@OPENCV_ANDROID_CMAKE_EXTRA_ARGS@
} else {
arguments "-DOPENCV_VERSION_MAJOR=@OPENCV_VERSION_MAJOR@",
"-DOPENCV_FROM_SDK=FALSE"@OPENCV_ANDROID_CMAKE_EXTRA_ARGS@
}
targets "JNIpart"
}
}
@ -35,9 +43,18 @@ android {
path '@ANDROID_SAMPLE_JNI_PATH@/CMakeLists.txt'
}
}
buildFeatures {
if (gradle.opencv_source == "maven_local" || gradle.opencv_source == "maven_cenral") {
prefab true
}
}
}
dependencies {
//implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':opencv')
if (gradle.opencv_source == "sdk_path") {
implementation project(':opencv')
} else if (gradle.opencv_source == "maven_local" || gradle.opencv_source == "maven_cenral") {
implementation 'org.opencv:opencv:@OPENCV_VERSION_PLAIN@'
}
}

@ -1,9 +1,14 @@
cmake_minimum_required(VERSION 3.6)
set(target mixed_sample)
set(target JNIpart)
project(${target} CXX)
set(ANDROID_OPENCV_COMPONENTS "opencv_java" CACHE STRING "")
if (OPENCV_FROM_SDK)
set(ANDROID_OPENCV_COMPONENTS "opencv_java" CACHE STRING "")
else()
set(ANDROID_OPENCV_COMPONENTS "OpenCV::opencv_java${OPENCV_VERSION_MAJOR}" CACHE STRING "")
endif()
message(STATUS "ANDROID_ABI=${ANDROID_ABI}")
find_package(OpenCV REQUIRED COMPONENTS ${ANDROID_OPENCV_COMPONENTS})

Loading…
Cancel
Save