Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 14 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions cmake/android/android_gradle_projects.cmake
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions platforms/android/build-tests/test_gradle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
7 changes: 6 additions & 1 deletion samples/android/15-puzzle/build.gradle.in
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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@'
}
}
5 changes: 5 additions & 0 deletions samples/android/build.gradle.in
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ allprojects {
repositories {
google()
jcenter()
if (gradle.opencv_source == "maven_local") {
maven {
url gradle.opencv_maven_path
}
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion samples/android/camera-calibration/build.gradle.in
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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@'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,23 +136,22 @@ public boolean onPrepareOptionsMenu (Menu menu) {

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();
Expand Down Expand Up @@ -196,7 +195,7 @@ protected void onPostExecute(Void result) {
}
}.execute();
return true;
default:
} else {
return super.onOptionsItemSelected(item);
}
}
Expand Down
7 changes: 6 additions & 1 deletion samples/android/color-blob-detection/build.gradle.in
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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@'
}
}
21 changes: 19 additions & 2 deletions samples/android/face-detection/build.gradle.in
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
}
}
Expand All @@ -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@'
}
}
10 changes: 8 additions & 2 deletions samples/android/face-detection/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand All @@ -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})
7 changes: 6 additions & 1 deletion samples/android/image-manipulations/build.gradle.in
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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@'
}
}
7 changes: 6 additions & 1 deletion samples/android/mobilenet-objdetect/build.gradle.in
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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@'
}
}
7 changes: 6 additions & 1 deletion samples/android/tutorial-1-camerapreview/build.gradle.in
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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@'
}
}
21 changes: 19 additions & 2 deletions samples/android/tutorial-2-mixedprocessing/build.gradle.in
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
}
}
Expand All @@ -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@'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down
7 changes: 6 additions & 1 deletion samples/android/tutorial-3-cameracontrol/build.gradle.in
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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@'
}
}
Loading
Loading