Merge pull request #26057 from asmorkalov:as/android_16k_pages

Android builds update #26057

Fixes https://github.com/opencv/opencv/issues/26027
Should also address https://github.com/opencv/opencv/issues/26542
 
Changes:
- Switched to Android build tools 34, NDK 26d, target API level 34 (required by Google Play).
- Use flexible page size on Android by default to support Android 15+.
- Dummy stub for R and BuildConfig classes for javadoc.
- Java 17 everywhere.
- Strict ndkVersion and ABI list in release package.

Related:
- Docker: https://github.com/opencv-infrastructure/opencv-gha-dockerfile/pull/41
- Pipeline: https://github.com/opencv/ci-gha-workflow/pull/183

Related IPP issue with NDK 27+: https://github.com/opencv/opencv/issues/26072

Google documentation for 16kb pages support : https://developer.android.com/guide/practices/page-sizes?hl=en

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Alexander Smorkalov 2024-12-31 11:53:04 +03:00 committed by GitHub
parent 269ff8cd83
commit c803aa2ddd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 91 additions and 23 deletions

View File

@ -404,6 +404,14 @@ if(NOT OPENCV_SKIP_LINK_NO_UNDEFINED)
endif()
endif()
# For 16k pages support with NDK prior 27
# Details: https://developer.android.com/guide/practices/page-sizes?hl=en
if(ANDROID AND ANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES AND (ANDROID_ABI STREQUAL arm64-v8a OR ANDROID_ABI STREQUAL x86_64))
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-z,max-page-size=16384")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,max-page-size=16384")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-z,max-page-size=16384")
endif()
# combine all "extra" options
if(NOT OPENCV_SKIP_EXTRA_COMPILER_FLAGS)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_C_FLAGS}")

View File

