Skip to content

Added scripts for creating an AAR package and a local Maven repository with OpenCV library#24456

Merged
asmorkalov merged 13 commits intoopencv:4.xfrom
alexlyulkov:al/aar
Nov 7, 2023
Merged

Added scripts for creating an AAR package and a local Maven repository with OpenCV library#24456
asmorkalov merged 13 commits intoopencv:4.xfrom
alexlyulkov:al/aar

Conversation

@alexlyulkov
Copy link
Copy Markdown
Contributor

@alexlyulkov alexlyulkov commented Oct 26, 2023

Added scripts for creating an AAR package and a local Maven repository with OpenCV library.

The build_java_shared_aar.py script creates AAR with Java + C++ shared libraries.
The build_static_aar.py script creates AAR with static C++ libraries.

The scripts use an Android project template. The project is almost a default Android AAR library project with empty Java code and one empty C++ library. Only build.gradle.template and CMakeLists.txt.template files contain significant changes.

See README.md for more information.

@asmorkalov
Copy link
Copy Markdown
Contributor

 platforms/android/aar-creating/build_static_aar.py:119: trailing whitespace.
+        
platforms/android/aar-creating/build_static_aar.py:198: trailing whitespace.
+    
Error: Process completed with exit code 2.

@asmorkalov
Copy link
Copy Markdown
Contributor

I propose to rename aar-creating -> aar-template

["sdk/native/libs/<ABI>/libopencv_java4.so", "prefab/modules/opencv_java4/libs/android.<ABI>/libopencv_java4.so"],
]

ANDROID_PROJECT_TEMPLATE_DIR = "OpenCVAndroidProject"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The script might be executed outside of OpenCV. Please use full path.

Copy link
Copy Markdown
Contributor Author

@alexlyulkov alexlyulkov Oct 26, 2023

Choose a reason for hiding this comment

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

Maybe use __file__ constant?

Comment on lines +52 to +54
subprocess.run(["gradlew", "assembleRelease"],
shell=True,
cwd=ANDROID_PROJECT_DIR)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please check build status. I get error on the next steps, if something went wrong:

Adding libs to AAR...
Traceback (most recent call last):
  File "./build_java_shared_aar.py", line 92, in <module>
    main(args.opencv_sdk_path, args.opencv_version)
  File "./build_java_shared_aar.py", line 60, in main
    shutil.unpack_archive(COMPILED_AAR_PATH, AAR_UNZIPPED_DIR, "zip")
  File "/usr/lib/python3.6/shutil.py", line 974, in unpack_archive
    func(filename, extract_dir, **dict(format_info[2]))
  File "/usr/lib/python3.6/shutil.py", line 883, in _unpack_zipfile
    raise ReadError("%s is not a zip file" % filename)
shutil.ReadError: build_java_shared/AndroidProject/OpenCV/build/outputs/aar/opencv-release.aar is not a zip file

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I probably fixed the problem, please try again

@asmorkalov asmorkalov added this to the 4.9.0 milestone Oct 26, 2023
@asmorkalov
Copy link
Copy Markdown
Contributor

python3 /home/alexander/Projects/OpenCV/opencv-master/platforms/android/aar-template/build_java_shared_aar.py /home/alexander/Projects/OpenCV/OpenCV4Android-4.8.1/
Preparing Android project...
Running gradle assembleRelease...
Starting a Gradle Daemon, 1 incompatible Daemon could not be reused, use --status for details

> Configure project :OpenCV
Checking the license for package NDK (Side by side) 25.1.8937393 in /home/alexander/Android2/licenses
License for package NDK (Side by side) 25.1.8937393 accepted.
Preparing "Install NDK (Side by side) 25.1.8937393 (revision: 25.1.8937393)".
"Install NDK (Side by side) 25.1.8937393 (revision: 25.1.8937393)" ready.
Installing NDK (Side by side) 25.1.8937393 in /home/alexander/Android2/ndk/25.1.8937393
"Install NDK (Side by side) 25.1.8937393 (revision: 25.1.8937393)" complete.
"Install NDK (Side by side) 25.1.8937393 (revision: 25.1.8937393)" finished.
Checking the license for package Android SDK Build-Tools 30.0.3 in /home/alexander/Android2/licenses
License for package Android SDK Build-Tools 30.0.3 accepted.
Preparing "Install Android SDK Build-Tools 30.0.3 (revision: 30.0.3)".
"Install Android SDK Build-Tools 30.0.3 (revision: 30.0.3)" ready.
Installing Android SDK Build-Tools 30.0.3 in /home/alexander/Android2/build-tools/30.0.3
"Install Android SDK Build-Tools 30.0.3 (revision: 30.0.3)" complete.
"Install Android SDK Build-Tools 30.0.3 (revision: 30.0.3)" finished.
Checking the license for package Android SDK Platform 33 in /home/alexander/Android2/licenses
License for package Android SDK Platform 33 accepted.
Preparing "Install Android SDK Platform 33 (revision: 3)".
"Install Android SDK Platform 33 (revision: 3)" ready.
Installing Android SDK Platform 33 in /home/alexander/Android2/platforms/android-33
"Install Android SDK Platform 33 (revision: 3)" complete.
"Install Android SDK Platform 33 (revision: 3)" finished.

