AAR package build and test with CI

- Use the same tools and plugins for SDK build and AAR build
- Added script to test Gradle-based samples against local maven repo
- Various local fixes and debug prints
pull/24554/head
Alexander Smorkalov 1 year ago
parent 2c1ec4245d
commit aa6b399b5a
  1. 10
      cmake/android/android_gradle_projects.cmake
  2. 4
      platforms/android/aar-template/build.gradle
  3. 2
      platforms/android/aar-template/gradle/wrapper/gradle-wrapper.properties
  4. 6
      platforms/android/build-tests/test_gradle.sh
  5. 47
      platforms/android/build-tests/test_gradle_aar.sh
  6. 14
      platforms/android/build_java_shared_aar.py
  7. 12
      platforms/android/build_static_aar.py
  8. 6
      samples/android/15-puzzle/build.gradle.in
  9. 2
      samples/android/camera-calibration/build.gradle.in
  10. 2
      samples/android/color-blob-detection/build.gradle.in
  11. 2
      samples/android/face-detection/build.gradle.in
  12. 2
      samples/android/face-detection/jni/CMakeLists.txt
  13. 2
      samples/android/image-manipulations/build.gradle.in
  14. 2
      samples/android/mobilenet-objdetect/build.gradle.in
  15. 2
      samples/android/tutorial-1-camerapreview/build.gradle.in
  16. 2
      samples/android/tutorial-2-mixedprocessing/build.gradle.in
  17. 2
      samples/android/tutorial-2-mixedprocessing/jni/CMakeLists.txt
  18. 2
      samples/android/tutorial-3-cameracontrol/build.gradle.in

