@@ -7,24 +7,43 @@ plugins {
77 id(" kotlin-parcelize" )
88}
99
10- // Task to build the Go library with native JNI
11- tasks.register<Exec >(" buildGoLibrary" ) {
12- group = " build"
13- description = " Build the Go kinetic library with native JNI"
14- workingDir = file(" src/main/go" )
10+ val nativeAbis = listOf (" arm64-v8a" , " armeabi-v7a" , " x86" , " x86_64" )
11+ val goSourceDir = layout.projectDirectory.dir(" src/main/go" )
12+ val thirdPartyOutputDir = goSourceDir.dir(" third_party/output" )
13+ val jniLibsDir = layout.projectDirectory.dir(" src/main/jniLibs" )
14+ val sharedThirdPartyLibs = listOf (" libusb-1.0.so" , " libsrt.so" , " librist.so" )
15+ val isWindowsHost = System .getProperty(" os.name" ).lowercase().contains(" windows" )
16+
17+ fun thirdPartyLibraries () = nativeAbis.flatMap { abi ->
18+ val libDir = thirdPartyOutputDir.dir(" $abi /lib" )
19+ val opensslLibExtension = if (isWindowsHost) " a" else " so"
20+ sharedThirdPartyLibs.map { libDir.file(it) } + listOf (
21+ libDir.file(" libssl.$opensslLibExtension " ),
22+ libDir.file(" libcrypto.$opensslLibExtension " ),
23+ )
24+ }
1525
16- // Detect OS and use appropriate command
17- val isWindows = System .getProperty(" os.name" ).lowercase().contains(" windows" )
26+ fun jniLibraries () = nativeAbis.flatMap { abi ->
27+ val libDir = jniLibsDir.dir(abi)
28+ val requiredLibs = listOf (
29+ libDir.file(" libkinetic.so" ),
30+ libDir.file(" libusb-1.0.so" ),
31+ libDir.file(" libsrt.so" ),
32+ libDir.file(" librist.so" ),
33+ )
34+ if (isWindowsHost) requiredLibs else requiredLibs + listOf (
35+ libDir.file(" libssl.so" ),
36+ libDir.file(" libcrypto.so" ),
37+ )
38+ }
1839
19- if (isWindows) {
20- commandLine(" powershell" , " -ExecutionPolicy" , " Bypass" , " -File" , " ./build.ps1" )
21- } else {
22- commandLine(" bash" , " ./build.sh" )
23- }
40+ fun Exec.configureNativeBuildEnvironment () {
41+ workingDir = goSourceDir.asFile
2442
2543 // Set environment variables if needed
2644 // Look for NDK in the Android SDK location, falling back to local.properties / OS defaults
2745 val osName = System .getProperty(" os.name" ).lowercase()
46+ val isWindows = osName.contains(" windows" )
2847 val isMac = osName.contains(" mac" ) || osName.contains(" darwin" )
2948 val sdkDirFromProps = rootProject.file(" local.properties" ).takeIf { it.exists() }
3049 ?.readLines()
@@ -52,6 +71,55 @@ tasks.register<Exec>("buildGoLibrary") {
5271 environment(" ANDROID_HOME" , androidHome)
5372}
5473
74+ val buildThirdPartyDeps = tasks.register<Exec >(" buildThirdPartyDeps" ) {
75+ group = " build"
76+ description = " Build third-party native dependencies used by the Go JNI library"
77+
78+ configureNativeBuildEnvironment()
79+
80+ if (isWindowsHost) {
81+ commandLine(" powershell" , " -ExecutionPolicy" , " Bypass" , " -File" , " ./build.ps1" , " -DepsOnly" )
82+ } else {
83+ commandLine(" bash" , " ./build.sh" , " --deps-only" )
84+ }
85+
86+ inputs.file(goSourceDir.file(" build.sh" ))
87+ inputs.file(goSourceDir.file(" build.ps1" ))
88+ inputs.file(goSourceDir.file(" third_party/CMakeLists.txt" ))
89+ outputs.files(thirdPartyLibraries())
90+ outputs.dirs(nativeAbis.map { abi -> thirdPartyOutputDir.dir(" $abi /include" ) })
91+ }
92+
93+ // Task to build the Go library with native JNI
94+ tasks.register<Exec >(" buildGoLibrary" ) {
95+ group = " build"
96+ description = " Build the Go kinetic library with native JNI"
97+ dependsOn(buildThirdPartyDeps)
98+
99+ configureNativeBuildEnvironment()
100+
101+ // Detect OS and use appropriate command
102+ if (isWindowsHost) {
103+ commandLine(" powershell" , " -ExecutionPolicy" , " Bypass" , " -File" , " ./build.ps1" , " -SkipDeps" )
104+ } else {
105+ commandLine(" bash" , " ./build.sh" , " --skip-deps" )
106+ }
107+
108+ inputs.files(fileTree(goSourceDir) {
109+ include(" **/*.go" )
110+ include(" **/*.c" )
111+ include(" **/*.h" )
112+ include(" go.mod" )
113+ include(" go.sum" )
114+ exclude(" include/**" )
115+ exclude(" third_party/**" )
116+ })
117+ inputs.files(thirdPartyLibraries())
118+ inputs.file(goSourceDir.file(" build.sh" ))
119+ inputs.file(goSourceDir.file(" build.ps1" ))
120+ outputs.files(jniLibraries())
121+ }
122+
55123// Make preBuild depend on buildGoLibrary
56124tasks.named(" preBuild" ) {
57125 dependsOn(" buildGoLibrary" )
0 commit comments