def paddleLiteLibs = 'https://paddlelite-demo.bj.bcebos.com/libs/android/paddle_lite_libs_v2_9_0.tar.gz' task downloadAndExtractPaddleLiteLibs(type: DefaultTask) { doFirst { println "Downloading and extracting Paddle Lite libs" } doLast { // Prepare cache folder for libs if (!file("cache").exists()) { mkdir "cache" } // Generate cache name for libs MessageDigest messageDigest = MessageDigest.getInstance('MD5') messageDigest.update(paddleLiteLibs.bytes) String cacheName = new BigInteger(1, messageDigest.digest()).toString(32) // Download libs if (!file("cache/${cacheName}.tar.gz").exists()) { ant.get(src: paddleLiteLibs, dest: file("cache/${cacheName}.tar.gz")) } // Unpack libs if (!file("cache/${cacheName}").exists()) { copy { from tarTree("cache/${cacheName}.tar.gz") into "cache/${cacheName}" } }
// Copy PaddlePredictor.jar if (!file("libs/PaddlePredictor.jar").exists()) { copy { from "cache/${cacheName}/java/PaddlePredictor.jar" into "libs" } } // Copy libpaddle_lite_jni.so for armeabi-v7a and arm64-v8a if (!file("src/main/jniLibs/armeabi-v7a/libpaddle_lite_jni.so").exists()) { copy { from "cache/${cacheName}/java/libs/armeabi-v7a/" into "src/main/jniLibs/armeabi-v7a" // into "src/main/jni/armeabi-v7a" } } if (!file("src/main/jniLibs/arm64-v8a/libpaddle_lite_jni.so").exists()) { copy { from "cache/${cacheName}/java/libs/arm64-v8a/" into "src/main/jniLibs/arm64-v8a" // into "src/main/jni/arm64-v8a" } } } } preBuild.dependsOn downloadAndExtractPaddleLiteLibs
def paddleLiteModels = [ [ 'src' : 'https://paddlelite-demo.bj.bcebos.com/models/deeplab_mobilenet_fp32_for_cpu_v2_9_0.tar.gz', 'dest': 'src/main/assets/image_segmentation/models/deeplab_mobilenet_for_cpu' ], ] task downloadAndExtractPaddleLiteModels(type: DefaultTask) { doFirst { println "Downloading and extracting Paddle Lite models" } doLast { // Prepare cache folder for models String cachePath = "cache" if (!file("${cachePath}").exists()) { mkdir "${cachePath}" } paddleLiteModels.eachWithIndex { model, index -> MessageDigest messageDigest = MessageDigest.getInstance('MD5') messageDigest.update(model.src.bytes) String cacheName = new BigInteger(1, messageDigest.digest()).toString(32) // Download the target model if not exists boolean copyFiles = !file("${model.dest}").exists() if (!file("${cachePath}/${cacheName}.tar.gz").exists()) { ant.get(src: model.src, dest: file("${cachePath}/${cacheName}.tar.gz")) copyFiles = true; // force to copy files from the latest archive files } // Copy model file if (copyFiles) { copy { from tarTree("${cachePath}/${cacheName}.tar.gz") into "${model.dest}" } } } } } preBuild.dependsOn downloadAndExtractPaddleLiteModels
# Creates and names a library, sets it as either STATIC # or SHARED, and provides the relative paths to its source code. # You can define multiple libraries, and CMake builds them for you. # Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library. native-lib
# Sets the library as a shared library. SHARED
# Provides a relative path to your source file(s). native-lib.cpp)
# Searches for a specified prebuilt library and stores the path as a # variable. Because CMake includes system libraries in the search path by # default, you only need to specify the name of the public NDK library # you want to add. CMake verifies that the library exists before # completing its build.
find_library( # Sets the name of the path variable. log-lib
# Specifies the name of the NDK library that # you want CMake to locate. log)
# Specifies libraries CMake should link to your target library. You # can link multiple libraries, such as libraries you define in this # build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library. native-lib
# 引入opencv libopencv_java3 # Links the target library to the log library # included in the NDK. ${log-lib})
def paddleLiteLibs = 'https://paddlelite-demo.bj.bcebos.com/libs/android/paddle_lite_libs_v2_9_0.tar.gz' task downloadAndExtractPaddleLiteLibs(type: DefaultTask) { doFirst { println "Downloading and extracting Paddle Lite libs" } doLast { // Prepare cache folder for libs if (!file("cache").exists()) { mkdir "cache" } // Generate cache name for libs MessageDigest messageDigest = MessageDigest.getInstance('MD5') messageDigest.update(paddleLiteLibs.bytes) String cacheName = new BigInteger(1, messageDigest.digest()).toString(32) // Download libs if (!file("cache/${cacheName}.tar.gz").exists()) { ant.get(src: paddleLiteLibs, dest: file("cache/${cacheName}.tar.gz")) } // Unpack libs if (!file("cache/${cacheName}").exists()) { copy { from tarTree("cache/${cacheName}.tar.gz") into "cache/${cacheName}" } }
// Copy PaddlePredictor.jar if (!file("libs/PaddlePredictor.jar").exists()) { copy { from "cache/${cacheName}/java/PaddlePredictor.jar" into "libs" } } // Copy libpaddle_lite_jni.so for armeabi-v7a and arm64-v8a if (!file("src/main/jniLibs/armeabi-v7a/libpaddle_lite_jni.so").exists()) { copy { from "cache/${cacheName}/java/libs/armeabi-v7a/" into "src/main/jniLibs/armeabi-v7a" // into "src/main/jni/armeabi-v7a" } } if (!file("src/main/jniLibs/arm64-v8a/libpaddle_lite_jni.so").exists()) { copy { from "cache/${cacheName}/java/libs/arm64-v8a/" into "src/main/jniLibs/arm64-v8a" // into "src/main/jni/arm64-v8a" } } } } preBuild.dependsOn downloadAndExtractPaddleLiteLibs
def paddleLiteModels = [ [ 'src' : 'https://paddlelite-demo.bj.bcebos.com/models/deeplab_mobilenet_fp32_for_cpu_v2_9_0.tar.gz', 'dest': 'src/main/assets/image_segmentation/models/deeplab_mobilenet_for_cpu' ], ] task downloadAndExtractPaddleLiteModels(type: DefaultTask) { doFirst { println "Downloading and extracting Paddle Lite models" } doLast { // Prepare cache folder for models String cachePath = "cache" if (!file("${cachePath}").exists()) { mkdir "${cachePath}" } paddleLiteModels.eachWithIndex { model, index -> MessageDigest messageDigest = MessageDigest.getInstance('MD5') messageDigest.update(model.src.bytes) String cacheName = new BigInteger(1, messageDigest.digest()).toString(32) // Download the target model if not exists boolean copyFiles = !file("${model.dest}").exists() if (!file("${cachePath}/${cacheName}.tar.gz").exists()) { ant.get(src: model.src, dest: file("${cachePath}/${cacheName}.tar.gz")) copyFiles = true; // force to copy files from the latest archive files } // Copy model file if (copyFiles) { copy { from tarTree("${cachePath}/${cacheName}.tar.gz") into "${model.dest}" } } } } } preBuild.dependsOn downloadAndExtractPaddleLiteModels
# Creates and names a library, sets it as either STATIC # or SHARED, and provides the relative paths to its source code. # You can define multiple libraries, and CMake builds them for you. # Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library. native-lib
# Sets the library as a shared library. SHARED
# Provides a relative path to your source file(s). native-lib.cpp # 需要使用到的cpp文件这里都要引入,否则会出问题 demo/demo.cpp demo/Scanner.cpp demo/document_detection.cpp
)
# Searches for a specified prebuilt library and stores the path as a # variable. Because CMake includes system libraries in the search path by # default, you only need to specify the name of the public NDK library # you want to add. CMake verifies that the library exists before # completing its build.
find_library( # Sets the name of the path variable. log-lib
# Specifies the name of the NDK library that # you want CMake to locate. log)
# Specifies libraries CMake should link to your target library. You # can link multiple libraries, such as libraries you define in this # build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library. native-lib
# 引入opencv libopencv_java3 # Links the target library to the log library # included in the NDK. ${log-lib})