> Task :OpenCV:compileReleaseJavaWithJavac
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

BUILD SUCCESSFUL in 2m 45s
39 actionable tasks: 39 executed
Adding libs to AAR...
Creating local maven repo...
Traceback (most recent call last):
  File "/usr/lib/python3.6/shutil.py", line 550, in move
    os.rename(src, real_dst)
FileNotFoundError: [Errno 2] No such file or directory: 'build_java_shared/AndroidProject/OpenCV/build/repo/org/opencv/opencv' -> 'outputs/maven_repo/org/opencv/opencv'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/alexander/Projects/OpenCV/opencv-master/platforms/android/aar-template/build_java_shared_aar.py", line 120, in <module>
    main(args.opencv_sdk_path, get_opencv_version(args.opencv_sdk_path))
  File "/home/alexander/Projects/OpenCV/opencv-master/platforms/android/aar-template/build_java_shared_aar.py", line 112, in main
    path.join(FINAL_REPO_PATH, "org/opencv/opencv"))
  File "/usr/lib/python3.6/shutil.py", line 564, in move
    copy_function(src, real_dst)
  File "/usr/lib/python3.6/shutil.py", line 263, in copy2
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "/usr/lib/python3.6/shutil.py", line 120, in copyfile
    with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'build_java_shared/AndroidProject/OpenCV/build/repo/org/opencv/opencv'

@@ -0,0 +1,5 @@
cmake_minimum_required(VERSION 3.22.1)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Need to reduce required version as low as possible for old NDKs support.

Comment on lines +7 to +12
namespace 'org.opencv'
compileSdk 33

