xref: /aosp_15_r20/external/pytorch/android/test_app/app/build.gradle (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1apply plugin: 'com.android.application'
2
3repositories {
4    jcenter()
5    maven {
6        url "https://oss.sonatype.org/content/repositories/snapshots"
7    }
8    flatDir {
9        dirs 'aars'
10    }
11}
12
13android {
14    configurations {
15        extractForNativeBuild
16    }
17    compileOptions {
18        sourceCompatibility 1.8
19        targetCompatibility 1.8
20    }
21    compileSdkVersion rootProject.compileSdkVersion
22    buildToolsVersion rootProject.buildToolsVersion
23    defaultConfig {
24        applicationId "org.pytorch.testapp"
25        minSdkVersion rootProject.minSdkVersion
26        targetSdkVersion rootProject.targetSdkVersion
27        versionCode 1
28        versionName "1.0"
29        ndk {
30            abiFilters ABI_FILTERS.split(",")
31        }
32        // Commented due to dependency on local copy of pytorch_android aar to aars folder
33        //externalNativeBuild {
34        //    cmake {
35        //        abiFilters ABI_FILTERS.split(",")
36        //        arguments "-DANDROID_STL=c++_shared"
37        //    }
38        //}
39        buildConfigField("String", "MODULE_ASSET_NAME", "\"mobilenet2q.pt\"")
40        buildConfigField("String", "LOGCAT_TAG", "@string/app_name")
41        buildConfigField("long[]", "INPUT_TENSOR_SHAPE", "new long[]{1, 3, 224, 224}")
42        buildConfigField("boolean", "NATIVE_BUILD", 'false')
43        buildConfigField("boolean", "USE_VULKAN_DEVICE", 'false')
44        buildConfigField(
45                "int",
46                "BUILD_LITE_INTERPRETER",
47                System.env.BUILD_LITE_INTERPRETER != null ? System.env.BUILD_LITE_INTERPRETER : "1"
48        )
49        addManifestPlaceholders([APP_NAME: "@string/app_name", MAIN_ACTIVITY: "org.pytorch.testapp.MainActivity"])
50    }
51    buildTypes {
52        debug {
53            minifyEnabled false
54            debuggable true
55        }
56        release {
57            minifyEnabled false
58        }
59    }
60    // Commented due to dependency on local copy of pytorch_android aar to aars folder
61    //externalNativeBuild {
62    //    cmake {
63    //        path "CMakeLists.txt"
64    //    }
65    //}
66    flavorDimensions "model", "build", "activity"
67    productFlavors {
68        mnet {
69            dimension "model"
70            applicationIdSuffix ".mnet"
71            buildConfigField("String", "MODULE_ASSET_NAME", "\"mobilenet_v2.ptl\"")
72            addManifestPlaceholders([APP_NAME: "MNET"])
73            buildConfigField("String", "LOGCAT_TAG", "\"pytorch-mnet\"")
74        }
75        // NB: This is not working atm https://github.com/pytorch/pytorch/issues/102966
76        mnetVulkan {
77            dimension "model"
78            applicationIdSuffix ".mnet_vulkan"
79            buildConfigField("String", "MODULE_ASSET_NAME", "\"mobilenet_v2_vulkan.ptl\"")
80            buildConfigField("boolean", "USE_VULKAN_DEVICE", 'true')
81            addManifestPlaceholders([APP_NAME: "MNET_VULKAN"])
82            buildConfigField("String", "LOGCAT_TAG", "\"pytorch-mnet-vulkan\"")
83        }
84        resnet18 {
85            dimension "model"
86            applicationIdSuffix ".resnet18"
87            buildConfigField("String", "MODULE_ASSET_NAME", "\"resnet18.ptl\"")
88            addManifestPlaceholders([APP_NAME: "RN18"])
89            buildConfigField("String", "LOGCAT_TAG", "\"pytorch-resnet18\"")
90        }
91        local {
92            dimension "build"
93        }
94        nightly {
95            dimension "build"
96        }
97        aar {
98            dimension "build"
99        }
100        // Commented due to dependency on local copy of pytorch_android aar to aars folder
101        //nativeBuild {
102        //    dimension "build"
103        //    buildConfigField("boolean", "NATIVE_BUILD", "true")
104        //}
105        camera {
106            dimension "activity"
107            addManifestPlaceholders([MAIN_ACTIVITY: "org.pytorch.testapp.CameraActivity"])
108        }
109        base {
110            dimension "activity"
111            sourceSets {
112                main {
113                    java {
114                        exclude 'org/pytorch/testapp/CameraActivity.java'
115                    }
116                }
117            }
118        }
119    }
120    packagingOptions {
121        doNotStrip '**.so'
122    }
123
124    // Filtering for CI
125    if (!testAppAllVariantsEnabled.toBoolean()) {
126        variantFilter { variant ->
127            def names = variant.flavors*.name
128            if (names.contains("nightly")
129                || names.contains("camera")
130                || names.contains("aar")
131                || names.contains("nativeBuild")) {
132                setIgnore(true)
133            }
134        }
135    }
136}
137
138tasks.all { task ->
139    // Disable externalNativeBuild for all but nativeBuild variant
140    if (task.name.startsWith('externalNativeBuild')
141          && !task.name.contains('NativeBuild')) {
142        task.enabled = false
143    }
144}
145
146dependencies {
147    implementation 'com.android.support:appcompat-v7:28.0.0'
148    implementation 'com.facebook.soloader:nativeloader:0.10.5'
149
150    localImplementation project(':pytorch_android')
151    localImplementation project(':pytorch_android_torchvision')
152
153    // Commented due to dependency on local copy of pytorch_android aar to aars folder
154    //nativeBuildImplementation(name: 'pytorch_android-release', ext: 'aar')
155    //nativeBuildImplementation(name: 'pytorch_android_torchvision-release', ext: 'aar')
156    //extractForNativeBuild(name: 'pytorch_android-release', ext: 'aar')
157
158    nightlyImplementation 'org.pytorch:pytorch_android:2.2.0-SNAPSHOT'
159    nightlyImplementation 'org.pytorch:pytorch_android_torchvision:2.2.0-SNAPSHOT'
160
161    aarImplementation(name:'pytorch_android', ext:'aar')
162    aarImplementation(name:'pytorch_android_torchvision', ext:'aar')
163    aarImplementation 'com.facebook.soloader:nativeloader:0.10.5'
164    aarImplementation 'com.facebook.fbjni:fbjni-java-only:0.2.2'
165
166    def camerax_version = "1.0.0-alpha05"
167    cameraImplementation "androidx.camera:camera-core:$camerax_version"
168    cameraImplementation "androidx.camera:camera-camera2:$camerax_version"
169    cameraImplementation 'com.google.android.material:material:1.0.0-beta01'
170}
171
172task extractAARForNativeBuild {
173    doLast {
174        configurations.extractForNativeBuild.files.each {
175            def file = it.absoluteFile
176            copy {
177                from zipTree(file)
178                into "$buildDir/$file.name"
179                include "headers/**"
180                include "jni/**"
181            }
182        }
183    }
184}
185
186tasks.whenTaskAdded { task ->
187  if (task.name.contains('externalNativeBuild')) {
188    task.dependsOn(extractAARForNativeBuild)
189  }
190}
191