diff --git a/.gitignore b/.gitignore index 08cdfdb..febddc5 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,4 @@ build # Ignore 3rd party binaries /externals/downloadable /externals/gst-1.0-android-universal +/externals/tensorflow-lite diff --git a/externals/build.gradle.kts b/externals/build.gradle.kts index cd39d61..abd3274 100644 --- a/externals/build.gradle.kts +++ b/externals/build.gradle.kts @@ -34,6 +34,11 @@ tasks { val gstVersion = libs.versions.gstreamer.get() val gstTarFileName = "gstreamer-1.0-android-universal-$gstVersion.tar" val gstAndroidPath = Path("${projectDir}/${project.properties["dir.gstAndroid"].toString()}") + val gstAndroidUniversalUrl = "https://gstreamer.freedesktop.org/pkg/android/$gstVersion" + + val fileList = mutableListOf( + Triple(gstTarFileName, gstAndroidPath, gstAndroidUniversalUrl) + ) fun downloadFile(url: URL, outputFileName: String) { url.openStream().use { @@ -51,41 +56,65 @@ tasks { } } + fun prepareTflite() { + val tfliteVersion = libs.versions.tensorflowLite.get() + val tfliteTarFileName = "tensorflow-lite-$tfliteVersion.tar" + val tfliteAndroidPath = Path("${projectDir}/${project.properties["dir.tfliteAndroid"].toString()}") + val tfliteUrl = "https://raw.githubusercontent.com/nnstreamer/nnstreamer-android-resource/master/external" + + fileList.add(Triple(tfliteTarFileName, tfliteAndroidPath, tfliteUrl)) + } + register("prepareDownloadable") { + if (project.hasProperty("dir.tfliteAndroid")) { + prepareTflite() + } + if (!downloadablePath.isDirectory()) { downloadablePath.createDirectories() } - //gstreamer-1.0-android-universal - val gstXzFileName = "$gstTarFileName.xz" - val gstAndroidUniversalUrl = URL("https://gstreamer.freedesktop.org/pkg/android/$gstVersion/$gstXzFileName") - - if (!Path("$downloadablePath/$gstTarFileName").isRegularFile()) { - println("Could not find $gstTarFileName") - println("...downloading from https://gstreamer.freedesktop.org/pkg/android") - println("This step may take some time to complete...") - downloadFile(gstAndroidUniversalUrl, "$downloadablePath/$gstXzFileName") - - try { - File("$downloadablePath/$gstXzFileName").inputStream().buffered().use { bufferedIn -> - XZCompressorInputStream(bufferedIn).toFile("$downloadablePath/$gstTarFileName") + for (file in fileList) { + val tarFileName = file.first + val androidPath = file.second + val url = file.third + val xzFileName = "$tarFileName.xz" + + if (!Path("$androidPath").isDirectory()) { + println("Could not find $androidPath") + println("...downloading from $url") + println("This step may take some time to complete...") + downloadFile(URL("$url/$xzFileName"), "$downloadablePath/$xzFileName") + + try { + File("$downloadablePath/$xzFileName").inputStream().buffered().use { bufferedIn -> + XZCompressorInputStream(bufferedIn).toFile("$downloadablePath/$tarFileName") + } + } catch (e: java.io.IOException) { + println("Failed to decompress $downloadablePath/$xzFileName") + } finally { + Path("$downloadablePath/xzFileName").deleteIfExists() } - } catch (e: java.io.IOException) { - println("Failed to decompress $downloadablePath/$gstXzFileName") - } finally { - Path("$downloadablePath/$gstXzFileName").deleteIfExists() } } } - register("copyFromTar", Copy::class) { - from(tarTree("${downloadablePath.toUri()}/$gstTarFileName")) - into(gstAndroidPath) - - dependsOn("prepareDownloadable") + register("copyFromTar") { doLast { - Path("$downloadablePath/$gstTarFileName").deleteIfExists() + for (file in fileList) { + val tarFileName = file.first + val androidPath = file.second + if (!Path("$androidPath").isDirectory()) { + copy { + from(tarTree("${downloadablePath.toUri()}/$tarFileName")) + into(androidPath) + } + } + Path("$downloadablePath/$tarFileName").deleteIfExists() + } } + + dependsOn("prepareDownloadable") } register("initGitSubmodules", GitTask::class) { diff --git a/gradle.properties b/gradle.properties index 67866e6..1560088 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,6 +2,7 @@ org.gradle.jvmargs=-Xmx4096M -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF dir.externals=externals dir.downloadable=downloadable dir.gstAndroid=gst-1.0-android-universal +dir.tfliteAndroid=tensorflow-lite dir.nnstreamer=nnstreamer dir.mlApi=ml-api url.repo.nnstreamer=https://github.com/nnstreamer/nnstreamer diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index c1ff257..e1e68f8 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,5 +1,6 @@ [versions] gstreamer = "1.24.0" +tensorflowLite = "2.8.1" tukaani-xz-plugin = "1.9" commons-compress-lib = "1.26.1" xyz-simple-git-plugin = "2.0.3" diff --git a/nnstreamer-api/build.gradle.kts b/nnstreamer-api/build.gradle.kts index bc4a9c6..2e02b0b 100644 --- a/nnstreamer-api/build.gradle.kts +++ b/nnstreamer-api/build.gradle.kts @@ -11,7 +11,6 @@ android { val externalDirPath by rootProject.extra { project.rootDir.toPath().resolve(properties["dir.externals"].toString()) } - val relativePathExternals = projectDir.toPath().relativize(externalDirPath) namespace = "org.nnsuite.nnstreamer" compileSdk = libs.versions.android.compile.sdk.get().toInt() @@ -31,6 +30,10 @@ android { "ML_API_ROOT=$externalDirPath/ml-api" ) targets("nnstreamer-native") + + if (project.hasProperty("dir.tfliteAndroid")) { + arguments("TFLITE_ROOT_ANDROID=$externalDirPath/tensorflow-lite/tensorflow-lite") + } } } } @@ -45,7 +48,7 @@ android { } externalNativeBuild { ndkBuild { - path=file("$relativePathExternals/ml-api/java/android/nnstreamer/src/main/jni/Android.mk") + path=file(project.projectDir.toPath().resolve("src/main/jni/Android.mk")) } } @@ -59,7 +62,7 @@ android { val genNnsSrc = tasks.register("genNnsSrc", Copy::class) { val srcDirPath = externalDirPath.resolve("ml-api/java/android/nnstreamer/src/main/java/org/nnsuite/nnstreamer") - val outDirPath = project.projectDir.toPath().resolve("src/main/java/org/nnsuite/nnstreamer").apply { + val outDirPath = project.projectDir.toPath().resolve("src/main/java/org/nnsuite/nnstreamer/java").apply { createDirectories() } @@ -89,8 +92,30 @@ android { } tasks { + register("genJniSrc", Copy::class) { + val srcDirPath = externalDirPath.resolve("ml-api/java/android/nnstreamer/src/main/jni") + val outDirPath = project.projectDir.toPath().resolve("src/main/jni").apply { + createDirectories() + } + + group = BasePlugin.BUILD_GROUP + description = "Generates NNStreamer JNI sources" + + from(srcDirPath) + into(outDirPath) + + if (project.hasProperty("dir.tfliteAndroid")) { + val tfliteVersion = libs.versions.tensorflowLite.get() + filter { line: String -> + line.replace("TFLITE_VERSION := 2.8.1", "TFLITE_VERSION := $tfliteVersion") + } + } + filteringCharset = "UTF-8" + } + named("preBuild") { dependsOn("genNnsSrc") + dependsOn("genJniSrc") } } }