Running a Tensorflow model on Android
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Running a TensorFlow model on Android usually means deploying a TensorFlow Lite model, not shipping a full training artifact into the app. TensorFlow Lite is the mobile-oriented runtime designed for smaller binaries, faster startup, and device-friendly inference.
The deployment flow is simple in principle: convert the trained model to .tflite, package it in the app, load it with the Lite interpreter, and feed input tensors that match the training-time shape and preprocessing.
Convert the Model Before It Reaches Android
Model conversion happens outside the Android project, typically in Python after training:
That .tflite file is what the Android app should ship. For production builds, you may later add quantization, but first get the plain model working correctly.
Add the Lite Runtime to the Android App
Put the model file in app/src/main/assets/, then add the TensorFlow Lite dependency:
The exact version depends on the project, but the structure stays the same: dependency plus bundled model asset.
Load the Model from Assets
A common Android pattern is to memory-map the model file and create an Interpreter from that mapping:
This keeps model loading efficient and avoids unnecessary file copying.
Run Inference with the Correct Shapes
Inference works only if the input and output containers match the model's expected tensor shapes and data types.
If the model expects normalized input, the Android code must normalize values the same way training did. A correct model with incorrect preprocessing still produces bad predictions.
Keep Preprocessing Consistent
This is the most common mobile inference bug. If the training pipeline resized images, scaled pixels to 0.0 through 1.0, subtracted mean values, or changed channel order, Android must do the same.
For image input, the app often has to:
- resize to the expected width and height
- convert pixel data to the expected numeric type
- normalize with the same formula used during training
If one of those steps differs, the problem is usually blamed on the model when the real bug is input preparation.
Reuse the Interpreter and Keep Work Off the Main Thread
Creating an interpreter is more expensive than running one inference on an already loaded interpreter. In a real app, it is better to create it once per feature or screen lifecycle and reuse it.
Also avoid running heavy inference on the main thread. Even a modest model can create visible jank if input preparation and inference happen directly in UI callbacks.
Common Pitfalls
- Trying to use a training-time TensorFlow model directly instead of converting to TensorFlow Lite.
- Loading the model correctly but feeding input arrays with the wrong shape or dtype.
- Forgetting that Android preprocessing must exactly match training preprocessing.
- Recreating the interpreter for every prediction instead of reusing it.
- Optimizing with delegates or quantization before the plain baseline inference path is known to work.
Summary
- On Android, deploy TensorFlow models as TensorFlow Lite
.tflitefiles. - Add the Lite runtime dependency and bundle the model in app assets.
- Load the model with an
Interpreter, usually from a memory-mapped asset. - Match Android-side preprocessing and tensor shapes exactly to the training pipeline.
- Reuse the interpreter and keep inference off the main thread when possible.
Related reading
- Running a tensorflow program multiple times each time afresh
- Running a Tensorflow program on an IPU Model throws an Illegal instruction core dumped error
- Running Keras model for prediction in multiple threads
- Running Keras with double precision fails
- Running Adam Optimizer
- Running multiple tensorflow sessions concurrently
- Running Kafka cluster in Docker containers?
- Running RabbitMQ+Celery in the same server as production environment

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.