tensorflow/tensorflow
Java / Android API
Java bindings under tensorflow/java/ (server-side JVM) and tensorflow/lite/java/ (Android via TensorFlow Lite).
Purpose
- Build/load graphs and run them from Java/Kotlin/Scala.
- Run pre-trained models on Android through TensorFlow Lite.
- Provide JNI wrappers around the C API (
tensorflow/c/c_api.h).
Directory layout
tensorflow/java/
├── pom.xml # Maven build descriptor
├── src/main/java/org/tensorflow/ # Public Java classes
├── src/main/native/ # JNI implementation (C++)
└── ...
tensorflow/lite/java/
├── src/main/java/org/tensorflow/lite/ # Interpreter, Tensor, NnApiDelegate, ...
├── src/main/native/ # JNI bridge to libtensorflowlite_jni.so
└── AAR build rules in BUILD # Android Archive packagingTwo distinct APIs
The repo has two unrelated Java APIs that share a lineage but address different audiences:
org.tensorflow.*(full TensorFlow runtime intensorflow/java/). Targets server-side use cases. Wraps the full C API:Graph,Session,Tensor,Operation, eagerEagerSession. Ships as a JAR plus thelibtensorflow_jni.sonative library.org.tensorflow.lite.*(TFLite intensorflow/lite/java/). Targets Android. Wraps the TFLite interpreter only — no graph construction. Ships as an Android AAR (tensorflow-lite) withlibtensorflowlite_jni.so.
Most Android apps only need the TFLite Java API. The full Java API has been somewhat eclipsed by the standalone tensorflow/java repo, which is a more modern, idiomatic Java SDK; what's in this repo is older but still functional.
Key abstractions
Full Java API
| Class | File path (approx.) | Role |
|---|---|---|
org.tensorflow.Graph |
tensorflow/java/src/main/java/org/tensorflow/Graph.java |
Wrapper over TF_Graph. |
org.tensorflow.Session |
tensorflow/java/src/main/java/org/tensorflow/Session.java |
Wrapper over TF_Session. |
org.tensorflow.Tensor |
tensorflow/java/src/main/java/org/tensorflow/Tensor.java |
Wrapper over TF_Tensor. |
org.tensorflow.EagerSession |
tensorflow/java/src/main/java/org/tensorflow/EagerSession.java |
Wrapper over TFE_Context. |
org.tensorflow.SavedModelBundle |
.../SavedModelBundle.java |
SavedModel loading. |
TensorFlow Lite Java API
| Class | Role |
|---|---|
org.tensorflow.lite.Interpreter |
Loads a .tflite model and runs inference. |
org.tensorflow.lite.Tensor |
Input/output tensor view. |
org.tensorflow.lite.nnapi.NnApiDelegate |
Hands subgraphs to Android NNAPI. |
org.tensorflow.lite.gpu.GpuDelegate |
GPU acceleration via OpenCL/OpenGL. |
org.tensorflow.lite.support.* |
Higher-level helpers (image preprocessing, labels). |
How it works (TFLite on Android)
graph LR
App[Android app]
JavaAPI[org.tensorflow.lite.Interpreter]
JNI[libtensorflowlite_jni.so]
Lite[TFLite C++ Interpreter]
Delegate[GPU/NNAPI/XNNPACK delegate]
App --> JavaAPI
JavaAPI -- JNI --> JNI
JNI --> Lite
Lite --> Delegate- App loads
tensorflow-liteAAR (the JNI lib is bundled). Interpreter(file)reads the flatbuffer model from disk/asset.- Java sets input tensors, calls
interpreter.run(input, output). - JNI calls into the C++ TFLite interpreter, which dispatches the op kernels (or the configured delegate's kernels).
Build
- Full Java API:
bazel build //tensorflow/java:libtensorflow_jni.soand//tensorflow/java:tensorflow. - TFLite Android AAR:
bazel build //tensorflow/lite/java:tensorflow-lite. - Maven artifacts are pushed by the release pipeline.
Integration points
- JNI bridge:
tensorflow/java/src/main/native/andtensorflow/lite/java/src/main/native/. These are pure C++ files that includetensorflow/c/c_api.h(or the TFLite C API) and defineJNIEXPORTsymbols. - Backed by: the C API (apps/c-api) for the full Java API; the TFLite interpreter (apps/tensorflow-lite) for TFLite Java.
- Used by: any JVM application that needs to run TF models without spawning Python.
Entry points for modification
- New TFLite Java method — add to
Interpreter.java, then to the JNI bridge intensorflow/lite/java/src/main/native/nativeinterpreterwrapper_jni.cc. - New TFLite Java delegate wrapper — model after
GpuDelegate.java/NnApiDelegate.javaand link against the corresponding native delegate library. - Full Java API additions are rare; consider contributing to the standalone
tensorflow/javarepo first.
Related
- c-api — the C ABI the JNI wrappers call into.
- tensorflow-lite — the runtime backing TFLite Java.
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.