build.gradle 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import java.security.MessageDigest
  2. apply plugin: 'com.android.application'
  3. android {
  4. compileSdkVersion 28
  5. defaultConfig {
  6. applicationId "com.baidu.paddleseg.lite.demo.human_matting"
  7. minSdkVersion 15
  8. versionCode 1
  9. versionName "1.0"
  10. testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  11. }
  12. buildTypes {
  13. release {
  14. minifyEnabled false
  15. proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
  16. }
  17. }
  18. }
  19. dependencies {
  20. implementation fileTree(include: ['*.jar'], dir: 'libs')
  21. implementation 'com.android.support:appcompat-v7:28.0.0'
  22. implementation 'com.android.support.constraint:constraint-layout:1.1.3'
  23. implementation 'com.android.support:design:28.0.0'
  24. testImplementation 'junit:junit:4.12'
  25. androidTestImplementation 'com.android.support.test:runner:1.0.2'
  26. androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
  27. implementation files('libs/PaddlePredictor.jar') //添加PaddleLite包
  28. }
  29. def paddleLiteLibs = 'https://paddlelite-demo.bj.bcebos.com/libs/android/paddle_lite_libs_v2_8_0.tar.gz'
  30. task downloadAndExtractPaddleLiteLibs(type: DefaultTask) {
  31. doFirst {
  32. println "Downloading and extracting Paddle Lite libs"
  33. }
  34. doLast {
  35. // Prepare cache folder for libs
  36. if (!file("cache").exists()) {
  37. mkdir "cache"
  38. }
  39. // Generate cache name for libs
  40. MessageDigest messageDigest = MessageDigest.getInstance('MD5')
  41. messageDigest.update(paddleLiteLibs.bytes)
  42. String cacheName = new BigInteger(1, messageDigest.digest()).toString(32)
  43. // Download libs
  44. if (!file("cache/${cacheName}.tar.gz").exists()) {
  45. ant.get(src: paddleLiteLibs, dest: file("cache/${cacheName}.tar.gz"))
  46. }
  47. // Unpack libs
  48. copy {
  49. from tarTree("cache/${cacheName}.tar.gz")
  50. into "cache/${cacheName}"
  51. }
  52. // Copy PaddlePredictor.jar
  53. if (!file("libs/PaddlePredictor.jar").exists()) {
  54. copy {
  55. from "cache/${cacheName}/java/PaddlePredictor.jar"
  56. into "libs"
  57. }
  58. }
  59. // Copy libpaddle_lite_jni.so for armeabi-v7a and arm64-v8a
  60. if (!file("src/main/jniLibs/armeabi-v7a/libpaddle_lite_jni.so").exists()) {
  61. copy {
  62. from "cache/${cacheName}/java/libs/armeabi-v7a/"
  63. into "src/main/jniLibs/armeabi-v7a"
  64. }
  65. }
  66. if (!file("src/main/jniLibs/arm64-v8a/libpaddle_lite_jni.so").exists()) {
  67. copy {
  68. from "cache/${cacheName}/java/libs/arm64-v8a/"
  69. into "src/main/jniLibs/arm64-v8a"
  70. }
  71. }
  72. }
  73. }
  74. preBuild.dependsOn downloadAndExtractPaddleLiteLibs
  75. def paddleLiteModels = [
  76. [
  77. 'src' : 'https://paddleseg.bj.bcebos.com/matting/models/deploy/hrnet_w18.tar.gz',
  78. 'dest' : 'src/main/assets/image_matting/models/modnet'
  79. ],
  80. ]
  81. task downloadAndExtractPaddleLiteModels(type: DefaultTask) {
  82. doFirst {
  83. println "Downloading and extracting Paddle Lite models"
  84. }
  85. doLast {
  86. // Prepare cache folder for models
  87. if (!file("cache").exists()) {
  88. mkdir "cache"
  89. }
  90. paddleLiteModels.eachWithIndex { model, index ->
  91. MessageDigest messageDigest = MessageDigest.getInstance('MD5')
  92. messageDigest.update(model.src.bytes)
  93. String cacheName = new BigInteger(1, messageDigest.digest()).toString(32)
  94. // Download model file
  95. if (!file("cache/${cacheName}.tar.gz").exists()) {
  96. ant.get(src: model.src, dest: file("cache/${cacheName}.tar.gz"))
  97. }
  98. // Unpack model file
  99. copy {
  100. from tarTree("cache/${cacheName}.tar.gz")
  101. into "cache/${cacheName}"
  102. }
  103. // Copy model file
  104. if (!file("${model.dest}/hrnet_w18.nb").exists()) {
  105. copy {
  106. from "cache/${cacheName}"
  107. into "${model.dest}"
  108. }
  109. }
  110. }
  111. }
  112. }
  113. preBuild.dependsOn downloadAndExtractPaddleLiteModels