@ -114,8 +114,7 @@ endif()
file(WRITE "${ANDROID_BUILD_BASE_DIR}/settings.gradle" "
gradle.ext {
//opencv_source = 'maven_central'
//opencv_source = 'maven_local'
// possible options: 'maven_central', 'maven_local', 'sdk_path'
opencv_source = 'sdk_path'
}
@ -126,19 +125,18 @@ file(WRITE "${ANDROID_TMP_INSTALL_BASE_DIR}/settings.gradle" "
rootProject.name = 'opencv_samples'
gradle.ext {
//opencv_source = 'maven_central'
//opencv_source = 'maven_local'
// possible options: 'maven_central', 'maven_local', 'sdk_path'
opencv_source = 'sdk_path'
}
if (gradle.opencv_source == 'maven_local') {
gradle.ext {
opencv_maven_path = '/<path_to_maven_repo>'
opencv_maven_path = '<path_to_maven_repo>'
}
}
if (gradle.opencv_source == 'sdk_path') {
def opencvsdk='../'
def opencvsdk = '../'
//def opencvsdk='/<path to OpenCV-android-sdk>'
//println opencvsdk
include ':opencv'

@ -1,4 +1,4 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.library' version '8.0.2' apply false
}
id 'com.android.library' version '7.3.0' apply false
}

@ -1,6 +1,6 @@
#Mon Jul 10 11:57:38 SGT 2023
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

@ -1,5 +1,6 @@
#!/bin/bash -e
SDK_DIR=$1
echo "OpenCV Android SDK path: ${SDK_DIR}"
ANDROID_HOME=${ANDROID_HOME:-${ANDROID_SDK_ROOT:-${ANDROID_SDK?Required ANDROID_HOME/ANDROID_SDK/ANDROID_SDK_ROOT}}}
@ -29,9 +30,10 @@ 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 "ndk.dir=${ANDROID_NDK}" > "test-gradle/samples/local.properties"
echo "cmake.dir=$(dirname $(dirname $(which cmake)))" >> "test-gradle/samples/local.properties"
echo "Run gradle ..."
(cd "test-gradle/samples"; ./gradlew -i assemble)

@ -0,0 +1,47 @@
#!/bin/bash -e
SDK_DIR=$1
LOCAL_MAVEN_REPO=$2
echo "OpenCV Android SDK path: ${SDK_DIR}"
echo "Use local maven repo from $LOCAL_MAVEN_REPO"
ANDROID_HOME=${ANDROID_HOME:-${ANDROID_SDK_ROOT:-${ANDROID_SDK?Required ANDROID_HOME/ANDROID_SDK/ANDROID_SDK_ROOT}}}
ANDROID_NDK=${ANDROID_NDK_HOME-${ANDROID_NDK:-${NDKROOT?Required ANDROID_NDK_HOME/ANDROID_NDK/NDKROOT}}}
echo "Android SDK: ${ANDROID_HOME}"
echo "Android NDK: ${ANDROID_NDK}"
if [ ! -d "${ANDROID_HOME}" ]; then
echo "FATAL: Missing Android SDK directory"
exit 1
fi
if [ ! -d "${ANDROID_NDK}" ]; then
echo "FATAL: Missing Android NDK directory"
exit 1
fi
export ANDROID_HOME=${ANDROID_HOME}
export ANDROID_SDK=${ANDROID_HOME}
export ANDROID_SDK_ROOT=${ANDROID_HOME}
export ANDROID_NDK=${ANDROID_NDK}
export ANDROID_NDK_HOME=${ANDROID_NDK}
echo "Cloning OpenCV Android SDK ..."
rm -rf "test-gradle-aar"
mkdir test-gradle-aar
cp -rp ${SDK_DIR}/samples/* test-gradle-aar/
echo "Cloning OpenCV Android SDK ... Done!"
# drop cmake bin name and "bin" folder from path
echo "ndk.dir=${ANDROID_NDK}" > "test-gradle-aar/local.properties"
echo "cmake.dir=$(dirname $(dirname $(which cmake)))" >> "test-gradle-aar/local.properties"
sed -i "s/opencv_source = 'sdk_path'/opencv_source = 'maven_local'/g" test-gradle-aar/settings.gradle
sed -i "s+opencv_maven_path = '<path_to_maven_repo>'+opencv_maven_path = 'file\\://$LOCAL_MAVEN_REPO'+g" test-gradle-aar/settings.gradle
echo "Run gradle ..."
(cd "test-gradle-aar"; ./gradlew -i assemble)
echo "#"
echo "# Done!"
echo "#"

@ -100,6 +100,16 @@ def main(args):
path.join(ANDROID_PROJECT_DIR, "OpenCV/src/main/cpp/CMakeLists.txt"),
{"LIB_NAME": lib_name, "LIB_TYPE": "SHARED"})
local_props = ""
if args.ndk_location:
local_props += "ndk.dir=" + args.ndk_location + "\n"
if args.cmake_location:
local_props += "cmake.dir=" + args.cmake_location + "\n"
if local_props:
with open(path.join(ANDROID_PROJECT_DIR, "local.properties"), "wt") as f:
f.write(local_props)
# Copying Java code and C++ public headers from SDK to the Android project
for src, dst in COPY_FROM_SDK_TO_ANDROID_PROJECT:
shutil.copytree(path.join(args.opencv_sdk_path, src),
@ -149,8 +159,10 @@ if __name__ == "__main__":
parser.add_argument('opencv_sdk_path')
parser.add_argument('--android_compile_sdk', default="26")
parser.add_argument('--android_min_sdk', default="21")
parser.add_argument('--android_target_sdk', default="26")
parser.add_argument('--android_target_sdk', default="31")
parser.add_argument('--java_version', default="1_8")
parser.add_argument('--ndk_location', default="")
parser.add_argument('--cmake_location', default="")
args = parser.parse_args()
main(args)

@ -131,6 +131,16 @@ def main(args):
path.join(ANDROID_PROJECT_DIR, "OpenCV/src/main/cpp/CMakeLists.txt"),
{"LIB_NAME": "templib", "LIB_TYPE": "STATIC"})
local_props = ""
if args.ndk_location:
local_props += "ndk.dir=" + args.ndk_location + "\n"
if args.cmake_location:
local_props += "cmake.dir=" + args.cmake_location + "\n"
if local_props:
with open(path.join(ANDROID_PROJECT_DIR, "local.properties"), "wt") as f:
f.write(local_props)
opencv_libs = get_list_of_opencv_libs(sdk_dir)
external_libs = get_list_of_3rdparty_libs(sdk_dir, abis)
@ -224,6 +234,8 @@ if __name__ == "__main__":
parser.add_argument('--android_min_sdk', default="21")
parser.add_argument('--android_target_sdk', default="26")
parser.add_argument('--java_version', default="1_8")
parser.add_argument('--ndk_location', default="")
parser.add_argument('--cmake_location', default="")
args = parser.parse_args()
main(args)

@ -28,9 +28,11 @@ android {
dependencies {
//implementation fileTree(dir: 'libs', include: ['*.jar'])
if (gradle.opencv_source == "sdk_path") {
if (gradle.opencv_source == 'sdk_path') {
println 'Using OpenCV from SDK'
implementation project(':opencv')
} else if (gradle.opencv_source == "maven_local" || gradle.opencv_source == "maven_cenral") {
} else if (gradle.opencv_source == 'maven_local' || gradle.opencv_source == 'maven_cenral') {
println 'Using OpenCV from Maven repo'
implementation 'org.opencv:opencv:@OPENCV_VERSION_PLAIN@'
}
}

@ -29,8 +29,10 @@ android {
dependencies {
//implementation fileTree(dir: 'libs', include: ['*.jar'])
if (gradle.opencv_source == "sdk_path") {
println 'Using OpenCV from SDK'
implementation project(':opencv')
} else if (gradle.opencv_source == "maven_local" || gradle.opencv_source == "maven_cenral") {
println 'Using OpenCV from Maven repo'
implementation 'org.opencv:opencv:@OPENCV_VERSION_PLAIN@'
}
}

@ -29,8 +29,10 @@ android {
dependencies {
//implementation fileTree(dir: 'libs', include: ['*.jar'])
if (gradle.opencv_source == "sdk_path") {
println 'Using OpenCV from from SDK'
implementation project(':opencv')
} else if (gradle.opencv_source == "maven_local" || gradle.opencv_source == "maven_cenral") {
println 'Using OpenCV from Maven repo'
implementation 'org.opencv:opencv:@OPENCV_VERSION_PLAIN@'
}
}

@ -53,8 +53,10 @@ android {
dependencies {
//implementation fileTree(dir: 'libs', include: ['*.jar'])
if (gradle.opencv_source == "sdk_path") {
println 'Using OpenCV from from SDK'
implementation project(':opencv')
} else if (gradle.opencv_source == "maven_local" || gradle.opencv_source == "maven_cenral") {
println 'Using OpenCV from Maven repo'
implementation 'org.opencv:opencv:@OPENCV_VERSION_PLAIN@'
}
}

@ -4,8 +4,10 @@ set(target detection_based_tracker)
project(${target} CXX)
if (OPENCV_FROM_SDK)
message(STATUS "Using OpenCV from local SDK")
set(ANDROID_OPENCV_COMPONENTS "opencv_java" CACHE STRING "")
else()
message(STATUS "Using OpenCV from AAR (Maven repo)")
set(ANDROID_OPENCV_COMPONENTS "OpenCV::opencv_java${OPENCV_VERSION_MAJOR}" CACHE STRING "")
endif()

@ -29,8 +29,10 @@ android {
dependencies {
//implementation fileTree(dir: 'libs', include: ['*.jar'])
if (gradle.opencv_source == "sdk_path") {
println 'Using OpenCV from from SDK'
implementation project(':opencv')
} else if (gradle.opencv_source == "maven_local" || gradle.opencv_source == "maven_cenral") {
println 'Using OpenCV from Maven repo'
implementation 'org.opencv:opencv:@OPENCV_VERSION_PLAIN@'
}
}

@ -29,8 +29,10 @@ android {
dependencies {
//implementation fileTree(dir: 'libs', include: ['*.jar'])
if (gradle.opencv_source == "sdk_path") {
println 'Using OpenCV from SDK'
implementation project(':opencv')
} else if (gradle.opencv_source == "maven_local" || gradle.opencv_source == "maven_cenral") {
println 'Using OpenCV from Maven repo'
implementation 'org.opencv:opencv:@OPENCV_VERSION_PLAIN@'
}
}

@ -29,8 +29,10 @@ android {
dependencies {
//implementation fileTree(dir: 'libs', include: ['*.jar'])
if (gradle.opencv_source == "sdk_path") {
println 'Using OpenCV from SDK'
implementation project(':opencv')
} else if (gradle.opencv_source == "maven_local" || gradle.opencv_source == "maven_cenral") {
println 'Using OpenCV from Maven repo'
implementation 'org.opencv:opencv:@OPENCV_VERSION_PLAIN@'
}
}

@ -53,8 +53,10 @@ android {
dependencies {
//implementation fileTree(dir: 'libs', include: ['*.jar'])
if (gradle.opencv_source == "sdk_path") {
println 'Using OpenCV from SDK'
implementation project(':opencv')
} else if (gradle.opencv_source == "maven_local" || gradle.opencv_source == "maven_cenral") {
println 'Using OpenCV from Maven repo'
implementation 'org.opencv:opencv:@OPENCV_VERSION_PLAIN@'
}
}

@ -4,8 +4,10 @@ set(target mixed_sample)
project(${target} CXX)
if (OPENCV_FROM_SDK)
message(STATUS "Using OpenCV from local SDK")
set(ANDROID_OPENCV_COMPONENTS "opencv_java" CACHE STRING "")
else()
message(STATUS "Using OpenCV from AAR (Maven repo)")
set(ANDROID_OPENCV_COMPONENTS "OpenCV::opencv_java${OPENCV_VERSION_MAJOR}" CACHE STRING "")
endif()

@ -29,8 +29,10 @@ android {
dependencies {
//implementation fileTree(dir: 'libs', include: ['*.jar'])
if (gradle.opencv_source == "sdk_path") {
println 'Using OpenCV from SDK'
implementation project(':opencv')
} else if (gradle.opencv_source == "maven_local" || gradle.opencv_source == "maven_cenral") {
println 'Using OpenCV from Maven repo'
implementation 'org.opencv:opencv:@OPENCV_VERSION_PLAIN@'
}
}

Loading…
Cancel
Save