Open-Source Wikis

/

TensorFlow

/

Apps

/

Java / Android API

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 packaging

Two distinct APIs

The repo has two unrelated Java APIs that share a lineage but address different audiences:

  1. org.tensorflow.* (full TensorFlow runtime in tensorflow/java/). Targets server-side use cases. Wraps the full C API: Graph, Session, Tensor, Operation, eager EagerSession. Ships as a JAR plus the libtensorflow_jni.so native library.
  2. org.tensorflow.lite.* (TFLite in tensorflow/lite/java/). Targets Android. Wraps the TFLite interpreter only — no graph construction. Ships as an Android AAR (tensorflow-lite) with libtensorflowlite_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
  1. App loads tensorflow-lite AAR (the JNI lib is bundled).
  2. Interpreter(file) reads the flatbuffer model from disk/asset.
  3. Java sets input tensors, calls interpreter.run(input, output).
  4. 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.so and //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/ and tensorflow/lite/java/src/main/native/. These are pure C++ files that include tensorflow/c/c_api.h (or the TFLite C API) and define JNIEXPORT symbols.
  • 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 in tensorflow/lite/java/src/main/native/nativeinterpreterwrapper_jni.cc.
  • New TFLite Java delegate wrapper — model after GpuDelegate.java/NnApiDelegate.java and link against the corresponding native delegate library.
  • Full Java API additions are rare; consider contributing to the standalone tensorflow/java repo first.
  • 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.

Java / Android API – TensorFlow wiki | Factory