forked from opencv/opencv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_java_shared_aar.py
executable file
·182 lines (152 loc) · 7.83 KB
/
build_java_shared_aar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python
import argparse
from os import path
import os
import re
import shutil
import string
import subprocess
COPY_FROM_SDK_TO_ANDROID_PROJECT = [
["sdk/native/jni/include", "OpenCV/src/main/cpp/include"],
["sdk/java/src/org", "OpenCV/src/main/java/org"],
["sdk/java/res", "OpenCV/src/main/res"]
]
COPY_FROM_SDK_TO_APK = [
["sdk/native/libs/<ABI>/lib<LIB_NAME>.so", "jni/<ABI>/lib<LIB_NAME>.so"],
["sdk/native/libs/<ABI>/lib<LIB_NAME>.so", "prefab/modules/<LIB_NAME>/libs/android.<ABI>/lib<LIB_NAME>.so"],
]
ANDROID_PROJECT_TEMPLATE_DIR = path.join(path.dirname(__file__), "aar-template")
TEMP_DIR = "build_java_shared"
ANDROID_PROJECT_DIR = path.join(TEMP_DIR, "AndroidProject")
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 = path.join(TEMP_DIR, "aar_unzipped")
FINAL_AAR_PATH_TEMPLATE = "outputs/opencv_java_shared_<OPENCV_VERSION>.aar"
FINAL_REPO_PATH = "outputs/maven_repo"
MAVEN_PACKAGE_NAME = "opencv"
def fill_template(src_path, dst_path, args_dict):
with open(src_path, "r") as f:
template_text = f.read()
template = string.Template(template_text)
text = template.safe_substitute(args_dict)
with open(dst_path, "w") as f:
f.write(text)
def get_opencv_version(opencv_sdk_path):
version_hpp_path = path.join(opencv_sdk_path, "sdk/native/jni/include/opencv2/core/version.hpp")
with open(version_hpp_path, "rt") as f:
data = f.read()
major = re.search(r'^#define\W+CV_VERSION_MAJOR\W+(\d+)$', data, re.MULTILINE).group(1)
minor = re.search(r'^#define\W+CV_VERSION_MINOR\W+(\d+)$', data, re.MULTILINE).group(1)
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_compiled_aar_path(path1, path2):
if path.exists(path1):
return path1
elif path.exists(path2):
return path2
else:
raise Exception("Can't find compiled AAR path in [" + path1 + ", " + path2 + "]")
def cleanup(paths_to_remove):
exists = False
for p in paths_to_remove:
if path.exists(p):
exists = True
if path.isdir(p):
shutil.rmtree(p)
else:
os.remove(p)
print("Removed", p)
if not exists:
print("Nothing to remove")
def main(args):
opencv_version = get_opencv_version(args.opencv_sdk_path)
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)
print("Removing data from previous runs...")
cleanup([TEMP_DIR, final_aar_path, path.join(FINAL_REPO_PATH, "org/opencv", MAVEN_PACKAGE_NAME)])
print("Preparing Android project...")
# ANDROID_PROJECT_TEMPLATE_DIR contains an Android project template that creates AAR
shutil.copytree(ANDROID_PROJECT_TEMPLATE_DIR, ANDROID_PROJECT_DIR)
# Configuring the Android project to Java + shared C++ lib version
shutil.rmtree(path.join(ANDROID_PROJECT_DIR, "OpenCV/src/main/cpp/include"))
fill_template(path.join(ANDROID_PROJECT_DIR, "OpenCV/build.gradle.template"),
path.join(ANDROID_PROJECT_DIR, "OpenCV/build.gradle"),
{"LIB_NAME": lib_name,
"LIB_TYPE": "c++_shared",
"PACKAGE_NAME": MAVEN_PACKAGE_NAME,
"OPENCV_VERSION": opencv_version,
"COMPILE_SDK": args.android_compile_sdk,
"MIN_SDK": args.android_min_sdk,
"TARGET_SDK": args.android_target_sdk,
"ABI_FILTERS": ", ".join(['"' + x + '"' for x in abis]),
"JAVA_VERSION": args.java_version,
})
fill_template(path.join(ANDROID_PROJECT_DIR, "OpenCV/src/main/cpp/CMakeLists.txt.template"),
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),
path.join(ANDROID_PROJECT_DIR, dst))
print("Running gradle assembleRelease...")
# Running gradle to build the Android project
cmd = ["./gradlew", "assembleRelease"]
if args.offline:
cmd = cmd + ["--offline"]
subprocess.run(cmd, shell=False, cwd=ANDROID_PROJECT_DIR, check=True)
print("Adding libs to AAR...")
# The created AAR package doesn't contain C++ shared libs.
# We need to add them manually.
# AAR package is just a zip archive.
complied_aar_path = get_compiled_aar_path(COMPILED_AAR_PATH_1, COMPILED_AAR_PATH_2) # two possible paths
shutil.unpack_archive(complied_aar_path, AAR_UNZIPPED_DIR, "zip")
for abi in abis:
for src, dst in COPY_FROM_SDK_TO_APK:
src = src.replace("<ABI>", abi).replace("<LIB_NAME>", lib_name)
dst = dst.replace("<ABI>", abi).replace("<LIB_NAME>", lib_name)
shutil.copy(path.join(args.opencv_sdk_path, src),
path.join(AAR_UNZIPPED_DIR, dst))
# Creating final AAR zip archive
os.makedirs("outputs", exist_ok=True)
shutil.make_archive(final_aar_path, "zip", AAR_UNZIPPED_DIR, ".")
os.rename(final_aar_path + ".zip", final_aar_path)
print("Creating local maven repo...")
shutil.copy(final_aar_path, path.join(ANDROID_PROJECT_DIR, "OpenCV/opencv-release.aar"))
print("Creating a maven repo from project sources (with sources jar and javadoc jar)...")
cmd = ["./gradlew", "publishReleasePublicationToMyrepoRepository"]
if args.offline:
cmd = cmd + ["--offline"]
subprocess.run(cmd, shell=False, cwd=ANDROID_PROJECT_DIR, check=True)
os.makedirs(path.join(FINAL_REPO_PATH, "org/opencv"), exist_ok=True)
shutil.move(path.join(ANDROID_PROJECT_DIR, "OpenCV/build/repo/org/opencv", MAVEN_PACKAGE_NAME),
path.join(FINAL_REPO_PATH, "org/opencv", MAVEN_PACKAGE_NAME))
print("Creating a maven repo from modified AAR (with cpp libraries)...")
cmd = ["./gradlew", "publishModifiedPublicationToMyrepoRepository"]
if args.offline:
cmd = cmd + ["--offline"]
subprocess.run(cmd, shell=False, cwd=ANDROID_PROJECT_DIR, check=True)
# Replacing AAR from the first maven repo with modified AAR from the second maven repo
shutil.copytree(path.join(ANDROID_PROJECT_DIR, "OpenCV/build/repo/org/opencv", MAVEN_PACKAGE_NAME),
path.join(FINAL_REPO_PATH, "org/opencv", MAVEN_PACKAGE_NAME),
dirs_exist_ok=True)
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_min_sdk', default="21")
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="")
parser.add_argument('--offline', action="store_true", help="Force Gradle use offline mode")
args = parser.parse_args()
main(args)