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