@ -1,5 +1,5 @@
# https://developer.android.com/studio/releases/gradle-plugin
set(ANDROID_GRADLE_PLUGIN_VERSION "7.3.1" CACHE STRING "Android Gradle Plugin version")
set(ANDROID_GRADLE_PLUGIN_VERSION "8.6.0" CACHE STRING "Android Gradle Plugin version")
message(STATUS "Android Gradle Plugin version: ${ANDROID_GRADLE_PLUGIN_VERSION}")
set(KOTLIN_PLUGIN_VERSION "1.8.20" CACHE STRING "Kotlin Plugin version")
@ -13,16 +13,16 @@ else()
set(KOTLIN_STD_LIB "" CACHE STRING "Kotlin Standard Library dependency")
endif()
set(GRADLE_VERSION "7.6.3" CACHE STRING "Gradle version")
set(GRADLE_VERSION "8.11.1" CACHE STRING "Gradle version")
message(STATUS "Gradle version: ${GRADLE_VERSION}")
set(ANDROID_COMPILE_SDK_VERSION "31" CACHE STRING "Android compileSdkVersion")
set(ANDROID_COMPILE_SDK_VERSION "34" CACHE STRING "Android compileSdkVersion")
if(ANDROID_NATIVE_API_LEVEL GREATER 21)
set(ANDROID_MIN_SDK_VERSION "${ANDROID_NATIVE_API_LEVEL}" CACHE STRING "Android minSdkVersion")
else()
set(ANDROID_MIN_SDK_VERSION "21" CACHE STRING "Android minSdkVersion")
endif()
set(ANDROID_TARGET_SDK_VERSION "31" CACHE STRING "Android minSdkVersion")
set(ANDROID_TARGET_SDK_VERSION "34" CACHE STRING "Android targetSdkVersion")
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")
@ -35,11 +35,11 @@ include '${ANDROID_ABI}'
")
set(ANDROID_INSTALL_ABI_FILTER "
//reset()
//include 'armeabi-v7a'
//include 'arm64-v8a'
//include 'x86'
//include 'x86_64'
reset()
include 'armeabi-v7a'
include 'arm64-v8a'
include 'x86'
include 'x86_64'
")
if(NOT INSTALL_CREATE_DISTRIB)
set(ANDROID_INSTALL_ABI_FILTER "${ANDROID_BUILD_ABI_FILTER}")
@ -54,7 +54,9 @@ set(ANDROID_STRICT_BUILD_CONFIGURATION "true")
configure_file("${OpenCV_SOURCE_DIR}/samples/android/build.gradle.in" "${ANDROID_BUILD_BASE_DIR}/build.gradle" @ONLY)
set(ANDROID_ABI_FILTER "${ANDROID_INSTALL_ABI_FILTER}")
set(ANDROID_STRICT_BUILD_CONFIGURATION "false")
# CI uses NDK 26d to overcome https://github.com/opencv/opencv/issues/26072
# It's ahead of default configuration and we have to force version to get non-controversial parts of the package
#set(ANDROID_STRICT_BUILD_CONFIGURATION "false")
configure_file("${OpenCV_SOURCE_DIR}/samples/android/build.gradle.in" "${ANDROID_TMP_INSTALL_BASE_DIR}/${ANDROID_INSTALL_SAMPLES_DIR}/build.gradle" @ONLY)
install(FILES "${ANDROID_TMP_INSTALL_BASE_DIR}/${ANDROID_INSTALL_SAMPLES_DIR}/build.gradle" DESTINATION "${ANDROID_INSTALL_SAMPLES_DIR}" COMPONENT samples)

View File

@ -20,6 +20,11 @@ set(SOURSE_SETS_JAVA_SRC_DIRS "'java/src'")
set(SOURSE_SETS_RES_SRC_DIRS "'java/res'")
set(SOURSE_SETS_MANIFEST_SRC_FILE "'java/AndroidManifest.xml'")
set(BUILD_GRADLE_COMPILE_OPTIONS "
android {
buildFeatures {
buildConfig true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_${ANDROID_GRADLE_JAVA_VERSION_INIT}
targetCompatibility JavaVersion.VERSION_${ANDROID_GRADLE_JAVA_VERSION_INIT}
@ -183,7 +188,6 @@ set(SOURSE_SETS_JNI_LIBS_SRC_DIRS "'../../jni'")
set(SOURSE_SETS_JAVA_SRC_DIRS "'src'")
set(SOURSE_SETS_RES_SRC_DIRS "'${OpenCV_SOURCE_DIR}/modules/java/android_sdk/android_gradle_lib/res'")
set(SOURSE_SETS_MANIFEST_SRC_FILE "'AndroidManifest.xml'")
set(BUILD_GRADLE_COMPILE_OPTIONS "")
set(MAVEN_PUBLISH_PLUGIN_DECLARATION "")
set(BUILD_GRADLE_ANDROID_PUBLISHING_CONFIG "")
set(BUILD_GRADLE_PUBLISHING_CONFIG "")

View File

@ -7,6 +7,7 @@ plugins {
android {
namespace 'org.opencv'
compileSdk ${COMPILE_SDK}
ndkVersion "${NDK_VERSION}"
defaultConfig {
minSdk ${MIN_SDK}

View File

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

View File

@ -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-7.6.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@ -47,6 +47,14 @@ def get_opencv_version(opencv_sdk_path):
revision = re.search(r'^#define\W+CV_VERSION_REVISION\W+(\d+)$', data, re.MULTILINE).group(1)
return "%(major)s.%(minor)s.%(revision)s" % locals()
def get_ndk_version(ndk_path):
props_path = path.join(ndk_path, "source.properties")
with open(props_path, "rt") as f:
data = f.read()
version = re.search(r'Pkg\.Revision\W+=\W+(\d+\.\d+\.\d+)', data).group(1)
return version.strip()
def get_compiled_aar_path(path1, path2):
if path.exists(path1):
return path1
@ -70,6 +78,8 @@ def cleanup(paths_to_remove):
def main(args):
opencv_version = get_opencv_version(args.opencv_sdk_path)
ndk_version = get_ndk_version(args.ndk_location)
print("Detected ndk_version:", ndk_version)
abis = os.listdir(path.join(args.opencv_sdk_path, "sdk/native/libs"))
lib_name = "opencv_java" + opencv_version.split(".")[0]
final_aar_path = FINAL_AAR_PATH_TEMPLATE.replace("<OPENCV_VERSION>", opencv_version)
@ -90,6 +100,7 @@ def main(args):
"LIB_TYPE": "c++_shared",
"PACKAGE_NAME": MAVEN_PACKAGE_NAME,
"OPENCV_VERSION": opencv_version,
"NDK_VERSION": ndk_version,
"COMPILE_SDK": args.android_compile_sdk,
"MIN_SDK": args.android_min_sdk,
"TARGET_SDK": args.android_target_sdk,
@ -170,10 +181,10 @@ def main(args):
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Builds AAR with Java and shared C++ libs from OpenCV SDK")
parser.add_argument('opencv_sdk_path')
parser.add_argument('--android_compile_sdk', default="31")
parser.add_argument('--android_compile_sdk', default="34")
parser.add_argument('--android_min_sdk', default="21")
parser.add_argument('--android_target_sdk', default="31")
parser.add_argument('--java_version', default="1_8")
parser.add_argument('--android_target_sdk', default="34")
parser.add_argument('--java_version', default="17")
parser.add_argument('--ndk_location', default="")
parser.add_argument('--cmake_location', default="")
parser.add_argument('--offline', action="store_true", help="Force Gradle use offline mode")

View File

@ -285,7 +285,10 @@ class Builder:
cmd.append(self.opencvdir)
execute(cmd)
# full parallelism for C++ compilation tasks
execute([self.ninja_path, "opencv_modules"])
build_targets = ["opencv_modules"]
if do_install:
build_targets.append("opencv_tests")
execute([self.ninja_path, *build_targets])
# limit parallelism for building samples (avoid huge memory consumption)
if self.no_samples_build:
execute([self.ninja_path, "install" if (self.debug_info or self.debug) else "install/strip"])
@ -300,6 +303,14 @@ class Builder:
classpaths.append(os.path.join(dir, f))
srcdir = os.path.join(self.resultdest, 'sdk', 'java', 'src')
dstdir = self.docdest
# HACK: create stubs for auto-generated files to satisfy imports
with open(os.path.join(srcdir, 'org', 'opencv', 'BuildConfig.java'), 'wt') as fs:
fs.write("package org.opencv;\n public class BuildConfig {\n}")
fs.close()
with open(os.path.join(srcdir, 'org', 'opencv', 'R.java'), 'wt') as fs:
fs.write("package org.opencv;\n public class R {\n}")
fs.close()
# synchronize with modules/java/jar/build.xml.in
shutil.copy2(os.path.join(SCRIPT_DIR, '../../doc/mymath.js'), dstdir)
cmd = [
@ -327,9 +338,12 @@ class Builder:
'-bottom', 'Generated on %s / OpenCV %s' % (time.strftime("%Y-%m-%d %H:%M:%S"), self.opencv_version),
"-d", dstdir,
"-classpath", ":".join(classpaths),
'-subpackages', 'org.opencv',
'-subpackages', 'org.opencv'
]
execute(cmd)
# HACK: remove temporary files needed to satisfy javadoc imports
os.remove(os.path.join(srcdir, 'org', 'opencv', 'BuildConfig.java'))
os.remove(os.path.join(srcdir, 'org', 'opencv', 'R.java'))
def gather_results(self):
# Copy all files

View File

@ -7,7 +7,7 @@ import os
import shutil
import subprocess
from build_java_shared_aar import cleanup, fill_template, get_compiled_aar_path, get_opencv_version
from build_java_shared_aar import cleanup, fill_template, get_compiled_aar_path, get_opencv_version, get_ndk_version
ANDROID_PROJECT_TEMPLATE_DIR = path.join(path.dirname(__file__), "aar-template")
@ -103,6 +103,8 @@ def convert_deps_list_to_prefab(linked_libs, opencv_libs, external_libs):
def main(args):
opencv_version = get_opencv_version(args.opencv_sdk_path)
ndk_version = get_ndk_version(args.ndk_location)
print("Detected ndk_version:", ndk_version)
abis = os.listdir(path.join(args.opencv_sdk_path, "sdk/native/libs"))
final_aar_path = FINAL_AAR_PATH_TEMPLATE.replace("<OPENCV_VERSION>", opencv_version)
sdk_dir = args.opencv_sdk_path
@ -121,6 +123,7 @@ def main(args):
"LIB_TYPE": "c++_static",
"PACKAGE_NAME": MAVEN_PACKAGE_NAME,
"OPENCV_VERSION": opencv_version,
"NDK_VERSION": ndk_version,
"COMPILE_SDK": args.android_compile_sdk,
"MIN_SDK": args.android_min_sdk,
"TARGET_SDK": args.android_target_sdk,
@ -243,9 +246,9 @@ def main(args):
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Builds AAR with static C++ libs from OpenCV SDK")
parser.add_argument('opencv_sdk_path')
parser.add_argument('--android_compile_sdk', default="31")
parser.add_argument('--android_compile_sdk', default="34")
parser.add_argument('--android_min_sdk', default="21")
parser.add_argument('--android_target_sdk', default="31")
parser.add_argument('--android_target_sdk', default="34")
parser.add_argument('--java_version', default="1_8")
parser.add_argument('--ndk_location', default="")
parser.add_argument('--cmake_location', default="")

View File

@ -0,0 +1,6 @@
ABIs = [
ABI("2", "armeabi-v7a", None, 21, cmake_vars=dict(ANDROID_ABI='armeabi-v7a with NEON')),
ABI("3", "arm64-v8a", None, 21, cmake_vars=dict(ANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES='ON')),
ABI("5", "x86_64", None, 21, cmake_vars=dict(ANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES='ON')),
ABI("4", "x86", None, 21),
]

View File

@ -99,7 +99,7 @@ gradle.afterProject { project ->
// Android Gradle Plugin (AGP) 3.5+ is required
// https://github.com/android/ndk-samples/wiki/Configure-NDK-Path
def isNdkVersionSupported = project.android.metaClass.getProperties().find { it.name == 'ndkVersion' } != null
if ((false || opencv_strict_build_configuration) && isNdkVersionSupported) {
if (opencv_strict_build_configuration && isNdkVersionSupported) {
gradle.println("Override ndkVersion for the project ${project.name}")
project.android {
ndkVersion '@ANDROID_NDK_REVISION@'

View File

@ -14,10 +14,11 @@ android {
cmake {
if (gradle.opencv_source == "sdk_path") {
arguments "-DOpenCV_DIR=" + project(':opencv').projectDir + "/@ANDROID_PROJECT_JNI_PATH@",
"-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON",
"-DOPENCV_FROM_SDK=TRUE"@OPENCV_ANDROID_CMAKE_EXTRA_ARGS@
} else {
arguments "-DOPENCV_VERSION_MAJOR=@OPENCV_VERSION_MAJOR@",
"-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON",
"-DOPENCV_FROM_SDK=FALSE"@OPENCV_ANDROID_CMAKE_EXTRA_ARGS@
}
targets "mixed_sample"

View File

@ -14,6 +14,14 @@ endif()
message(STATUS "ANDROID_ABI=${ANDROID_ABI}")
find_package(OpenCV REQUIRED COMPONENTS ${ANDROID_OPENCV_COMPONENTS})
# For 16k pages support with NDK prior 27
# Details: https://developer.android.com/guide/practices/page-sizes?hl=en
if(ANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES)
if(ANDROID_ABI STREQUAL arm64-v8a OR ANDROID_ABI STREQUAL x86_64)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,max-page-size=16384")
endif()
endif()
file(GLOB srcs *.cpp *.c)
file(GLOB hdrs *.hpp *.h)

View File

@ -15,11 +15,13 @@ android {
if (gradle.opencv_source == "sdk_path") {
arguments "-DOpenCV_DIR=" + project(':opencv').projectDir + "/@ANDROID_PROJECT_JNI_PATH@",
"-DOPENCV_FROM_SDK=TRUE",
"-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON",
"-DANDROID_OPENCL_SDK=@ANDROID_OPENCL_SDK@" @OPENCV_ANDROID_CMAKE_EXTRA_ARGS@
} else {
arguments "-DOPENCV_VERSION_MAJOR=@OPENCV_VERSION_MAJOR@",
"-DOPENCV_FROM_SDK=FALSE",
"-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON",
"-DANDROID_OPENCL_SDK=@ANDROID_OPENCL_SDK@" @OPENCV_ANDROID_CMAKE_EXTRA_ARGS@
}
targets "JNIpart"

View File

@ -18,6 +18,14 @@ find_package(OpenCL QUIET)
file(GLOB srcs *.cpp *.c)
file(GLOB hdrs *.hpp *.h)
# For 16k pages support with NDK prior 27
# Details: https://developer.android.com/guide/practices/page-sizes?hl=en
if(ANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES)
if(ANDROID_ABI STREQUAL arm64-v8a OR ANDROID_ABI STREQUAL x86_64)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,max-page-size=16384")
endif()
endif()
include_directories("${CMAKE_CURRENT_LIST_DIR}")
add_library(${target} SHARED ${srcs} ${hdrs})