defaultConfig {
minSdk 21
targetSdk 33
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

To be discussed.

externalNativeBuild {
cmake {
path file('src/main/cpp/CMakeLists.txt')
version '3.22.1'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

To be discussed.

groupId = 'org.opencv'
artifactId = '${PACKAGE_NAME}'
version = '${OPENCV_VERSION}'
artifact("opencv-release.aar")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I propose to substitute OpenCV version to the file name.

Comment on lines +19 to +20
["sdk/native/libs/<ABI>/libopencv_java4.so", "jni/<ABI>/libopencv_java4.so"],
["sdk/native/libs/<ABI>/libopencv_java4.so", "prefab/modules/opencv_java4/libs/android.<ABI>/libopencv_java4.so"],
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please use OPENCV_VERSION_MAJOR instead of 4 to prevent issues with 5.x.

Comment on lines +25 to +26
COMPILED_AAR_PATH_1 = path.join(ANDROID_PROJECT_DIR, "OpenCV/build/outputs/aar/OpenCV-release.aar") # original package name
COMPILED_AAR_PATH_2 = path.join(ANDROID_PROJECT_DIR, "OpenCV/build/outputs/aar/opencv-release.aar") # lower case package name
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sounds strange. The file name is defined in the project template. Why do you have options here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

For some reason I have different filenames on Windows and Linux. Probably because of different Android SDK versions.

["sdk/java/res", "OpenCV/src/main/res"]
]

ABIS = ["arm64-v8a", "armeabi-v7a", "x86", "x86_64"] # if you want to change it, you also need to change Android project template
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I propose to build the list of ABIs from SDK content by listing sdk/native/libs. User may build custom SDK for reduced list of architectures. Also I do not see platforms definition in the Android project template.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I propose to add brief comment to the file why it's empty.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The same about empty file.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's not required. It's not recommend to build OpenCV inside source tree.

COMPILED_AAR_PATH_1 = path.join(ANDROID_PROJECT_DIR, "OpenCV/build/outputs/aar/OpenCV-release.aar") # original package name
COMPILED_AAR_PATH_2 = path.join(ANDROID_PROJECT_DIR, "OpenCV/build/outputs/aar/opencv-release.aar") # lower case package name
AAR_UNZIPPED_DIR = "build_java_shared/aar_unzipped"
FINAL_AAR_PATH = "outputs/opencv_java_shared.aar"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Add version to the file name.

print("Creating local maven repo...")

shutil.copy(FINAL_AAR_PATH, path.join(ANDROID_PROJECT_DIR, "OpenCV/opencv-release.aar"))
subprocess.run(["sh", "gradlew", "publishReleasePublicationToMyrepoRepository"],
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

let's set executable flag to gradlew and remove sh from command line.

@asmorkalov
Copy link
Copy Markdown
Contributor

Common proposals:

  • Extract minSdkversion, buildSDK version, CMake version, etc from template and make them command line variables. It allows to build custom aars for newer Android and has different CI configs in different branches.
  • Move both python scripts from aar-template to just platforms/android and make them executable.

Copy link
Copy Markdown
Contributor

@asmorkalov asmorkalov left a comment

Choose a reason for hiding this comment

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

👍

@asmorkalov
Copy link
Copy Markdown
Contributor

@mshabunin @opencv-alalek please review too.

@asmorkalov asmorkalov merged commit 30549d6 into opencv:4.x Nov 7, 2023
@@ -0,0 +1,156 @@
#!/usr/bin/env python
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
#!/usr/bin/env python
#!/usr/bin/env python3

I'd suggest using pathlib for all path/file related operations (https://docs.python.org/3.8/library/pathlib.html#correspondence-to-tools-in-the-os-module).
We support python 3.8+, do we?

@@ -0,0 +1,229 @@
#!/usr/bin/env python
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

At first glance this file seem to repeat build_java_shared_aar.py in many aspects, is it possible to join these files into a single script with --static/--dynamic flag?

asmorkalov pushed a commit that referenced this pull request Nov 17, 2023
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:
- opencv/ci-gha-workflow#124
- opencv-infrastructure/opencv-gha-dockerfile#26
IskXCr pushed a commit to Haosonn/opencv that referenced this pull request Dec 20, 2023
Added scripts for creating an AAR package and a local Maven repository with OpenCV library opencv#24456

Added scripts for creating an AAR package and a local Maven repository with OpenCV library.

The build_java_shared_aar.py script creates AAR with Java + C++ shared libraries.
The build_static_aar.py script creates AAR with static C++ libraries.

The scripts use an Android project template. The project is almost a default Android AAR library project with empty Java code and one empty C++ library. Only build.gradle.template and CMakeLists.txt.template files contain significant changes.

See README.md for more information.
IskXCr pushed a commit to Haosonn/opencv that referenced this pull request Dec 20, 2023
Updated Android samples for modern Android studio. Added OpenCV from Maven support. opencv#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 opencv#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:
- opencv/ci-gha-workflow#124
- opencv-infrastructure/opencv-gha-dockerfile#26
@opencv-alalek opencv-alalek removed their request for review December 26, 2023 20:45
thewoz pushed a commit to thewoz/opencv that referenced this pull request Jan 4, 2024
Added scripts for creating an AAR package and a local Maven repository with OpenCV library opencv#24456

Added scripts for creating an AAR package and a local Maven repository with OpenCV library.

The build_java_shared_aar.py script creates AAR with Java + C++ shared libraries.
The build_static_aar.py script creates AAR with static C++ libraries.

The scripts use an Android project template. The project is almost a default Android AAR library project with empty Java code and one empty C++ library. Only build.gradle.template and CMakeLists.txt.template files contain significant changes.

See README.md for more information.
thewoz pushed a commit to thewoz/opencv that referenced this pull request Jan 4, 2024
Updated Android samples for modern Android studio. Added OpenCV from Maven support. opencv#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 opencv#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:
- opencv/ci-gha-workflow#124
- opencv-infrastructure/opencv-gha-dockerfile#26
@asmorkalov asmorkalov mentioned this pull request Jan 19, 2024
thewoz pushed a commit to thewoz/opencv that referenced this pull request May 29, 2024
Added scripts for creating an AAR package and a local Maven repository with OpenCV library opencv#24456

Added scripts for creating an AAR package and a local Maven repository with OpenCV library.

The build_java_shared_aar.py script creates AAR with Java + C++ shared libraries.
The build_static_aar.py script creates AAR with static C++ libraries.

The scripts use an Android project template. The project is almost a default Android AAR library project with empty Java code and one empty C++ library. Only build.gradle.template and CMakeLists.txt.template files contain significant changes.

See README.md for more information.
thewoz pushed a commit to thewoz/opencv that referenced this pull request May 29, 2024
Updated Android samples for modern Android studio. Added OpenCV from Maven support. opencv#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 opencv#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:
- opencv/ci-gha-workflow#124
- opencv-infrastructure/opencv-gha-dockerfile#26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants