diff --git a/Cargo.lock b/Cargo.lock
index d09aed2c51..f7df333f52 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -80,6 +80,23 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
+[[package]]
+name = "android_log-sys"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5ecc8056bf6ab9892dcd53216c83d1597487d7dacac16c8df6b877d127df9937"
+
+[[package]]
+name = "android_logger"
+version = "0.14.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "05b07e8e73d720a1f2e4b6014766e6039fd2e96a4fa44e2a78d0e1fa2ff49826"
+dependencies = [
+ "android_log-sys",
+ "env_filter",
+ "log",
+]
+
[[package]]
name = "android_system_properties"
version = "0.1.5"
@@ -911,6 +928,12 @@ dependencies = [
"shlex",
]
+[[package]]
+name = "cesu8"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6d43a04d8753f35258c91f8ec639f792891f748a1edbd759cf1dcea3382ad83c"
+
[[package]]
name = "cfg-expr"
version = "0.15.8"
@@ -1145,6 +1168,16 @@ dependencies = [
"syn 1.0.109",
]
+[[package]]
+name = "combine"
+version = "4.6.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ba5a308b75df32fe02788e748662718f03fde005016435c444eea572398219fd"
+dependencies = [
+ "bytes",
+ "memchr",
+]
+
[[package]]
name = "comfy-table"
version = "7.1.1"
@@ -3191,6 +3224,22 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9028f49264629065d057f340a86acb84867925865f73bbf8d47b4d149a7e88b8"
+[[package]]
+name = "jni"
+version = "0.21.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1a87aa2bb7d2af34197c04845522473242e1aa17c12f4935d5856491a7fb8c97"
+dependencies = [
+ "cesu8",
+ "cfg-if",
+ "combine",
+ "jni-sys",
+ "log",
+ "thiserror",
+ "walkdir",
+ "windows-sys 0.45.0",
+]
+
[[package]]
name = "jni-sys"
version = "0.3.0"
@@ -3568,6 +3617,17 @@ dependencies = [
"serde",
]
+[[package]]
+name = "mnist-inference-android"
+version = "0.15.0"
+dependencies = [
+ "android_logger",
+ "burn",
+ "burn-import",
+ "jni",
+ "log",
+]
+
[[package]]
name = "mnist-inference-web"
version = "0.15.0"
@@ -7320,6 +7380,15 @@ dependencies = [
"windows_x86_64_msvc 0.36.1",
]
+[[package]]
+name = "windows-sys"
+version = "0.45.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0"
+dependencies = [
+ "windows-targets 0.42.2",
+]
+
[[package]]
name = "windows-sys"
version = "0.48.0"
@@ -7347,6 +7416,21 @@ dependencies = [
"windows-targets 0.52.6",
]
+[[package]]
+name = "windows-targets"
+version = "0.42.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071"
+dependencies = [
+ "windows_aarch64_gnullvm 0.42.2",
+ "windows_aarch64_msvc 0.42.2",
+ "windows_i686_gnu 0.42.2",
+ "windows_i686_msvc 0.42.2",
+ "windows_x86_64_gnu 0.42.2",
+ "windows_x86_64_gnullvm 0.42.2",
+ "windows_x86_64_msvc 0.42.2",
+]
+
[[package]]
name = "windows-targets"
version = "0.48.5"
@@ -7378,6 +7462,12 @@ dependencies = [
"windows_x86_64_msvc 0.52.6",
]
+[[package]]
+name = "windows_aarch64_gnullvm"
+version = "0.42.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8"
+
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
@@ -7396,6 +7486,12 @@ version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47"
+[[package]]
+name = "windows_aarch64_msvc"
+version = "0.42.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43"
+
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
@@ -7414,6 +7510,12 @@ version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6"
+[[package]]
+name = "windows_i686_gnu"
+version = "0.42.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f"
+
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
@@ -7438,6 +7540,12 @@ version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024"
+[[package]]
+name = "windows_i686_msvc"
+version = "0.42.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060"
+
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
@@ -7456,6 +7564,12 @@ version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1"
+[[package]]
+name = "windows_x86_64_gnu"
+version = "0.42.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36"
+
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
@@ -7468,6 +7582,12 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
+[[package]]
+name = "windows_x86_64_gnullvm"
+version = "0.42.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3"
+
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
@@ -7486,6 +7606,12 @@ version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680"
+[[package]]
+name = "windows_x86_64_msvc"
+version = "0.42.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0"
+
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
diff --git a/Cargo.toml b/Cargo.toml
index 27c376ecd3..5e15acfff9 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -11,12 +11,14 @@ members = [
"crates/burn-import/onnx-tests",
"examples/*",
"examples/pytorch-import/model",
+ "examples/mnist-inference-android/app/src/main/rust",
"xtask",
]
exclude = [
"examples/notebook",
- "examples/raspberry-pi-pico", # will cause dependency building issues otherwise
+ "examples/mnist-inference-android",
+ "examples/raspberry-pi-pico", # will cause dependency building issues otherwise
# "crates/burn-cuda", # comment this line to work on burn-cuda
]
diff --git a/_typos.toml b/_typos.toml
index 54a3645698..7ea989be1e 100644
--- a/_typos.toml
+++ b/_typos.toml
@@ -6,8 +6,9 @@ extend-exclude = [
"*.onnx",
"assets/ModuleSerialization.xml",
"examples/image-classification-web/src/model/label.txt",
+ "examples/mnist-inference-android/gradle/*",
]
[default.extend-words]
# Don't correct "arange" which is intentional
-arange = "arange"
\ No newline at end of file
+arange = "arange"
diff --git a/examples/mnist-inference-android/.gitignore b/examples/mnist-inference-android/.gitignore
new file mode 100644
index 0000000000..aa724b7707
--- /dev/null
+++ b/examples/mnist-inference-android/.gitignore
@@ -0,0 +1,15 @@
+*.iml
+.gradle
+/local.properties
+/.idea/caches
+/.idea/libraries
+/.idea/modules.xml
+/.idea/workspace.xml
+/.idea/navEditor.xml
+/.idea/assetWizardSettings.xml
+.DS_Store
+/build
+/captures
+.externalNativeBuild
+.cxx
+local.properties
diff --git a/examples/mnist-inference-android/README.md b/examples/mnist-inference-android/README.md
new file mode 100644
index 0000000000..dbdd5b9faa
--- /dev/null
+++ b/examples/mnist-inference-android/README.md
@@ -0,0 +1,105 @@
+# MNIST number detector Android App
+
+This project is a sample Android application that demonstrates how to integrate a `Burn` into an android app using the JNI (Java Native Interface).
+
+## Table of Contents
+
+- [Workflow](#workflow)
+- [Prerequisites](#prerequisites)
+- [Setup](#setup)
+- [How To make your own](#how-to-make-your-own)
+- [License](#license)
+
+## Workflow
+1. **Image Input:** The user provides an image input through the app's interface.
+2. **Image Processing:** The image is converted to a grayscale `byteArray` in Kotlin.
+3. **JNI Bridge:** The grayscale `byteArray` is passed to a Rust function via JNI.
+4. **Rust Processing:** The Rust function calls the `forward` method from the `burn` library, using a pretrained MNIST ONNX model to perform inference.
+5. **Result Handling:** The result, an integer representing the predicted digit, is logged to the android console and returned from Rust to Kotlin.
+6. **Output Display:** The predicted digit is displayed on the screen
+
+## Prerequisites
+- Android Studio (latest version recommended)
+- Rust (installed and configured)
+- Android NDK (Native Development Kit)
+
+## Setup
+1. **Install Rust dependencies:**
+
+ Ensure Rust is installed and the `cargo` command is available:
+
+ ```bash
+ rustup update
+ ```
+ And that you have installed all the rustup toolchains required:
+ ```bash
+ rustup target add \
+ aarch64-linux-android \
+ armv7-linux-androideabi \
+ i686-linux-android \
+ x86_64-linux-android
+ ```
+
+2. **Configure the Android NDK:**
+
+ Ensure that the Android NDK is installed. You can install it via Android Studio's SDK Manager.
+
+3. **Build the android app:**
+
+ Running the android app should automatically build the rust libraries due to the gradle tasks configured at the app level. (More on that later)
+
+
+## How To make your own
+1. There are a few ways to compile a rust library for android -
+ - Add targets in `.cargo/config.toml` and build with them. Then we can add the `.so` files generated to the jni directory in `app/src/main/jniLibs`
+ - Add gradle plugins (like [rust-android-gradle](https://github.com/mozilla/rust-android-gradle) or [cargo-ndk-android](https://github.com/willir/cargo-ndk-android-gradle) using `rust-android-gradle` in this project) to do the work for you, so that the rust library is built on each app build. (Might want to change for expensive library builds)
+2. To interface with Kotlin(Java) you can either use an interface generator (like [flapigen-rs](https://github.com/Dushistov/flapigen-rs)) or make them by yourself. This sample function doesn't use flapigen.
+3. Now the function to be called from android (`infer()` here) needs to follow the [JNI naming conventions](https://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/design.html) (The correct name is also shown in the call error if it doesn't exist).
+4. **Important** The first 2 arguments of the jni interfacing function will be the `env` variable (for interface functions) and the `this` object. The data you pass will start from the 3rd argument.
+5. Next for converting the data from java to rust data types, there are multiple functions in the env variable passed to the function. Use as required...
+6. Then in the app's `build.gradle` we add the part to run the cargo build before building the app and the also the cargo build details:
+```kotlin
+// Cargo build details
+cargo {
+ module = "./src/main/rust" // Or whatever directory contains your Cargo.toml
+ libname = "mnist_inference_android" // Or whatever matches Cargo.toml's [package] name.
+ targets = listOf(
+ "arm", "arm64",
+ "x86",
+ "x86_64"
+ )
+ prebuiltToolchains = true
+}
+
+// Used to build cargo before the android build task is run
+// See more options here: https://github.com/mozilla/rust-android-gradle/issues/133
+project.afterEvaluate {
+ tasks.withType(com.nishtahir.CargoBuildTask::class)
+ .forEach { buildTask ->
+ tasks.withType(com.android.build.gradle.tasks.MergeSourceSetFolders::class)
+ .configureEach {
+ this.inputs.dir(
+ layout.buildDirectory.dir("rustJniLibs" + File.separatorChar + buildTask.toolchain!!.folder)
+ )
+ this.dependsOn(buildTask)
+ }
+ }
+}
+```
+(In the example we have also added the target directory in `config.toml` since otherwise it will build into the workspace target, which we do not want)
+7. Here the library's name is `mnist-android` so we will initialize it in our app:
+```kotlin
+class MainActivity : ComponentActivity() {
+ init {
+ System.loadLibrary("mnist_android") // Note: '-' is changed to '_'
+ }
+ ...
+}
+```
+8. Finally use it by declaring it as an external function first
+```kotlin
+external fun infer(inputImage: ByteArray): Int;
+
+...
+infer(byteArray)
+```
\ No newline at end of file
diff --git a/examples/mnist-inference-android/app/.gitignore b/examples/mnist-inference-android/app/.gitignore
new file mode 100644
index 0000000000..42afabfd2a
--- /dev/null
+++ b/examples/mnist-inference-android/app/.gitignore
@@ -0,0 +1 @@
+/build
\ No newline at end of file
diff --git a/examples/mnist-inference-android/app/build.gradle.kts b/examples/mnist-inference-android/app/build.gradle.kts
new file mode 100644
index 0000000000..33a1f02d98
--- /dev/null
+++ b/examples/mnist-inference-android/app/build.gradle.kts
@@ -0,0 +1,91 @@
+plugins {
+ alias(libs.plugins.android.application)
+ alias(libs.plugins.jetbrains.kotlin.android)
+ alias(libs.plugins.mozilla.rust.android)
+}
+
+android {
+ namespace = "com.example.mnistinferenceandroid"
+ compileSdk = 34
+
+ defaultConfig {
+ applicationId = "com.example.mnistinferenceandroid"
+ minSdk = 24
+ targetSdk = 34
+ versionCode = 1
+ versionName = "1.0"
+
+ testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
+ vectorDrawables {
+ useSupportLibrary = true
+ }
+ }
+
+ buildTypes {
+ release {
+ isMinifyEnabled = false
+ proguardFiles(
+ getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
+ )
+ }
+ }
+ compileOptions {
+ sourceCompatibility = JavaVersion.VERSION_1_8
+ targetCompatibility = JavaVersion.VERSION_1_8
+ }
+ kotlinOptions {
+ jvmTarget = "1.8"
+ }
+ buildFeatures {
+ compose = true
+ }
+ composeOptions {
+ kotlinCompilerExtensionVersion = "1.5.1"
+ }
+ packaging {
+ resources {
+ excludes += "/META-INF/{AL2.0,LGPL2.1}"
+ }
+ }
+ ndkVersion = "27.0.12077973"
+}
+
+dependencies {
+ implementation(libs.androidx.core.ktx)
+ implementation(libs.androidx.lifecycle.runtime.ktx)
+ implementation(libs.androidx.activity.compose)
+ implementation(platform(libs.androidx.compose.bom))
+ implementation(libs.androidx.ui)
+ implementation(libs.androidx.ui.graphics)
+ implementation(libs.androidx.ui.tooling.preview)
+ implementation(libs.androidx.material3)
+ testImplementation(libs.junit)
+ androidTestImplementation(libs.androidx.junit)
+ androidTestImplementation(libs.androidx.espresso.core)
+ androidTestImplementation(platform(libs.androidx.compose.bom))
+ androidTestImplementation(libs.androidx.ui.test.junit4)
+ debugImplementation(libs.androidx.ui.tooling)
+ debugImplementation(libs.androidx.ui.test.manifest)
+}
+
+cargo {
+ module = "./src/main/rust" // Or whatever directory contains your Cargo.toml
+ libname = "mnist_inference_android" // Or whatever matches Cargo.toml's [package] name.
+ targets = listOf(
+ "arm", "arm64", "x86", "x86_64"
+ )
+ prebuiltToolchains = true
+}
+
+// Used to build cargo before the android build task is run
+project.afterEvaluate {
+ tasks.withType(com.nishtahir.CargoBuildTask::class).forEach { buildTask ->
+ tasks.withType(com.android.build.gradle.tasks.MergeSourceSetFolders::class)
+ .configureEach {
+ this.inputs.dir(
+ layout.buildDirectory.dir("rustJniLibs" + File.separatorChar + buildTask.toolchain!!.folder)
+ )
+ this.dependsOn(buildTask)
+ }
+ }
+}
\ No newline at end of file
diff --git a/examples/mnist-inference-android/app/proguard-rules.pro b/examples/mnist-inference-android/app/proguard-rules.pro
new file mode 100644
index 0000000000..481bb43481
--- /dev/null
+++ b/examples/mnist-inference-android/app/proguard-rules.pro
@@ -0,0 +1,21 @@
+# Add project specific ProGuard rules here.
+# You can control the set of applied configuration files using the
+# proguardFiles setting in build.gradle.
+#
+# For more details, see
+# http://developer.android.com/guide/developing/tools/proguard.html
+
+# If your project uses WebView with JS, uncomment the following
+# and specify the fully qualified class name to the JavaScript interface
+# class:
+#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
+# public *;
+#}
+
+# Uncomment this to preserve the line number information for
+# debugging stack traces.
+#-keepattributes SourceFile,LineNumberTable
+
+# If you keep the line number information, uncomment this to
+# hide the original source file name.
+#-renamesourcefileattribute SourceFile
\ No newline at end of file
diff --git a/examples/mnist-inference-android/app/src/androidTest/java/com/example/mnistinferenceandroid/ExampleInstrumentedTest.kt b/examples/mnist-inference-android/app/src/androidTest/java/com/example/mnistinferenceandroid/ExampleInstrumentedTest.kt
new file mode 100644
index 0000000000..44f832b984
--- /dev/null
+++ b/examples/mnist-inference-android/app/src/androidTest/java/com/example/mnistinferenceandroid/ExampleInstrumentedTest.kt
@@ -0,0 +1,24 @@
+package com.example.mnistinferenceandroid
+
+import androidx.test.platform.app.InstrumentationRegistry
+import androidx.test.ext.junit.runners.AndroidJUnit4
+
+import org.junit.Test
+import org.junit.runner.RunWith
+
+import org.junit.Assert.*
+
+/**
+ * Instrumented test, which will execute on an Android device.
+ *
+ * See [testing documentation](http://d.android.com/tools/testing).
+ */
+@RunWith(AndroidJUnit4::class)
+class ExampleInstrumentedTest {
+ @Test
+ fun useAppContext() {
+ // Context of the app under test.
+ val appContext = InstrumentationRegistry.getInstrumentation().targetContext
+ assertEquals("com.example.mnistinferenceandroid", appContext.packageName)
+ }
+}
\ No newline at end of file
diff --git a/examples/mnist-inference-android/app/src/main/AndroidManifest.xml b/examples/mnist-inference-android/app/src/main/AndroidManifest.xml
new file mode 100644
index 0000000000..8405077d8b
--- /dev/null
+++ b/examples/mnist-inference-android/app/src/main/AndroidManifest.xml
@@ -0,0 +1,28 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/MainActivity.kt b/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/MainActivity.kt
new file mode 100644
index 0000000000..a4f5c52404
--- /dev/null
+++ b/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/MainActivity.kt
@@ -0,0 +1,28 @@
+package com.example.mnistinferenceandroid
+
+import android.os.Bundle
+import androidx.activity.ComponentActivity
+import androidx.activity.compose.setContent
+import androidx.activity.enableEdgeToEdge
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.material3.Scaffold
+import androidx.compose.ui.Modifier
+import com.example.mnistinferenceandroid.ui.theme.MnistInferenceAndroidTheme
+
+class MainActivity : ComponentActivity() {
+ init {
+ System.loadLibrary("mnist_inference_android")
+ }
+
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+ enableEdgeToEdge()
+ setContent {
+ MnistInferenceAndroidTheme {
+ Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
+ MnistRecognizePage()
+ }
+ }
+ }
+ }
+}
diff --git a/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/MnistInferPage.kt b/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/MnistInferPage.kt
new file mode 100644
index 0000000000..8c1a6faaac
--- /dev/null
+++ b/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/MnistInferPage.kt
@@ -0,0 +1,53 @@
+package com.example.mnistinferenceandroid
+
+import android.net.Uri
+import androidx.activity.compose.rememberLauncherForActivityResult
+import androidx.activity.result.contract.ActivityResultContracts
+import androidx.compose.foundation.layout.*
+import androidx.compose.material3.Button
+import androidx.compose.material3.Text
+import androidx.compose.runtime.*
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.platform.LocalContext
+import androidx.compose.ui.unit.dp
+
+external fun infer(inputImage: ByteArray): Int;
+@Composable
+fun MnistRecognizePage() {
+ var result by remember { mutableStateOf(null) }
+ val context = LocalContext.current
+
+ // Launcher to pick an image from the file explorer
+ val pickImageLauncher = rememberLauncherForActivityResult(
+ contract = ActivityResultContracts.GetContent()
+ ) { uri: Uri? ->
+ uri?.let {
+ val byteArray = uriToByteArray(context, it);
+ if(byteArray != null){
+ result = infer(byteArray)
+ }
+ }
+ }
+
+ Column(
+ modifier = Modifier
+ .fillMaxSize()
+ .padding(16.dp),
+ horizontalAlignment = Alignment.CenterHorizontally,
+ verticalArrangement = Arrangement.Center
+ ) {
+ Button(onClick = { pickImageLauncher.launch("image/*") }) {
+ Text("Select Image")
+ }
+
+ Spacer(modifier = Modifier.height(16.dp))
+
+ result?.let {
+ if (it == -1)
+ Text(text = "Model prediction error")
+ else
+ Text("The number is: $it")
+ }
+ }
+}
\ No newline at end of file
diff --git a/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/ui/theme/Color.kt b/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/ui/theme/Color.kt
new file mode 100644
index 0000000000..a0bca2a41d
--- /dev/null
+++ b/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/ui/theme/Color.kt
@@ -0,0 +1,11 @@
+package com.example.mnistinferenceandroid.ui.theme
+
+import androidx.compose.ui.graphics.Color
+
+val Purple80 = Color(0xFFD0BCFF)
+val PurpleGrey80 = Color(0xFFCCC2DC)
+val Pink80 = Color(0xFFEFB8C8)
+
+val Purple40 = Color(0xFF6650a4)
+val PurpleGrey40 = Color(0xFF625b71)
+val Pink40 = Color(0xFF7D5260)
\ No newline at end of file
diff --git a/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/ui/theme/Theme.kt b/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/ui/theme/Theme.kt
new file mode 100644
index 0000000000..305550545d
--- /dev/null
+++ b/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/ui/theme/Theme.kt
@@ -0,0 +1,58 @@
+package com.example.mnistinferenceandroid.ui.theme
+
+import android.app.Activity
+import android.os.Build
+import androidx.compose.foundation.isSystemInDarkTheme
+import androidx.compose.material3.MaterialTheme
+import androidx.compose.material3.darkColorScheme
+import androidx.compose.material3.dynamicDarkColorScheme
+import androidx.compose.material3.dynamicLightColorScheme
+import androidx.compose.material3.lightColorScheme
+import androidx.compose.runtime.Composable
+import androidx.compose.ui.platform.LocalContext
+
+private val DarkColorScheme = darkColorScheme(
+ primary = Purple80,
+ secondary = PurpleGrey80,
+ tertiary = Pink80
+)
+
+private val LightColorScheme = lightColorScheme(
+ primary = Purple40,
+ secondary = PurpleGrey40,
+ tertiary = Pink40
+
+ /* Other default colors to override
+ background = Color(0xFFFFFBFE),
+ surface = Color(0xFFFFFBFE),
+ onPrimary = Color.White,
+ onSecondary = Color.White,
+ onTertiary = Color.White,
+ onBackground = Color(0xFF1C1B1F),
+ onSurface = Color(0xFF1C1B1F),
+ */
+)
+
+@Composable
+fun MnistInferenceAndroidTheme(
+ darkTheme: Boolean = isSystemInDarkTheme(),
+ // Dynamic color is available on Android 12+
+ dynamicColor: Boolean = true,
+ content: @Composable () -> Unit
+) {
+ val colorScheme = when {
+ dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
+ val context = LocalContext.current
+ if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
+ }
+
+ darkTheme -> DarkColorScheme
+ else -> LightColorScheme
+ }
+
+ MaterialTheme(
+ colorScheme = colorScheme,
+ typography = Typography,
+ content = content
+ )
+}
\ No newline at end of file
diff --git a/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/ui/theme/Type.kt b/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/ui/theme/Type.kt
new file mode 100644
index 0000000000..b906ab59b1
--- /dev/null
+++ b/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/ui/theme/Type.kt
@@ -0,0 +1,17 @@
+package com.example.mnistinferenceandroid.ui.theme
+
+import androidx.compose.material3.Typography
+import androidx.compose.ui.text.TextStyle
+import androidx.compose.ui.text.font.FontFamily
+import androidx.compose.ui.text.font.FontWeight
+import androidx.compose.ui.unit.sp
+
+val Typography = Typography(
+ bodyLarge = TextStyle(
+ fontFamily = FontFamily.Default,
+ fontWeight = FontWeight.Normal,
+ fontSize = 16.sp,
+ lineHeight = 24.sp,
+ letterSpacing = 0.5.sp
+ )
+)
\ No newline at end of file
diff --git a/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/utils.kt b/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/utils.kt
new file mode 100644
index 0000000000..ed4dde1c28
--- /dev/null
+++ b/examples/mnist-inference-android/app/src/main/java/com/example/mnistinferenceandroid/utils.kt
@@ -0,0 +1,42 @@
+package com.example.mnistinferenceandroid
+
+import android.content.Context
+import android.graphics.Bitmap
+import android.graphics.BitmapFactory
+import android.graphics.Color
+import android.net.Uri
+
+fun uriToByteArray(context: Context, uri: Uri): ByteArray? {
+ val inputStream = context.contentResolver.openInputStream(uri) ?: return null
+ val byteArray = inputStream.readBytes()
+ val imageMap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size)
+
+ // The model takes 28x28 images as input so reduce size before grayscale conversion
+ val reducedMap = Bitmap.createScaledBitmap(imageMap, 28, 28, false)
+
+ val pixelArray = convertToGrayscaleArray(reducedMap)
+ return pixelArray
+}
+
+fun convertToGrayscaleArray(bmp: Bitmap): ByteArray {
+ // Create a mutable bitmap with the same dimensions as the original
+ val width = bmp.width
+ val height = bmp.height
+
+ val grayscaleArray = ByteArray(width * height)
+ // Iterate over each pixel in the original bitmap
+ for (y in 0 until height) {
+ for (x in 0 until width) {
+ // Get the pixel color at (x, y)
+ val pixel = bmp.getPixel(x, y)
+
+ val r = Color.red(pixel)
+ val g = Color.green(pixel)
+ val b = Color.blue(pixel)
+ // Converting to grayscale using the NTSC formula
+ val gray = (0.299 * r + 0.587 * g + 0.114 * b).toInt()
+ grayscaleArray[x + y * width] = gray.toByte() // Can also use the int array directly
+ }
+ }
+ return grayscaleArray
+}
diff --git a/examples/mnist-inference-android/app/src/main/res/drawable/ic_launcher_background.xml b/examples/mnist-inference-android/app/src/main/res/drawable/ic_launcher_background.xml
new file mode 100644
index 0000000000..07d5da9cbf
--- /dev/null
+++ b/examples/mnist-inference-android/app/src/main/res/drawable/ic_launcher_background.xml
@@ -0,0 +1,170 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/mnist-inference-android/app/src/main/res/drawable/ic_launcher_foreground.xml b/examples/mnist-inference-android/app/src/main/res/drawable/ic_launcher_foreground.xml
new file mode 100644
index 0000000000..2b068d1146
--- /dev/null
+++ b/examples/mnist-inference-android/app/src/main/res/drawable/ic_launcher_foreground.xml
@@ -0,0 +1,30 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/examples/mnist-inference-android/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/examples/mnist-inference-android/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml
new file mode 100644
index 0000000000..6f3b755bf5
--- /dev/null
+++ b/examples/mnist-inference-android/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/examples/mnist-inference-android/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml b/examples/mnist-inference-android/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml
new file mode 100644
index 0000000000..6f3b755bf5
--- /dev/null
+++ b/examples/mnist-inference-android/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/examples/mnist-inference-android/app/src/main/res/mipmap-hdpi/ic_launcher.webp b/examples/mnist-inference-android/app/src/main/res/mipmap-hdpi/ic_launcher.webp
new file mode 100644
index 0000000000..c209e78ecd
Binary files /dev/null and b/examples/mnist-inference-android/app/src/main/res/mipmap-hdpi/ic_launcher.webp differ
diff --git a/examples/mnist-inference-android/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp b/examples/mnist-inference-android/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp
new file mode 100644
index 0000000000..b2dfe3d1ba
Binary files /dev/null and b/examples/mnist-inference-android/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp differ
diff --git a/examples/mnist-inference-android/app/src/main/res/mipmap-mdpi/ic_launcher.webp b/examples/mnist-inference-android/app/src/main/res/mipmap-mdpi/ic_launcher.webp
new file mode 100644
index 0000000000..4f0f1d64e5
Binary files /dev/null and b/examples/mnist-inference-android/app/src/main/res/mipmap-mdpi/ic_launcher.webp differ
diff --git a/examples/mnist-inference-android/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp b/examples/mnist-inference-android/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp
new file mode 100644
index 0000000000..62b611da08
Binary files /dev/null and b/examples/mnist-inference-android/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp differ
diff --git a/examples/mnist-inference-android/app/src/main/res/mipmap-xhdpi/ic_launcher.webp b/examples/mnist-inference-android/app/src/main/res/mipmap-xhdpi/ic_launcher.webp
new file mode 100644
index 0000000000..948a3070fe
Binary files /dev/null and b/examples/mnist-inference-android/app/src/main/res/mipmap-xhdpi/ic_launcher.webp differ
diff --git a/examples/mnist-inference-android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp b/examples/mnist-inference-android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp
new file mode 100644
index 0000000000..1b9a6956b3
Binary files /dev/null and b/examples/mnist-inference-android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp differ
diff --git a/examples/mnist-inference-android/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp b/examples/mnist-inference-android/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp
new file mode 100644
index 0000000000..28d4b77f9f
Binary files /dev/null and b/examples/mnist-inference-android/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp differ
diff --git a/examples/mnist-inference-android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp b/examples/mnist-inference-android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp
new file mode 100644
index 0000000000..9287f50836
Binary files /dev/null and b/examples/mnist-inference-android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp differ
diff --git a/examples/mnist-inference-android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp b/examples/mnist-inference-android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp
new file mode 100644
index 0000000000..aa7d6427e6
Binary files /dev/null and b/examples/mnist-inference-android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp differ
diff --git a/examples/mnist-inference-android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp b/examples/mnist-inference-android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp
new file mode 100644
index 0000000000..9126ae37cb
Binary files /dev/null and b/examples/mnist-inference-android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp differ
diff --git a/examples/mnist-inference-android/app/src/main/res/values/colors.xml b/examples/mnist-inference-android/app/src/main/res/values/colors.xml
new file mode 100644
index 0000000000..f8c6127d32
--- /dev/null
+++ b/examples/mnist-inference-android/app/src/main/res/values/colors.xml
@@ -0,0 +1,10 @@
+
+
+ #FFBB86FC
+ #FF6200EE
+ #FF3700B3
+ #FF03DAC5
+ #FF018786
+ #FF000000
+ #FFFFFFFF
+
\ No newline at end of file
diff --git a/examples/mnist-inference-android/app/src/main/res/values/strings.xml b/examples/mnist-inference-android/app/src/main/res/values/strings.xml
new file mode 100644
index 0000000000..1270390b3b
--- /dev/null
+++ b/examples/mnist-inference-android/app/src/main/res/values/strings.xml
@@ -0,0 +1,3 @@
+
+ mnist inference android
+
\ No newline at end of file
diff --git a/examples/mnist-inference-android/app/src/main/res/values/themes.xml b/examples/mnist-inference-android/app/src/main/res/values/themes.xml
new file mode 100644
index 0000000000..e64bae40c7
--- /dev/null
+++ b/examples/mnist-inference-android/app/src/main/res/values/themes.xml
@@ -0,0 +1,5 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/examples/mnist-inference-android/app/src/main/res/xml/backup_rules.xml b/examples/mnist-inference-android/app/src/main/res/xml/backup_rules.xml
new file mode 100644
index 0000000000..fa0f996d2c
--- /dev/null
+++ b/examples/mnist-inference-android/app/src/main/res/xml/backup_rules.xml
@@ -0,0 +1,13 @@
+
+
+
+
\ No newline at end of file
diff --git a/examples/mnist-inference-android/app/src/main/res/xml/data_extraction_rules.xml b/examples/mnist-inference-android/app/src/main/res/xml/data_extraction_rules.xml
new file mode 100644
index 0000000000..9ee9997b0b
--- /dev/null
+++ b/examples/mnist-inference-android/app/src/main/res/xml/data_extraction_rules.xml
@@ -0,0 +1,19 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/examples/mnist-inference-android/app/src/main/rust/.cargo/config.toml b/examples/mnist-inference-android/app/src/main/rust/.cargo/config.toml
new file mode 100644
index 0000000000..686b4df4ea
--- /dev/null
+++ b/examples/mnist-inference-android/app/src/main/rust/.cargo/config.toml
@@ -0,0 +1,16 @@
+# Add your toolchain path if you want to build without using android studio
+# [target.aarch64-linux-android]
+# linker = "/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android24-clang"
+
+# [target.armv7-linux-androideabi]
+# linker = "/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi24-clang"
+
+# [target.i686-linux-android]
+# linker = "/toolchains/llvm/prebuilt/linux-x86_64/bin/i686-linux-android24-clang"
+
+# [target.x86_64-linux-android]
+# linker = "/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-linux-android24-clang"
+
+# We need the target to be in the current directory, not the workspace parent directory
+[build]
+target-dir = "./target"
diff --git a/examples/mnist-inference-android/app/src/main/rust/Cargo.toml b/examples/mnist-inference-android/app/src/main/rust/Cargo.toml
new file mode 100644
index 0000000000..d39c884aac
--- /dev/null
+++ b/examples/mnist-inference-android/app/src/main/rust/Cargo.toml
@@ -0,0 +1,25 @@
+[package]
+authors = ["Shashank (@scramjet911)"]
+name = "mnist-inference-android"
+edition.workspace = true
+license = "MIT OR Apache-2.0"
+publish = false
+version.workspace = true
+
+[lib]
+crate-type = ["cdylib"]
+
+[features]
+default = ["ndarray"]
+
+ndarray = ["burn/ndarray"]
+wgpu = ["burn/wgpu"]
+
+[dependencies]
+burn = { path = "../../../../../../crates/burn", features = ["ndarray"] }
+jni = "0.21.1"
+android_logger = "0.14.1"
+log = "0.4"
+
+[build-dependencies]
+burn-import = { path = "../../../../../../crates/burn-import" }
diff --git a/examples/mnist-inference-android/app/src/main/rust/build.rs b/examples/mnist-inference-android/app/src/main/rust/build.rs
new file mode 100644
index 0000000000..db1a539867
--- /dev/null
+++ b/examples/mnist-inference-android/app/src/main/rust/build.rs
@@ -0,0 +1,11 @@
+use burn_import::onnx::{ModelGen, RecordType};
+
+fn main() {
+ // If the embedded-model, then model is bundled into the binary.
+ ModelGen::new()
+ .input("src/model/mnist.onnx")
+ .out_dir("model/")
+ .record_type(RecordType::Bincode)
+ .embed_states(true)
+ .run_from_script();
+}
diff --git a/examples/mnist-inference-android/app/src/main/rust/buildScript.sh b/examples/mnist-inference-android/app/src/main/rust/buildScript.sh
new file mode 100755
index 0000000000..8b32c5ebf5
--- /dev/null
+++ b/examples/mnist-inference-android/app/src/main/rust/buildScript.sh
@@ -0,0 +1,18 @@
+#!/bin/zsh
+
+# Script, if you are building and moving the jni lib's manually
+cargo build --target aarch64-linux-android --release
+cargo build --target armv7-linux-androideabi --release
+cargo build --target i686-linux-android --release
+cargo build --target x86_64-linux-android --release
+
+rm -rf ../jniLibs
+mkdir -p ../jniLibs/arm64-v8a
+mkdir ../jniLibs/armeabi-v7a
+mkdir ../jniLibs/x86
+mkdir ../jniLibs/x86_64
+
+cp ./target/aarch64-linux-android/release/libmnist_inference_android.so ../jniLibs/arm64-v8a/libmnist_inference_android.so
+cp ./target/armv7-linux-androideabi/release/libmnist_inference_android.so ../jniLibs/armeabi-v7a/libmnist_inference_android.so
+cp ./target/i686-linux-android/release/libmnist_inference_android.so ../jniLibs/x86/libmnist_inference_android.so
+cp ./target/x86_64-linux-android/release/libmnist_inference_android.so ../jniLibs/x86_64/libmnist_inference_android.so
diff --git a/examples/mnist-inference-android/app/src/main/rust/src/lib.rs b/examples/mnist-inference-android/app/src/main/rust/src/lib.rs
new file mode 100644
index 0000000000..da27ea638a
--- /dev/null
+++ b/examples/mnist-inference-android/app/src/main/rust/src/lib.rs
@@ -0,0 +1,67 @@
+#![allow(clippy::new_without_default)]
+#![allow(non_snake_case)]
+#[macro_use]
+extern crate log;
+extern crate android_logger;
+
+use android_logger::Config;
+use log::LevelFilter;
+
+pub mod model;
+
+use jni::{
+ objects::JByteArray,
+ sys::{jint, jobject},
+ JNIEnv,
+};
+
+use burn::{backend::ndarray::NdArray, tensor::Tensor};
+
+use crate::model::mnist::Model;
+
+#[no_mangle]
+pub extern "C" fn Java_com_example_mnistinferenceandroid_MnistInferPageKt_infer(
+ env: JNIEnv,
+ _: jobject,
+ inputImage: JByteArray,
+) -> jint {
+ // Used to log to the android device's console since regular print statements won't show there, we use the android_logger package
+ android_logger::init_once(Config::default().with_max_level(LevelFilter::Trace));
+
+ let input = env
+ .convert_byte_array(&inputImage)
+ .expect("Error converting byteArray to Int vectors")
+ .into_iter()
+ .map(f32::from)
+ .collect::>();
+
+ // Just a POC, usually this would be done in an init function and not in each call
+ type Backend = NdArray;
+ let device = ::Device::default();
+
+ let model: Model = Model::default();
+
+ // Reshape from the 1D array to 3d tensor [batch, height, width]
+ let input =
+ Tensor::::from_floats(input.as_slice(), &device).reshape([1, 1, 28, 28]);
+
+ // Normalize input: make between [0,1] and make the mean=0 and std=1
+ // values mean=0.1307,std=0.3081 were copied from Pytorch Mist Example
+ // https://github.com/pytorch/examples/blob/54f4572509891883a947411fd7239237dd2a39c3/mnist/main.py#L122
+ let input = ((input / 255) - 0.1307) / 0.3081;
+
+ // Run the tensor input through the model
+ let output: Tensor = model.forward(input);
+
+ let res = match i32::try_from(output.argmax(1).into_scalar()) {
+ Ok(val) => {
+ debug!("The number is: {}", val);
+ val
+ }
+ Err(_) => {
+ debug!("Model output error!");
+ -1
+ }
+ };
+ res
+}
diff --git a/examples/mnist-inference-android/app/src/main/rust/src/model/mnist.onnx b/examples/mnist-inference-android/app/src/main/rust/src/model/mnist.onnx
new file mode 100644
index 0000000000..1384a37ba2
Binary files /dev/null and b/examples/mnist-inference-android/app/src/main/rust/src/model/mnist.onnx differ
diff --git a/examples/mnist-inference-android/app/src/main/rust/src/model/mod.rs b/examples/mnist-inference-android/app/src/main/rust/src/model/mod.rs
new file mode 100644
index 0000000000..4c821cafd4
--- /dev/null
+++ b/examples/mnist-inference-android/app/src/main/rust/src/model/mod.rs
@@ -0,0 +1,3 @@
+pub mod mnist {
+ include!(concat!(env!("OUT_DIR"), "/model/mnist.rs"));
+}
diff --git a/examples/mnist-inference-android/app/src/test/java/com/example/mnistinferenceandroid/ExampleUnitTest.kt b/examples/mnist-inference-android/app/src/test/java/com/example/mnistinferenceandroid/ExampleUnitTest.kt
new file mode 100644
index 0000000000..1b2e921e29
--- /dev/null
+++ b/examples/mnist-inference-android/app/src/test/java/com/example/mnistinferenceandroid/ExampleUnitTest.kt
@@ -0,0 +1,17 @@
+package com.example.mnistinferenceandroid
+
+import org.junit.Test
+
+import org.junit.Assert.*
+
+/**
+ * Example local unit test, which will execute on the development machine (host).
+ *
+ * See [testing documentation](http://d.android.com/tools/testing).
+ */
+class ExampleUnitTest {
+ @Test
+ fun addition_isCorrect() {
+ assertEquals(4, 2 + 2)
+ }
+}
\ No newline at end of file
diff --git a/examples/mnist-inference-android/build.gradle.kts b/examples/mnist-inference-android/build.gradle.kts
new file mode 100644
index 0000000000..d95aa8b4a3
--- /dev/null
+++ b/examples/mnist-inference-android/build.gradle.kts
@@ -0,0 +1,6 @@
+// Top-level build file where you can add configuration options common to all sub-projects/modules.
+plugins {
+ alias(libs.plugins.android.application) apply false
+ alias(libs.plugins.jetbrains.kotlin.android) apply false
+ alias(libs.plugins.mozilla.rust.android) apply false
+}
\ No newline at end of file
diff --git a/examples/mnist-inference-android/gradle.properties b/examples/mnist-inference-android/gradle.properties
new file mode 100644
index 0000000000..20e2a01520
--- /dev/null
+++ b/examples/mnist-inference-android/gradle.properties
@@ -0,0 +1,23 @@
+# Project-wide Gradle settings.
+# IDE (e.g. Android Studio) users:
+# Gradle settings configured through the IDE *will override*
+# any settings specified in this file.
+# For more details on how to configure your build environment visit
+# http://www.gradle.org/docs/current/userguide/build_environment.html
+# Specifies the JVM arguments used for the daemon process.
+# The setting is particularly useful for tweaking memory settings.
+org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
+# When configured, Gradle will run in incubating parallel mode.
+# This option should only be used with decoupled projects. For more details, visit
+# https://developer.android.com/r/tools/gradle-multi-project-decoupled-projects
+# org.gradle.parallel=true
+# AndroidX package structure to make it clearer which packages are bundled with the
+# Android operating system, and which are packaged with your app's APK
+# https://developer.android.com/topic/libraries/support-library/androidx-rn
+android.useAndroidX=true
+# Kotlin code style for this project: "official" or "obsolete":
+kotlin.code.style=official
+# Enables namespacing of each library's R class so that its R class includes only the
+# resources declared in the library itself and none from the library's dependencies,
+# thereby reducing the size of the R class for that library
+android.nonTransitiveRClass=true
\ No newline at end of file
diff --git a/examples/mnist-inference-android/gradle/libs.versions.toml b/examples/mnist-inference-android/gradle/libs.versions.toml
new file mode 100644
index 0000000000..e3a45cec81
--- /dev/null
+++ b/examples/mnist-inference-android/gradle/libs.versions.toml
@@ -0,0 +1,33 @@
+[versions]
+agp = "8.5.2"
+kotlin = "1.9.0"
+coreKtx = "1.13.1"
+junit = "4.13.2"
+junitVersion = "1.2.1"
+espressoCore = "3.6.1"
+lifecycleRuntimeKtx = "2.8.4"
+activityCompose = "1.9.1"
+composeBom = "2024.04.01"
+rustAndroidGradle = "0.9.4"
+
+[libraries]
+androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
+junit = { group = "junit", name = "junit", version.ref = "junit" }
+androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
+androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
+androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
+androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
+androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
+androidx-ui = { group = "androidx.compose.ui", name = "ui" }
+androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }
+androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
+androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
+androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
+androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
+androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
+
+[plugins]
+android-application = { id = "com.android.application", version.ref = "agp" }
+jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
+mozilla-rust-android = { id = "org.mozilla.rust-android-gradle.rust-android", version.ref = "rustAndroidGradle" }
+
diff --git a/examples/mnist-inference-android/gradle/wrapper/gradle-wrapper.jar b/examples/mnist-inference-android/gradle/wrapper/gradle-wrapper.jar
new file mode 100644
index 0000000000..e708b1c023
Binary files /dev/null and b/examples/mnist-inference-android/gradle/wrapper/gradle-wrapper.jar differ
diff --git a/examples/mnist-inference-android/gradle/wrapper/gradle-wrapper.properties b/examples/mnist-inference-android/gradle/wrapper/gradle-wrapper.properties
new file mode 100644
index 0000000000..aca6e74a4c
--- /dev/null
+++ b/examples/mnist-inference-android/gradle/wrapper/gradle-wrapper.properties
@@ -0,0 +1,6 @@
+#Fri Aug 30 11:24:08 IST 2024
+distributionBase=GRADLE_USER_HOME
+distributionPath=wrapper/dists
+distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
+zipStoreBase=GRADLE_USER_HOME
+zipStorePath=wrapper/dists
diff --git a/examples/mnist-inference-android/gradlew b/examples/mnist-inference-android/gradlew
new file mode 100755
index 0000000000..4f906e0c81
--- /dev/null
+++ b/examples/mnist-inference-android/gradlew
@@ -0,0 +1,185 @@
+#!/usr/bin/env sh
+
+#
+# Copyright 2015 the original author or authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+##############################################################################
+##
+## Gradle start up script for UN*X
+##
+##############################################################################
+
+# Attempt to set APP_HOME
+# Resolve links: $0 may be a link
+PRG="$0"
+# Need this for relative symlinks.
+while [ -h "$PRG" ] ; do
+ ls=`ls -ld "$PRG"`
+ link=`expr "$ls" : '.*-> \(.*\)$'`
+ if expr "$link" : '/.*' > /dev/null; then
+ PRG="$link"
+ else
+ PRG=`dirname "$PRG"`"/$link"
+ fi
+done
+SAVED="`pwd`"
+cd "`dirname \"$PRG\"`/" >/dev/null
+APP_HOME="`pwd -P`"
+cd "$SAVED" >/dev/null
+
+APP_NAME="Gradle"
+APP_BASE_NAME=`basename "$0"`
+
+# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
+
+# Use the maximum available, or set MAX_FD != -1 to use that value.
+MAX_FD="maximum"
+
+warn () {
+ echo "$*"
+}
+
+die () {
+ echo
+ echo "$*"
+ echo
+ exit 1
+}
+
+# OS specific support (must be 'true' or 'false').
+cygwin=false
+msys=false
+darwin=false
+nonstop=false
+case "`uname`" in
+ CYGWIN* )
+ cygwin=true
+ ;;
+ Darwin* )
+ darwin=true
+ ;;
+ MINGW* )
+ msys=true
+ ;;
+ NONSTOP* )
+ nonstop=true
+ ;;
+esac
+
+CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
+
+
+# Determine the Java command to use to start the JVM.
+if [ -n "$JAVA_HOME" ] ; then
+ if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
+ # IBM's JDK on AIX uses strange locations for the executables
+ JAVACMD="$JAVA_HOME/jre/sh/java"
+ else
+ JAVACMD="$JAVA_HOME/bin/java"
+ fi
+ if [ ! -x "$JAVACMD" ] ; then
+ die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+ fi
+else
+ JAVACMD="java"
+ which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+fi
+
+# Increase the maximum file descriptors if we can.
+if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
+ MAX_FD_LIMIT=`ulimit -H -n`
+ if [ $? -eq 0 ] ; then
+ if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
+ MAX_FD="$MAX_FD_LIMIT"
+ fi
+ ulimit -n $MAX_FD
+ if [ $? -ne 0 ] ; then
+ warn "Could not set maximum file descriptor limit: $MAX_FD"
+ fi
+ else
+ warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
+ fi
+fi
+
+# For Darwin, add options to specify how the application appears in the dock
+if $darwin; then
+ GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
+fi
+
+# For Cygwin or MSYS, switch paths to Windows format before running java
+if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
+ APP_HOME=`cygpath --path --mixed "$APP_HOME"`
+ CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
+
+ JAVACMD=`cygpath --unix "$JAVACMD"`
+
+ # We build the pattern for arguments to be converted via cygpath
+ ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
+ SEP=""
+ for dir in $ROOTDIRSRAW ; do
+ ROOTDIRS="$ROOTDIRS$SEP$dir"
+ SEP="|"
+ done
+ OURCYGPATTERN="(^($ROOTDIRS))"
+ # Add a user-defined pattern to the cygpath arguments
+ if [ "$GRADLE_CYGPATTERN" != "" ] ; then
+ OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
+ fi
+ # Now convert the arguments - kludge to limit ourselves to /bin/sh
+ i=0
+ for arg in "$@" ; do
+ CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
+ CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
+
+ if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
+ eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
+ else
+ eval `echo args$i`="\"$arg\""
+ fi
+ i=`expr $i + 1`
+ done
+ case $i in
+ 0) set -- ;;
+ 1) set -- "$args0" ;;
+ 2) set -- "$args0" "$args1" ;;
+ 3) set -- "$args0" "$args1" "$args2" ;;
+ 4) set -- "$args0" "$args1" "$args2" "$args3" ;;
+ 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
+ 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
+ 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
+ 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
+ 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
+ esac
+fi
+
+# Escape application args
+save () {
+ for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
+ echo " "
+}
+APP_ARGS=`save "$@"`
+
+# Collect all arguments for the java command, following the shell quoting and substitution rules
+eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
+
+exec "$JAVACMD" "$@"
diff --git a/examples/mnist-inference-android/gradlew.bat b/examples/mnist-inference-android/gradlew.bat
new file mode 100644
index 0000000000..ac1b06f938
--- /dev/null
+++ b/examples/mnist-inference-android/gradlew.bat
@@ -0,0 +1,89 @@
+@rem
+@rem Copyright 2015 the original author or authors.
+@rem
+@rem Licensed under the Apache License, Version 2.0 (the "License");
+@rem you may not use this file except in compliance with the License.
+@rem You may obtain a copy of the License at
+@rem
+@rem https://www.apache.org/licenses/LICENSE-2.0
+@rem
+@rem Unless required by applicable law or agreed to in writing, software
+@rem distributed under the License is distributed on an "AS IS" BASIS,
+@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+@rem See the License for the specific language governing permissions and
+@rem limitations under the License.
+@rem
+
+@if "%DEBUG%" == "" @echo off
+@rem ##########################################################################
+@rem
+@rem Gradle startup script for Windows
+@rem
+@rem ##########################################################################
+
+@rem Set local scope for the variables with windows NT shell
+if "%OS%"=="Windows_NT" setlocal
+
+set DIRNAME=%~dp0
+if "%DIRNAME%" == "" set DIRNAME=.
+set APP_BASE_NAME=%~n0
+set APP_HOME=%DIRNAME%
+
+@rem Resolve any "." and ".." in APP_HOME to make it shorter.
+for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
+
+@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
+
+@rem Find java.exe
+if defined JAVA_HOME goto findJavaFromJavaHome
+
+set JAVA_EXE=java.exe
+%JAVA_EXE% -version >NUL 2>&1
+if "%ERRORLEVEL%" == "0" goto execute
+
+echo.
+echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:findJavaFromJavaHome
+set JAVA_HOME=%JAVA_HOME:"=%
+set JAVA_EXE=%JAVA_HOME%/bin/java.exe
+
+if exist "%JAVA_EXE%" goto execute
+
+echo.
+echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:execute
+@rem Setup the command line
+
+set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
+
+
+@rem Execute Gradle
+"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
+
+:end
+@rem End local scope for the variables with windows NT shell
+if "%ERRORLEVEL%"=="0" goto mainEnd
+
+:fail
+rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
+rem the _cmd.exe /c_ return code!
+if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
+exit /b 1
+
+:mainEnd
+if "%OS%"=="Windows_NT" endlocal
+
+:omega
diff --git a/examples/mnist-inference-android/settings.gradle.kts b/examples/mnist-inference-android/settings.gradle.kts
new file mode 100644
index 0000000000..9064f8b077
--- /dev/null
+++ b/examples/mnist-inference-android/settings.gradle.kts
@@ -0,0 +1,24 @@
+pluginManagement {
+ repositories {
+ google {
+ content {
+ includeGroupByRegex("com\\.android.*")
+ includeGroupByRegex("com\\.google.*")
+ includeGroupByRegex("androidx.*")
+ }
+ }
+ mavenCentral()
+ gradlePluginPortal()
+ }
+}
+dependencyResolutionManagement {
+ repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
+ repositories {
+ google()
+ mavenCentral()
+ }
+}
+
+rootProject.name = "mnist inference android"
+include(":app")
+
\ No newline at end of file