/android/pytorch_android/build.gradle

https://github.com/pytorch/pytorch · Gradle · 144 lines · 129 code · 15 blank · 0 comment · 0 complexity · 9131d972395f884ac5fdf31baa0a799d MD5 · raw file

  1. apply plugin: 'com.android.library'
  2. apply plugin: 'maven'
  3. android {
  4. compileSdkVersion rootProject.compileSdkVersion
  5. buildToolsVersion rootProject.buildToolsVersion
  6. defaultConfig {
  7. minSdkVersion rootProject.minSdkVersion
  8. targetSdkVersion rootProject.targetSdkVersion
  9. versionCode 0
  10. versionName "0.1"
  11. testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  12. ndk {
  13. abiFilters ABI_FILTERS.split(",")
  14. }
  15. externalNativeBuild {
  16. cmake {
  17. arguments "-DANDROID_STL=c++_shared"
  18. }
  19. }
  20. }
  21. buildTypes {
  22. debug {
  23. minifyEnabled false
  24. debuggable true
  25. }
  26. release {
  27. minifyEnabled false
  28. }
  29. }
  30. sourceSets {
  31. main {
  32. java {
  33. exclude 'org/pytorch/LiteModuleLoader.java'
  34. exclude 'org/pytorch/LiteNativePeer.java'
  35. }
  36. jniLibs.srcDirs = ['src/main/jniLibs']
  37. }
  38. androidTest {
  39. java {
  40. exclude 'org/pytorch/PytorchHostTests.java'
  41. }
  42. }
  43. }
  44. externalNativeBuild {
  45. cmake {
  46. path "CMakeLists.txt"
  47. }
  48. }
  49. packagingOptions {
  50. if (nativeLibsDoNotStrip.toBoolean()) {
  51. doNotStrip "**/*.so"
  52. logger.warn('WARNING: nativeLibsDoNotStrip==true; debug symbols included')
  53. }
  54. }
  55. useLibrary 'android.test.runner'
  56. useLibrary 'android.test.base'
  57. useLibrary 'android.test.mock'
  58. }
  59. dependencies {
  60. implementation 'com.facebook.fbjni:fbjni-java-only:' + rootProject.fbjniJavaOnlyVersion
  61. implementation 'com.android.support:appcompat-v7:' + rootProject.androidSupportAppCompatV7Version
  62. implementation 'com.facebook.soloader:nativeloader:' + rootProject.soLoaderNativeLoaderVersion
  63. testImplementation 'junit:junit:' + rootProject.junitVersion
  64. testImplementation 'androidx.test:core:' + rootProject.coreVersion
  65. androidTestImplementation 'junit:junit:' + rootProject.junitVersion
  66. androidTestImplementation 'androidx.test:core:' + rootProject.coreVersion
  67. androidTestImplementation 'androidx.test.ext:junit:' + rootProject.extJUnitVersion
  68. androidTestImplementation 'androidx.test:rules:' + rootProject.rulesVersion
  69. androidTestImplementation 'androidx.test:runner:' + rootProject.runnerVersion
  70. }
  71. apply from: rootProject.file('gradle/release.gradle')
  72. task sourcesJar(type: Jar) {
  73. from android.sourceSets.main.java.srcDirs
  74. classifier = 'sources'
  75. }
  76. def getLibtorchHeadersDir() {
  77. def abi = ABI_FILTERS.split(",")[0]
  78. return "$rootDir/pytorch_android/src/main/cpp/libtorch_include/$abi"
  79. }
  80. afterEvaluate {
  81. if (POM_PACKAGING == 'aar') {
  82. android.libraryVariants.all { variant ->
  83. variant.outputs.each { output ->
  84. File f = output.outputFile
  85. if (f.name.endsWith(".aar")) {
  86. output.assemble.finalizedBy addFolderToAarTask(
  87. "addHeadersToAar" + variant.name,
  88. f.path,
  89. getLibtorchHeadersDir(),
  90. "headers")
  91. }
  92. }
  93. }
  94. }
  95. }
  96. tasks.whenTaskAdded { task ->
  97. if (task.name.startsWith("bundle") && task.name.endsWith("Aar")) {
  98. task.doLast {
  99. addFolderToAarTask(
  100. "addHeadersTo" + task.name,
  101. task.archivePath,
  102. getLibtorchHeadersDir(),
  103. "headers").execute()
  104. }
  105. }
  106. }
  107. def addFolderToAarTask(taskName, aarPath, folderPath, folderPathInAar) {
  108. def tmpDir = file("${buildDir}/${taskName}")
  109. tmpDir.mkdir()
  110. def tmpDirFolder = file("${tmpDir.path}/${folderPathInAar}")
  111. tmpDirFolder.mkdir()
  112. return tasks.create(name: taskName) {
  113. doLast {
  114. copy {
  115. from zipTree(aarPath)
  116. into tmpDir
  117. }
  118. copy {
  119. from fileTree(folderPath)
  120. into tmpDirFolder
  121. }
  122. ant.zip(destfile: aarPath) {
  123. fileset(dir: tmpDir.path)
  124. }
  125. delete tmpDir
  126. }
  127. }
  128. }
  129. artifacts.add('archives', sourcesJar)