From b59eca22ae7f581085a8009c2e4bcb96351bfc4e Mon Sep 17 00:00:00 2001 From: Alexander Smorkalov Date: Thu, 14 Dec 2023 18:38:35 +0300 Subject: [PATCH] Enable Android AAR package build with SDK project itself without Python - Added JavaDoc package build and publishing - Added Source package build and publishing - More metadata for publishing - Disable native samples build with aar, because prefab is not complete yet --- cmake/android/android_gradle_projects.cmake | 13 +++++- modules/java/android_sdk/build.gradle.in | 39 ++++++++++++++++ .../android_sdk/libcxx_helper/CMakeLists.txt | 2 + platforms/android/build_aar.sh | 45 +++++++++++++++++++ 4 files changed, 98 insertions(+), 1 deletion(-) create mode 100755 platforms/android/build_aar.sh diff --git a/cmake/android/android_gradle_projects.cmake b/cmake/android/android_gradle_projects.cmake index 782a46e012..9b3e7fbc9f 100644 --- a/cmake/android/android_gradle_projects.cmake +++ b/cmake/android/android_gradle_projects.cmake @@ -225,9 +225,20 @@ include ':${__dir}' configure_file("${path}/build.gradle.in" "${ANDROID_TMP_INSTALL_BASE_DIR}/${__dir}/build.gradle" @ONLY) install(FILES "${ANDROID_TMP_INSTALL_BASE_DIR}/${__dir}/build.gradle" DESTINATION "${ANDROID_INSTALL_SAMPLES_DIR}/${__dir}" COMPONENT samples) - file(APPEND "${ANDROID_TMP_INSTALL_BASE_DIR}/settings.gradle" " + # HACK: AAR packages generated from current OpenCV project has incomple prefab part + # and cannot be used for native linkage against OpenCV. + # Alternative way to build AAR: https://github.com/opencv/opencv/blob/4.x/platforms/android/build_java_shared_aar.py + if("${__dir}" STREQUAL "tutorial-2-mixedprocessing" OR "${__dir}" STREQUAL "tutorial-4-opencl") + file(APPEND "${ANDROID_TMP_INSTALL_BASE_DIR}/settings.gradle" " +if (gradle.opencv_source == 'sdk_path') { + include ':${__dir}' +} +") + else() + file(APPEND "${ANDROID_TMP_INSTALL_BASE_DIR}/settings.gradle" " include ':${__dir}' ") + endif() endmacro() diff --git a/modules/java/android_sdk/build.gradle.in b/modules/java/android_sdk/build.gradle.in index 6b71d1fe11..15bfdbefc1 100644 --- a/modules/java/android_sdk/build.gradle.in +++ b/modules/java/android_sdk/build.gradle.in @@ -89,6 +89,7 @@ // apply plugin: 'com.android.library' +apply plugin: 'maven-publish' @KOTLIN_PLUGIN_DECLARATION@ def openCVersionName = "@OPENCV_VERSION@" @@ -137,6 +138,17 @@ android { } } + buildFeatures { + aidl true + prefabPublishing true + buildConfig true + } + prefab { + opencv_jni_shared { + headers "native/jni/include" + } + } + sourceSets { main { jniLibs.srcDirs = ['native/libs'] @@ -147,6 +159,13 @@ android { } } + publishing { + singleVariant('release') { + withSourcesJar() + withJavadocJar() + } + } + externalNativeBuild { cmake { path (project.projectDir.toString() + '/libcxx_helper/CMakeLists.txt') @@ -154,5 +173,25 @@ android { } } +publishing { + publications { + release(MavenPublication) { + groupId = 'org.opencv' + artifactId = 'opencv' + version = '@OPENCV_VERSION_PLAIN@' + + afterEvaluate { + from components.release + } + } + } + repositories { + maven { + name = 'myrepo' + url = "${project.buildDir}/repo" + } + } +} + dependencies { } diff --git a/modules/java/android_sdk/libcxx_helper/CMakeLists.txt b/modules/java/android_sdk/libcxx_helper/CMakeLists.txt index bc9146f457..c2dcd6d4d8 100644 --- a/modules/java/android_sdk/libcxx_helper/CMakeLists.txt +++ b/modules/java/android_sdk/libcxx_helper/CMakeLists.txt @@ -1,4 +1,6 @@ cmake_minimum_required(VERSION 3.6) +project(opencv_jni_shared) + # dummy target to bring libc++_shared.so into packages add_library(opencv_jni_shared STATIC dummy.cpp) diff --git a/platforms/android/build_aar.sh b/platforms/android/build_aar.sh new file mode 100755 index 0000000000..9af3cd0941 --- /dev/null +++ b/platforms/android/build_aar.sh @@ -0,0 +1,45 @@ +#!/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}}} +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 "aar-build" +cp -rp "${SDK_DIR}" "aar-build" +echo "Cloning OpenCV Android SDK ... Done!" + +# drop cmake bin name and "bin" folder from path +echo "ndk.dir=${ANDROID_NDK}" > "aar-build/samples/local.properties" +echo "cmake.dir=$(dirname $(dirname $(which cmake)))" >> "aar-build/samples/local.properties" + +echo "Run gradle ..." +(cd "aar-build/samples"; ./gradlew ${OPENCV_GRADLE_VERBOSE_OPTIONS:--i} opencv:publishReleasePublicationToMyrepoRepository) + +mkdir "maven_repo" +cp -r aar-build/sdk/build/repo/* ./maven_repo/ + +echo "#" +echo "# Done!" +echo "#"